我在 Laravel 中使用 Elasticsearch 。
所以我有返回正确结果的查询:
$results = Es::search(array(
'index' => 'testindex',
'type' => $type,
'body' => [
'query' => [
'filtered' => [
'query' => [
'match' => [
'_all' => '2015-02'
]
],
'filter' => [
'bool' => [
'must' => [
'term' => [
'type' => 11
]
],
]
]
]
],
'size' => 5,
'from' => 0
]
));
它的作用是:
这是输出:http://pastebin.com/icWniix4
共有 9 个结果是正确的。
但是当我添加另一个必须项时,它会返回无效结果
$results = Es::search(array(
'index' => 'testindex',
'type' => $type,
'body' => [
'query' => [
'filtered' => [
'query' => [
'match' => [
'_all' => '2015-02'
]
],
'filter' => [
'bool' => [
'must' => [
'term' => [
'type' => 11
],
'term' => [
'public' => 1
]
],
]
]
]
],
'size' => 5,
'from' => 0
]
));
因此,这仅添加了“公共(public)”一词。
它的作用是:
所以现在结果总共有 429 个。它忽略“类型”术语并返回“公共(public)”= 1 的所有内容。但是根据文档,如果我使用 MUST,那么它应该匹配所有这些。搜索结果 http://pastebin.com/cVcatcyi
那么如何编写我需要的查询呢? $查询+类型+公共(public)
官方文档无法回答我的问题。
有什么建议吗?
最佳答案
这是你的麻烦点:
'must' => [
'term' => [
'type' => 11
],
'term' => [
'public' => 1
]
]
在这里,您正在分配
must
的值作为只有一个不同键的关联数组 - term
实际上被分配了两次,所以大概只有一个分配会“存活”(大概 public
存活,因为它出现在定义的最后)。最终结果是 must
最终指向一个只有一个键值对的关联数组。我怀疑你必须做的是:
'must' => [
[
'term' => [
'type' => 11
]
],
[
'term' => [
'public' => 1
]
]
]
现在
must
实际上是指向一个包含两个项目的数组。
关于php - 使用 bool 的 Elasticsearch 过滤器查询返回无效结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28606467/