我在弹性搜索中遇到嵌套查询问题。我正在通过 java api 构建一个复杂的嵌套查询,以在我的索引上运行过滤查询。从 elasticsearch 返回的结果表明有 1 个匹配项([total:1,take:79ms]),但这并没有返回任何“命中”作为响应的一部分。
def response = client.search(request).actionGet()
def searchHits = response.hits() // <--- this is empty
result.total = searchHits.totalHits() // <--- this is populated with one
result.took = response.getTook()
通过 curl 或 rest 客户端正确运行完全相同的查询会带回整个匹配文档。因此,我知道查询在这两种情况下都是正确构造的。
size 和 from 参数都设置为默认值,应该返回文档。
如果我将嵌套查询移动到过滤器部分,我会得到相同的匹配文档,但会填充 hits() 对象。
查询本身如下
{
"filtered" : {
"query" : {
"bool" : {
"must" : [ {
"nested" : {
"query" : {
"bool" : {
"must_not" : {
"bool" : {
"must" : [ {
"range" : {
"nested.from" : {
"from" : "2013-06-01T23:00:14.343Z",
"to" : "2013-06-02T23:00:14.343Z",
"include_lower" : true,
"include_upper" : true
}
}
}, {
"term" : {
"nested.status" : "accepted"
}
} ]
}
},
"should" : [ {
"bool" : {
"must" : [ {
"range" : {
"nested.from" : {
"from" : "2013-06-01T23:00:14.343Z",
"to" : "2013-06-02T23:00:14.343Z",
"include_lower" : true,
"include_upper" : true
}
}
}, {
"term" : {
"nested.status" : "saved"
}
} ]
}
} ]
}
},
"path" : "nested"
}
} ]
}
} ]
}
},
"filter" : {
"and" : {
"filters" : [ {
"type" : {
"value" : "user"
}
}]
}
}
}
}