Elastic Search Fliter在同一子文档中的两个字段上(Elastic Search Fliter on two fields within same sub document)

编程入门 行业动态 更新时间:2024-10-14 04:27:14
Elastic Search Fliter在同一子文档中的两个字段上(Elastic Search Fliter on two fields within same sub document)

我正在尝试编写一个弹性搜索查询,其中我有一组具有完全相同结构的子文档,我必须检查同一子文档中的两个条件。

我的数据如下所示:

{ "_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" } } }] }

更多推荐

本文发布于:2023-08-04 22:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1422651.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   两个   文档   在同一   Search

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!