elasticsearch - 在 ElasticSearch 中按多个字段搜索确切的短语
问题描述
我有带有属性的产品。例如“名称”、“品牌”、“颜色”、“类别”、“尺寸”。当我按短语搜索产品时(例如“black jack puma”),除了与“brand”=“puma”、“color”=“black”、“category”=“jacket”或“name”= “黑色彪马夹克”,我也有偏配的产品。我的查询是:
'match' => [
'message' => [
'query' => "black puma jacket"
'operator' => 'and'
]
]
我也试过这个查询:
'multi_match' => [
'fields' => [
'brand',
'color',
'name'
],
'query' => 'puma black jacket',
]
我的查询有什么问题?
升级版:
我的映射:
'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'
]
];
解决方案
根据您的映射和要求,我认为cross_fields可能会对您有所帮助。
只有 2 个属性(颜色和类别)的示例:
贴几个文件:
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"
}
您的搜索查询将如下所示:
GET my_index/_search
{
"query": {
"multi_match": {
"query": "black jacket",
"fields": [],
"type": "cross_fields",
"operator": "and",
"analyzer": "standard"
}
}
}
结果:
{
"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"
}
}
]
}
如您所见,我们没有得到所有其他文件与黑色或夹克部分匹配
希望这有帮助
推荐阅读
- c# - 尽可能快地在 DataTable 中找到匹配的记录
- php - nginx 错误:图像的 openat() 失败(20:不是目录)
- javascript - 设置页面谷歌分析
- oauth - 如何为访问 API 的第三方 Web 应用程序存储 API 密钥
- python - 为什么这个 dict 在 python 代码中不可迭代,但在 django 模板中可迭代?
- javascript - 如何从 reducer 修改 redux store 的状态
- html - 未添加复选框及其标签之间的空间
- c# - 如何在mysql存储过程中返回输出参数
- php - 您可以从位于单个结果集中的两个单独的表中提取数据吗?[不加入问题]
- angular - 如何启用托管在 Azure 应用服务中的 Angular 6 asp.net core 2.1 的 gzip 压缩