mst*_*std 9 elasticsearch laravel-5
我观看了Itamar Syn Hershko的Elasticsearch Do's,Don'ts和Pro-Tips
我在下图中的几个字段中看到多个条件:
我尝试在我的Laravel 5.7应用程序(使用elasticsearch/elasticsearch插件)中创建它,如下面的代码所示:
$elasticQuery = [
"bool" => [
'must' => [
'multi_match' => [
'query' => $text,
'fields' => ['name^4', 'description']
],
],
"should" => [
'term' => [
"category_id" => 1,
]
]
]
];
Run Code Online (Sandbox Code Playgroud)
但是我得到了错误:
{"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}
Run Code Online (Sandbox Code Playgroud)
但是当我使用一个简单的条件时:
$elasticQuery = [
'multi_match' => [
'query' => $text,
'fields' => ['name^4', 'description'],
],
];
Run Code Online (Sandbox Code Playgroud)
我得到了一个有效的结果:
[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
)
)
)
)
)
Run Code Online (Sandbox Code Playgroud)
这是多请求的有效格式吗?
修改后的块#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,
]
]
],
]
]
];
Run Code Online (Sandbox Code Playgroud)
当我需要通过文本字段和上面示例中的类别(1和3)的数组进行搜索时,它可以工作,但看起来像是"OR"条件,但我需要使用"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 ]
]
]
]
]
];
Run Code Online (Sandbox Code Playgroud)