[英]Search exact phrase by multiple fields in ElasticSearch
I have products with attributes.我有带属性的产品。 For example "name", "brand", "color", "category", "size".例如“名称”、“品牌”、“颜色”、“类别”、“尺寸”。 When I'm searching products by phrase (for example "black jacket puma"), besides exact match with "brand" = "puma", "color" = "black", "category" = "jacket" or "name" = "Black puma jacket", I have also products with a partial match.当我按词组搜索产品时(例如“blackjack puma”),除了与“brand”=“puma”、“color”=“black”、“category”=“jacket”或“name”=完全匹配外“黑色美洲豹夹克”,我也有部分搭配的产品。 My query is:我的查询是:
'match' => [
'message' => [
'query' => "black puma jacket"
'operator' => 'and'
]
]
I also tried this query:我也试过这个查询:
'multi_match' => [
'fields' => [
'brand',
'color',
'name'
],
'query' => 'puma black jacket',
]
What's wrong with my query?我的查询有什么问题?
UPD:更新:
My mappings:我的映射:
'brand' => [
'type' => 'string',
'fields' => [
'keyword' => [
'type' => 'string',
'analyzer' => 'slug',
'index_options' => 'docs',
],
'raw' => [
'type' => 'string',
'analyzer' => 'format',
]
]
],
'color' => [
'type' => 'string',
'fields' => [
'keyword' => [
'type' => 'string',
'analyzer' => 'slug',
'index_options' => 'docs',
],
'raw' => [
'type' => 'string',
'analyzer' => 'format',
]
]
],
'category' => [
'type' => 'string',
'fields' => [
'keyword' => [
'type' => 'string',
'analyzer' => 'slug',
'index_options' => 'docs',
],
'raw' => [
'type' => 'string',
'analyzer' => 'format',
]
]
],
'category_id' => [
'type' => 'integer',
],
'store_id' => [
'type' => 'integer',
],
'size' => [
'type' => 'string',
'fields' => [
'keyword' => [
'type' => 'string',
'analyzer' => 'slug',
'index_options' => 'docs',
],
'raw' => [
'type' => 'string',
'analyzer' => 'format',
]
]
],
'material' => [
'type' => 'string',
'fields' => [
'keyword' => [
'type' => 'string',
'analyzer' => 'slug',
'index_options' => 'docs',
],
'raw' => [
'type' => 'string',
'analyzer' => 'format',
]
]
],
'type' => [
'type' => 'string',
'fields' => [
'keyword' => [
'type' => 'string',
'analyzer' => 'slug',
'index_options' => 'docs',
],
'raw' => [
'type' => 'string',
'analyzer' => 'format',
]
]
],
'volume' => [
'type' => 'string',
'fields' => [
'keyword' => [
'type' => 'string',
'analyzer' => 'slug',
'index_options' => 'docs',
],
'raw' => [
'type' => 'string',
'analyzer' => 'format',
]
]
],
'price' => [
'type' => 'float',
],
'desc' => [
'type' => 'string',
],
'sku' => [
'type' => 'string',
'index' => 'not_analyzed'
],
'picture' => [
'type' => 'string',
'index' => 'not_analyzed'
]
]; ];
Base on your mappings and requirements i think cross_fields might help you.根据您的映射和要求,我认为cross_fields可能会对您有所帮助。
Example with only 2 properties (color and category):只有 2 个属性(颜色和类别)的示例:
Post a few documents:贴几个文件:
POST my_index/_doc/1
{
"color": "black",
"category": "1"
}
POST my_index/_doc/2
{
"color": "black",
"category": "2"
}
POST my_index/_doc/3
{
"color": "black",
"category": "3"
}
POST my_index/_doc/4
{
"color": "1",
"category": "jacket"
}
POST my_index/_doc/5
{
"color": "2",
"category": "jacket"
}
POST my_index/_doc/6
{
"color": "3",
"category": "jacket"
}
POST my_index/_doc/6
{
"color": "3",
"category": "jacket"
}
POST my_index/_doc/7
{
"color": "black",
"category": "jacket"
}
POST my_index/_doc/8
{
"color": "black",
"category": "jacket"
}
Your search query will look like:您的搜索查询将如下所示:
GET my_index/_search
{
"query": {
"multi_match": {
"query": "black jacket",
"fields": [],
"type": "cross_fields",
"operator": "and",
"analyzer": "standard"
}
}
}
Results:结果:
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 1.2192403,
"hits" : [
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "7",
"_score" : 1.2192403,
"_source" : {
"color" : "black",
"category" : "jacket"
}
},
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "8",
"_score" : 1.2192403,
"_source" : {
"color" : "black",
"category" : "jacket"
}
}
]
}
As you can see we didn't get all other document with partial match of black or jacket正如你所看到的,我们没有得到所有其他黑色或夹克部分匹配的文件
Hope this help希望这有帮助
According to your requirement, I would group brand, color & category into one field and then apply phrase match.根据您的要求,我会将品牌、颜色和类别归为一个字段,然后应用短语匹配。 You need to modify products mapping您需要修改产品映射
PUT /products
{
"mappings":{
"properties":{
"brand": {
"type": "text",
"copy_to": "name",
...
},
"color": {
"type": "text",
"copy_to": "name",
...
},
"category": {
"type": "text",
"copy_to": "name",
...
},
"name": {
"type": "text"
},
...
}
}
}
Search using phrase match on name field在名称字段上使用短语匹配进行搜索
GET /products/_search
{
"query":{
"match_phrase": {
"name": {
"query": {
"name": "black puma jacket",
"slop": 1
}
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.