带有多字段搜索的Elasticsearch语法错误

时间:2018-11-17 08:42:48

标签: elasticsearch laravel-5

我看过Itamar Syn Hershko的 Elasticsearch做,不做和专业提示

  

https://www.youtube.com/watch?v=c9O5_a50aOQ

我在下图中的几个字段中看到多个条件:

  

https://imgur.com/a/17zAZ4w

我试图做到 我的Laravel 5.7应用程序(带有elasticsearch / elasticsearch插件),如以下代码所示:

$elasticQuery = [
    "bool" => [
        'must'   => [
            'multi_match' => [
                'query'  => $text,
                'fields' => ['name^4', 'description']
            ],
        ],
        "should" => [
            'term' => [
                "category_id" => 1,
            ]
        ]
    ]
];

但是我得到了错误:

{"error":{"root_cause":[{"type":"parsing_exception","reason":"[bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":130}],"type":"parsing_exception","reason":"[bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":130},"status":400}

但是当我使用一个简单的条件时:

        $elasticQuery = [
            'multi_match' => [
                'query' => $text,
                'fields' => ['name^4', 'description'],
            ],
        ];

我得到一个有效的结果:

[hits] => Array
    (
        [total] => 1
        [max_score] => 7.4126062
        [hits] => Array
            (
                [0] => Array
                    (
                        [_index] => select_vote
                        [_type] => vote
                        [_id] => 16
                        [_score] => 7.4126062
                        [_source] => Array
                            (
                                [id] => 16
                                [slug] => in-the-film-babe-what-type-of-animal-was-babe
                                [name] => In the film Babe, what type of animal was Babe?
                                [description] => Babe is a 1995 A...
                                [created_at] => 2018-11-10 09:14:15
                                [category_id] => 2
                                [category] => Array
                                    (
                                        [name] => Movie&Cartoons
                                        [slug] => movie-cartoons
                                        [created_at] => 2018-11-10 09:14:12
                                    )

                            )

                    )

            )

    )

这是用于多请求的有效格式吗?

已修改的块#2: 进行搜索后我发现可以正常工作:

$elasticQuery = [
    "bool" => [
        'should' => [


                [
                        "multi_match" => [
                            "query"  => $text,
                            "type"   => "cross_fields",
                            "fields" => [
                                "name^4",
                                "description"
                            ]
                        ]
                ],

                [
                    'match' => [
                        'category_id' => [
                            'query' => 1,
                        ]
                    ]
                ],

                [
                    'match' => [
                        'category_id' => [
                            'query' => 3,
                        ]
                    ]
                ],

        ]
    ]
];

在上面的示例中,当我需要通过文本字段和类别(1和3)的数组进行搜索时,它可以工作,但看起来像是在“ OR”条件下工作,但是我需要进行类似“ AND”使用SQL术语...

哪种方式对“ AND”这样的限制是正确的?

谢谢!

1 个答案:

答案 0 :(得分:5)

如果仅将should更改为must,将无法正常工作,因为category_id不能同时具有两个值(除非它是一个数组,但不是)。 / p>

您需要改用以下查询:

$elasticQuery = [
"bool" => [
    'must' => [
            [
                    "multi_match" => [
                        "query"  => $text,
                        "type"   => "cross_fields",
                        "fields" => [
                            "name^4",
                            "description"
                        ]
                    ]
            ],
    ],
    'filter' => [
            [
                'terms' => [
                    'category_id' => [ 1, 3 ]
                ]
            ]
    ]
]
];