gpt4 book ai didi

多字段搜索的 Elasticsearch 语法错误

转载 作者:行者123 更新时间:2023-11-29 02:46:04 25 4
gpt4 key购买 nike

我观看了 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
)

)

)

)

)

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

修改 block #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)进行搜索时,它可以工作,但看起来它像“或”条件一样工作,但我需要进行限制,如“AND"使用 SQL 术语...

为了像“AND”这样的限制,哪种方式是正确的?

谢谢!

最佳答案

如果您只是将 should 更改为 must 它不会起作用,因为 category_id 不能同时具有两个值(除非它是一个数组,但它不是)。

您需要改用以下查询:

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

关于多字段搜索的 Elasticsearch 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53349629/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com