带有 bool returns 无效结果的弹性搜索过滤器查询
Elastic search filter query with bool returns invalid results
我在 Laravel 中使用弹性搜索。
所以我查询 returns 正确的结果:
$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
]
));
它的作用是:
- 在所有字段中搜索 $query AND
- 字段 "type" 必须为 11。
这是输出:http://pastebin.com/icWniix4
共有 9 个结果是正确的。
但是当我添加另一个必须项时,它 returns 无效结果
$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" 添加术语。
它的作用是:
- 在所有字段中搜索 $query AND
- 字段 "type" 必须为 11 AND
- 字段"public"必须为1
所以现在结果总共是 429。它忽略了 "type" 术语和 returns 所有 "public" = 1 的东西。但是根据文档,如果我使用 MUST 那么它应该匹配所有他们中的。搜索结果 http://pastebin.com/cVcatcyi
那么我该如何编写我需要的查询呢? $查询 + 类型 + public
官方文档没有回答我的问题
有什么建议吗?
这是你的麻烦点:
'must' => [
'term' => [
'type' => 11
],
'term' => [
'public' => 1
]
]
在这里,您将 must
的值分配为只有一个不同键的关联数组 - term
实际上被分配了两次,因此大概只有一个分配会 "survive"(大概 public
幸存下来,因为它出现在定义的最后)。最终结果是 must
最终指向一个只有一对键值对的关联数组。
我怀疑你必须做的是:
'must' => [
[
'term' => [
'type' => 11
]
],
[
'term' => [
'public' => 1
]
]
]
现在 must
实际上指向一个包含两个项目的数组。
我在 Laravel 中使用弹性搜索。
所以我查询 returns 正确的结果:
$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
]
));
它的作用是:
- 在所有字段中搜索 $query AND
- 字段 "type" 必须为 11。
这是输出:http://pastebin.com/icWniix4 共有 9 个结果是正确的。
但是当我添加另一个必须项时,它 returns 无效结果
$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" 添加术语。 它的作用是:
- 在所有字段中搜索 $query AND
- 字段 "type" 必须为 11 AND
- 字段"public"必须为1
所以现在结果总共是 429。它忽略了 "type" 术语和 returns 所有 "public" = 1 的东西。但是根据文档,如果我使用 MUST 那么它应该匹配所有他们中的。搜索结果 http://pastebin.com/cVcatcyi
那么我该如何编写我需要的查询呢? $查询 + 类型 + public
官方文档没有回答我的问题
有什么建议吗?
这是你的麻烦点:
'must' => [
'term' => [
'type' => 11
],
'term' => [
'public' => 1
]
]
在这里,您将 must
的值分配为只有一个不同键的关联数组 - term
实际上被分配了两次,因此大概只有一个分配会 "survive"(大概 public
幸存下来,因为它出现在定义的最后)。最终结果是 must
最终指向一个只有一对键值对的关联数组。
我怀疑你必须做的是:
'must' => [
[
'term' => [
'type' => 11
]
],
[
'term' => [
'public' => 1
]
]
]
现在 must
实际上指向一个包含两个项目的数组。