Nest ElasticSearch:使用嵌套查询和嵌套对象进行布尔搜索 [英] Nest ElasticSearch: Boolean Search using Nested Query and Nested Objects
问题描述
我正在使用Nest Elastic,并使用Head插件构建一个布尔搜索的查询,我结合多个查询
I am using Nest Elastic and building the query for a Boolean search using Head plugin , i am combining multiple queries
有关DB的注意事项结构和弹性映射
Notes about DB Structure and Elastic Mapping
- 数据库中的每个文档都链接到特定profileId,其中
turn有多个属性 - 每个文档都有多个与之关联的属性值
,我试图获取具有特定配置文件和属性值> 30的所有文档,请注意,此属性应仅具有属性ID 2。
In this query, i am trying to get all documents which has specific profile and attribute value > 30 keeping in mind that this attribute should have the attribute Id 2 only.
SQL查询:
从文档d内部连接属性中选择av。*,d.name $ val
d.DocumentId = av.DocumentId
其中d.profileid = 1和av.AttributeId = 2,av.Intvalue> 30
Select av.*, d.name from document d inner join attributeValue av on d.DocumentId = av.DocumentId where d.profileid = 1 and av.AttributeId = 2 and av.Intvalue >30
弹性查询
Elastic Query
{ "query": {
"bool": {
"must": [
{
"term": { "Document.profileid": "1" }
}
,
{
"term": {"Document.lstChildren.AttributeID": "2" }
}
,
{
"range": { "Document.lstChildren.IntValue": { "gt": "30"} }
}
,
{
"match_all": { }
}
],
"must_not": [ ],
"should": [ ]
}
}, "from": 0, "size": 10, "sort": [ ], "facets": { }
}
问题
Problem
结果还包含具有以下属性值的文档
The result also contains a document that has the following attribute values
- 属性值= 3,attributeId = 2(值为< 30)
- 属性值= 34,但attributeId不同于2 (不正确)
- Attribute value = 3 and attributeId = 2 (the value is < 30)
- Attribute value = 34 but with attributeId different than 2 (incorrect)
不能包含此文档,因为它不符合我的需要。
This document must not be included as it doesn't satisfy my needs.
如何构建此查询?
推荐答案
解决方案是首先通过使lstChildren成为嵌套对象来更改映射。然后使用嵌套查询将确保符合指定的所有条件。下面的嵌套查询指定了仅返回预期结果的两个条件,但是对于IntValue,我使用Equal而不是大于,以保持简单:
The Solution is to change the mapping first by making lstChildren a nested object. Then using nested query will ensure that all conditions are met as specified. The nested query below specifies two conditions that returns only expected results, but i used "Equal" instead of "greater than" for the "IntValue" in order keep it simple:
{
"query": {
"nested": {
"path": "lstChildren",
"query": {
"bool": {
"must": [
{
"match": {
"lstChildren.AttributeID":"2"
}
},
{
"match": {
"lstChildren.IntValue": "31"
}
}
]
}
}
}
}
}
这篇关于Nest ElasticSearch:使用嵌套查询和嵌套对象进行布尔搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!