我正在使用Nest Elastic并使用Head插件为 bool(boolean) 搜索构建查询,我正在组合多个查询
Notes about DB Structure and Elastic Mapping
依次具有多个属性
在此查询中,我试图获取所有具有特定配置文件和属性值> 30的文档,同时请记住,此属性应仅具有ID 2。
SQL查询:
从文档d内部联接属性中选择av。*,d.name属性值av
上
d.DocumentId = av.DocumentId
其中d.profileid = 1和av.AttributeId = 2且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
结果还包含一个具有以下属性值的文档
不能包含此文档,因为它不能满足我的需求。
如何建立此查询?
最佳答案
解决方案是通过使lstChildren成为嵌套对象来首先更改映射。然后,使用嵌套查询将确保符合指定的所有条件。下面的嵌套查询指定了两个仅返回预期结果的条件,但为了简单起见,我对“IntValue”使用“等于”而不是“大于”:
{
"query": {
"nested": {
"path": "lstChildren",
"query": {
"bool": {
"must": [
{
"match": {
"lstChildren.AttributeID":"2"
}
},
{
"match": {
"lstChildren.IntValue": "31"
}
}
]
}
}
}
}
}
关于elasticsearch - Nest ElasticSearch:使用嵌套查询和嵌套对象的 bool 搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34400601/