在ElasticSearch中按多个字段搜索确切短语

时间:2019-11-22 22:16:11

标签: elasticsearch

我有带有属性的产品。例如“名称”,“品牌”,“颜色”,“类别”,“尺寸”。当我按词组(例如“ black jacket 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',
]

我的查询出了什么问题?

UPD:

我的映射:

'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'
]   

];

2 个答案:

答案 0 :(得分:1)

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

如您所见,我们没有得到所有其他文档的部分匹配为 black jacket

希望获得帮助

答案 1 :(得分:0)

根据您的要求,我将品牌,颜色和类别分组为一个字段,然后应用词组匹配。您需要修改产品映射

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"
      },
      ...
    }
  }
}

在名称字段上使用词组匹配进行搜索

GET /products/_search
{
  "query":{
    "match_phrase": {
      "name": {
        "query": {
          "name": "black puma jacket",
          "slop": 1
        }
      }
    }
  }
}