我想使用elasticsearch创建热门搜索查询。
我希望匹配elasticsearch表中的category_name
,brand_name
和title
。匹配应该是短语和有或条件。
我的查询:
$query = [
"bool" => [
"must" => [
["match" => ["is_published" => 1]],
[
"multi_match" => [
"query" => $searchKey,
"fields" => ["category_name", "brand_name", "title"]
]
],
[
'nested' => [
'path' => 'category_with_in_title.parent_cat',
'query' => [
'bool' => [
'must' => [
['match' => [
'category_with_in_title.parent_cat.status' => 1,
]],
],
],
],
],
],
[
'nested' => [
'path' => 'brand',
'query' => [
'bool' => [
'must' => [
'match' => [
'brand.approved_status' => 1,
],
],
],
],
],
],
[
'nested' => [
'path' => 'default_product_low_price_with_seller.seller_detail',
'query' => [
'bool' => [
'must' => [
'match' => [
'default_product_low_price_with_seller.seller_detail.approve_status' => 1,
],
],
],
],
],
],
[
'nested' => [
'path' => 'default_product_low_price_with_seller',
'query' => [
'bool' => [
'must' => [
'match' => [
'default_product_low_price_with_seller.status' => 1,
],
],
],
],
],
],
],
],
];
我使用multi_match
但是如何在此查询中使用pharse?我必须写全文来搜索。
例如:
我想搜索category_name = Tops
如果我写“tops”或“top”或“to”,我想要结果。但是现在我必须写出“Tops”这个确切的词。
提前致谢...
答案 0 :(得分:1)
您可以使用match_phrase_prefix,它与match_phrase相同,但允许在文字的最后一个字词中添加前缀匹配。
您需要做的就是在"type": "phrase_prefix"
查询中添加multi_match
,如下所示:
"multi_match" => [
"query" => $searchKey,
"type": "phrase_prefix",
"fields" => ["category_name", "brand_name", "title"]
]
请告诉我这是否是您正在寻找的。 p>
答案 1 :(得分:1)
理想情况下,您应该multi_match
使用cross_fields
类型,这会将所有字段视为一个大字段并对其运行查询。但是,由于在这些模糊上禁用了模糊性,因此只会显示完全匹配。
请参阅Revelant Github问题 https://github.com/elastic/elasticsearch/issues/6866
因此,我的建议是将multi_match
替换为fuzzy_like_this
(elasticsearch version 1.x)或more_like_this
(elasticsearch version 2.x)。
$query = [
"bool" => [
"must" => [
["match" => ["is_published" => 1]],
[
"fuzzy_like_this" => [
"fields" => ["category_name", "brand_name", "title"],
"like_text" => $searchKey
]
],
....