在 Elasticsearch 中,如何从 multi-level 嵌套 objects 的多个字段上搜索字符串
In Elasticsearch, how do I search string on multiple fields from multi-level nested objects
在 Elasticsearch 6 中,我有嵌套 object 的数据,如下所示:
{
"brands" :
[
{
"brand_name" : "xyz",
"products" :
[
{
"title" : "test",
"mrp" : 100,
"sp" : 90,
"status" : 1
},
{
"title" : "test1",
"mrp" : 50,
"sp" : 45,
"status" : 1
}
]
},
{
"brand_name" : "aaa",
"products" :
[
{
"title" : "xyz",
"mrp" : 100,
"sp" : 90,
"status" : 1
},
{
"title" : "abc",
"mrp" : 50,
"sp" : 45,
"status" : 1
}
]
}
]
}
我想从字段 brand_name 或字段标题中搜索。我希望 return 所有结果都相同 inner_hits。
例如:如果我将搜索字符串输入为 "xyz",它应该 return 两个品牌 object 以及相应的产品 object。
如果我输入搜索字符串 "test" 它应该 return 只有第一个品牌数组只有第一个产品 object.
我怎样才能做到这一点。有什么想法吗?
我试过这样的嵌套路径查询:
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "brands",
"query": {
"bool": {
"should": [
{
"term": {
"brands.brand_name": "xyz"
}
},
{
"term": {
"brands.brand_name.keyword": "aaa"
}
},
{
"nested": {
"path": "brands.products",
"query": {
"bool": {
"should": [
{
"match": {
"brands.products.title": "xyz"
}
}
]
}
},
"inner_hits": {}
}
}
]
}
},
"inner_hits": {}
}
}
]
}
}
}
但是这个查询 returning 有多个 inner_hits 响应,每个品牌和每个产品都有多个数组 objects。
我想要这样的响应,例如与字符串匹配的所有品牌名称都应列在一个数组下,所有产品都应列在同一数组下的另一个数组下 inner_hits。
由于您希望内部匹配根据匹配发生的位置而有所不同,即 brands.brand_name
或 brands.products.title
,您可以有两个独立查询,一个用于品牌名称,另一个用于产品标题嵌套查询。然后这些查询应该在 bool
查询的 should
子句中。每个嵌套查询都应该有自己的 inner_hits
,如下所示:
{
"query": {
"bool": {
"should": [
{
"nested": {
"path": "brands",
"inner_hits": {},
"query": {
"term": {
"brands.brand_name.keyword": "test"
}
}
}
},
{
"nested": {
"path": "brands.products",
"inner_hits": {},
"query": {
"term": {
"brands.products.title": "test"
}
}
}
}
]
}
},
"_source": false
}
在 Elasticsearch 6 中,我有嵌套 object 的数据,如下所示:
{
"brands" :
[
{
"brand_name" : "xyz",
"products" :
[
{
"title" : "test",
"mrp" : 100,
"sp" : 90,
"status" : 1
},
{
"title" : "test1",
"mrp" : 50,
"sp" : 45,
"status" : 1
}
]
},
{
"brand_name" : "aaa",
"products" :
[
{
"title" : "xyz",
"mrp" : 100,
"sp" : 90,
"status" : 1
},
{
"title" : "abc",
"mrp" : 50,
"sp" : 45,
"status" : 1
}
]
}
]
}
我想从字段 brand_name 或字段标题中搜索。我希望 return 所有结果都相同 inner_hits。
例如:如果我将搜索字符串输入为 "xyz",它应该 return 两个品牌 object 以及相应的产品 object。 如果我输入搜索字符串 "test" 它应该 return 只有第一个品牌数组只有第一个产品 object.
我怎样才能做到这一点。有什么想法吗?
我试过这样的嵌套路径查询:
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "brands",
"query": {
"bool": {
"should": [
{
"term": {
"brands.brand_name": "xyz"
}
},
{
"term": {
"brands.brand_name.keyword": "aaa"
}
},
{
"nested": {
"path": "brands.products",
"query": {
"bool": {
"should": [
{
"match": {
"brands.products.title": "xyz"
}
}
]
}
},
"inner_hits": {}
}
}
]
}
},
"inner_hits": {}
}
}
]
}
}
}
但是这个查询 returning 有多个 inner_hits 响应,每个品牌和每个产品都有多个数组 objects。
我想要这样的响应,例如与字符串匹配的所有品牌名称都应列在一个数组下,所有产品都应列在同一数组下的另一个数组下 inner_hits。
由于您希望内部匹配根据匹配发生的位置而有所不同,即 brands.brand_name
或 brands.products.title
,您可以有两个独立查询,一个用于品牌名称,另一个用于产品标题嵌套查询。然后这些查询应该在 bool
查询的 should
子句中。每个嵌套查询都应该有自己的 inner_hits
,如下所示:
{
"query": {
"bool": {
"should": [
{
"nested": {
"path": "brands",
"inner_hits": {},
"query": {
"term": {
"brands.brand_name.keyword": "test"
}
}
}
},
{
"nested": {
"path": "brands.products",
"inner_hits": {},
"query": {
"term": {
"brands.products.title": "test"
}
}
}
}
]
}
},
"_source": false
}