首页 > 解决方案 > 在 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'
]   

];

标签: elasticsearch

解决方案


根据您的映射和要求,我认为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"
        }
      }
   ]
}

如您所见,我们没有得到所有其他文件与黑色夹克部分匹配

希望这有帮助


推荐阅读