【问题标题】:Elastic search filter query with bool returns invalid results使用 bool 的弹性搜索过滤器查询返回无效结果
【发布时间】:2015-02-20 00:28:59
【问题描述】:

我在 Laravel 中使用弹性搜索。

所以我有返回正确结果的查询:

$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
  • 字段“类型”必须为 11。

这里是输出: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
    ]
));

因此,这仅添加了“公共”一词。 它的作用是:

  • 在所有字段中搜索 $query AND
  • 字段“类型”必须是 11 并且
  • 字段“public”必须为 1

所以现在结果总共有 429 个。它忽略“类型”术语并返回“公共”= 1 的所有内容。但是根据文档,如果我使用 MUST,那么它应该匹配所有这些。搜索结果http://pastebin.com/cVcatcyi

那么我怎样才能写出我需要的查询呢? $query + type + public

官方文档没有回答我的问题。

有什么建议吗?

【问题讨论】:

    标签: php laravel-4 elasticsearch


    【解决方案1】:

    这是你的麻烦点:

                        'must' => [
                            'term' => [
                                'type' => 11
                            ],
                            'term' => [
                                'public' => 1
                            ]
                        ]
    

    在这里,您将 must 的值分配为只有一个不同键的关联数组 - term 实际上被分配了两次,所以大概只有一个分配会“存活”(大概是 public幸存下来,因为它出现在定义的最后)。最终结果是must 最终指向一个只有一个键值对的关联数组。

    我怀疑你必须这样做:

                        'must' => [
                            [
                                'term' => [
                                    'type' => 11
                                ]
                            ],
                            [
                                'term' => [
                                    'public' => 1
                                ]
                            ]
                        ]
    

    现在must 实际上指向一个包含两个项目的数组。

    【讨论】:

    • 谢谢。现在我因为错过了而变得愚蠢。
    猜你喜欢
    • 1970-01-01
    • 2021-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-20
    • 1970-01-01
    • 2016-07-28
    • 2015-01-25
    相关资源
    最近更新 更多