我想使用弹性搜索创建顶部搜索查询。
我想从弹性搜索表中匹配category_name
、brand_name
和title
。匹配项应为短语和with或condition。
我的查询:
$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
的记录
若我写"top"、"top"或"to",我想要结果。但现在我必须写"Tops"这个词。
提前感谢。。。
您可以使用match_phrase_prefix,它与match_p赫拉se相同,只是允许在文本中的最后一个术语上使用前缀匹配。
您所需要做的就是将"type": "phrase_prefix"
添加到multi_match
查询中,如下所示:
"multi_match" => [
"query" => $searchKey,
"type": "phrase_prefix",
"fields" => ["category_name", "brand_name", "title"]
]
如果这是你想要的,请告诉我。
理想情况下,您应该将multi_match
与cross_fields
类型一起使用,这将把所有字段视为一个大字段并对其运行查询。但是,由于这些字段禁用了模糊性,因此只显示完全匹配的字段。
请参阅Revelant Github问题https://github.com/elastic/elasticsearch/issues/6866
因此,我的建议是将multi_match
替换为fuzzy_like_this
(弹性搜索1.x版)或more_like_this
(弹性搜索2.x版)
$query = [
"bool" => [
"must" => [
["match" => ["is_published" => 1]],
[
"fuzzy_like_this" => [
"fields" => ["category_name", "brand_name", "title"],
"like_text" => $searchKey
]
],
....