在elasticsearch中从数组中选择匹配的对象 [英] select matching objects from array in elasticsearch
本文介绍了在elasticsearch中从数组中选择匹配的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<代码>{班级:1,用户:[{名称:'abc',姓氏:'def'}, {名称: 'xyz',姓氏:'wef'}, {名称:'abc',姓氏:'pqr'}]}
我有一个类似于上述对象的文档结构,我只想返回名称为abc"的用户,但问题是它匹配名称abc"但返回所有数组.我只想要匹配的用户.
映射 -
<代码>{"类":"字符串",用户":{类型":嵌套",特性": {名称":{类型":字符串"},姓氏":{类型":字符串"}}}}
解决方案
那么,如果您将 users
字段映射为 nested
类型,这是一个好的开始!>
使用 嵌套的inner_hits
,您可以使用这样的查询仅检索匹配的用户名:
<代码>{_source":假,询问": {嵌套":{路径":用户","inner_hits": { <---- 这就是魔法发生的地方_来源": [名称"]},询问": {布尔":{必须": [{学期": {"users.name": "abc"}}]}}}}}
{
class: 1,
users: [{
name: 'abc',
surname: 'def'
}, {
name: 'xyz',
surname: 'wef'
}, {
name: 'abc',
surname: 'pqr'
}]
}
I have a document structure like above object and I want to return only users who have name 'abc' but problem is it matches name 'abc' but returns all array. I want only matched users .
Mapping -
{
"class":"string",
"users" : {
"type" : "nested",
"properties": {
"name" : {"type": "string" },
"surname" : {"type": "string" }
}
}
}
解决方案
Then if you have your users
field mapped as nested
type, it's a good start!
Using nested inner_hits
, you can retrieve only the matching user names with a query like this one:
{
"_source": false,
"query": {
"nested": {
"path": "users",
"inner_hits": { <---- this is where the magic happens
"_source": [
"name"
]
},
"query": {
"bool": {
"must": [
{
"term": {
"users.name": "abc"
}
}
]
}
}
}
}
}
这篇关于在elasticsearch中从数组中选择匹配的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文