我正在尝试编写一个弹性搜索查询,其中我有一组具有完全相同结构的子文档,我必须检查同一子文档中的两个条件。
我的数据如下所示:
{ "_id": "5672ba7c0d71c93d159c408e", "productId": "1723913526", "title": "Digitab Tablet", "instock": true, "specifications": [{ "category": "In the box", "name": "Test1", "value": "No" }, { "category": "Warranty", "name": "Test2", "value": "Yes" }, { "category": "General", "name": "Test3", "value": "Abcd" }], } }我正在尝试的是:我应该得到products.name为“Test1”的产品和同一子文档的specifications.value为“是”
这是我为此编写的查询:
{ "query": { "bool": { "must": [ { "wildcard": { "specifications.name": "Test1*" } }, { "term": { "instock": "true" } }, { "term": { "specifications.value": "yes" } } ], "must_not": [], "should": [] } } }问题是 - 它不应该返回id为1723913526的产品,因为“value”:“Yes”对于“name”为真:“Test2”而不是“name”:“Test1”。
我希望问题很清楚,如果需要更多信息,请发表评论。
提前致谢。
I am trying to write an elastic search query where I have an array of sub-documents with exactly same structure and I have to check two conditions within the same sub document.
My data looks like this :
{ "_id": "5672ba7c0d71c93d159c408e", "productId": "1723913526", "title": "Digitab Tablet", "instock": true, "specifications": [{ "category": "In the box", "name": "Test1", "value": "No" }, { "category": "Warranty", "name": "Test2", "value": "Yes" }, { "category": "General", "name": "Test3", "value": "Abcd" }], } }What I am trying is : I should get products where specifications.name is "Test1" and specifications.value of the same sub document is "Yes"
Here is the Query that I have written for the this:
{ "query": { "bool": { "must": [ { "wildcard": { "specifications.name": "Test1*" } }, { "term": { "instock": "true" } }, { "term": { "specifications.value": "yes" } } ], "must_not": [], "should": [] } } }The issue is - It should not return the product with id 1723913526 because "value": "Yes" is true for "name": "Test2" and not for "name": "Test1".
I hope the question is clear, please comment if more information is needed.
Thanks in advance.
最满意答案
您需要修改映射以指示specifications是nested类型。
映射如:
"mappings": { "product": { "properties": { ... "specifications": { "type": "nested" } } } }然后查询如下:
"bool": { "must": [{ "nested": { "path": "specifications", "query": { "bool": { "must": [ { "wildcard": { "specifications.first": "Test1*" }}, { "term": { "specifications.value": "yes" }} ] } } }, { "term": { "instock": "true" } } }] }You need to modify your mapping to indicate that specifications is a nested type.
Mapping like:
"mappings": { "product": { "properties": { ... "specifications": { "type": "nested" } } } }And then querying like:
"bool": { "must": [{ "nested": { "path": "specifications", "query": { "bool": { "must": [ { "wildcard": { "specifications.first": "Test1*" }}, { "term": { "specifications.value": "yes" }} ] } } }, { "term": { "instock": "true" } } }] }更多推荐
发布评论