【问题标题】:Elasticsearch match certain fields exactly but not othersElasticsearch 完全匹配某些字段,但不匹配其他字段
【发布时间】:2017-08-18 21:12:37
【问题描述】:

我需要 ElasticSearch 来精确匹配某些字段,目前使用 multi_match。

例如,用户输入long beach chiropractor

我希望 long beach 与 city 字段完全匹配,而不是返回 seal beachglass beach 的结果。

同时chiropractor也应该匹配chiropractic

这是我正在使用的当前查询:

"query": {
    "bool": {
        "should": [
            {
                "multi_match": {
                                "fields": [
                                    "title",
                                    "location_address_address_1.value",
                                    "location_address_city.value^2",
                                    "location_address_state.value",
                                    "specialty" // e.g. chiropractor
                                ],
                                "query": "chiropractor long beach",
                                "boost": 6,
                                "type": "cross_fields"
                }
            }
        ]
    }
},

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    正确的方法是将搜索的术语与位置分开,并将位置存储为关键字类型。如果这不可能,那么您可以使用同义词标记器将位置存储为单个标记,但这需要拥有所有可能位置的列表。例如

    {
      "settings": {
        "analysis": {
          "filter": {
            "my_synonym_filter": {
              "type": "synonym",
              "synonyms": [
                "long beach=>long-beach"
              ]
            }
          },
          "analyzer": {
            "my_synonyms": {
              "tokenizer": "standard",
              "filter": [
                "lowercase",
                "my_synonym_filter"
              ]
            }
          }
        }
      }
    }
    

    现在如果你打电话

    POST /my_index/_analyze?analyzer=my_synonyms    
    {
        "text": ["chiropractor long beach"]
    }
    

    回应是

    {
        "tokens": [
            {
                "token": "chiropractor",
                "start_offset": 0,
                "end_offset": 12,
                "type": "<ALPHANUM>",
                "position": 0
            },
            {
                "token": "long-beach",
                "start_offset": 13,
                "end_offset": 23,
                "type": "SYNONYM",
                "position": 1
            }
        ]
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多