elasticsearch - Elasticsearch匹配查询与带有撇号的文档不匹配

标签 elasticsearch autocomplete

我正在为本地自动完成功能(一种简单的Google Maps版本)构建搜索器。我正在使用的查询似乎一切正常:

{
  "query": {
    "bool": {
      "must": {
        "multi_match": {
          "query": "Ametlla",
          "type": "best_fields",
          "fields": [
            "locality",
            "alternative_names"
          ],
          "operator": "and"
        }
      },
      "filter": {
        "term": {
          "country_code": "ES"
        }
      }
    }
  }
}
我发现的问题与西类牙的一个城市有关: L'Ametlla de Mar
/ localities_index / localities / 10088
{
  "_index": "localities_index",
  "_type": "localities",
  "_id": "10088",
  "_version": 1,
  "_seq_no": 133,
  "_primary_term": 4,
  "found": true,
  "_source": {
    "country_code": "es",
    "locality": "L'Ametlla de Mar",
    "alternative_names": []
  }
}
您可以搜索Ametlla并匹配它(请参见下面的部分名称示例查询)
{
    "query": {
        "match": {
            "locality": {
                "query" : "Ametlla"
            }
        }
    }
}
/ localities_index / localities / 10088 / _explain
{
  "_index": "localities_index",
  "_type": "localities",
  "_id": "10088",
  "matched": true,
  "explanation": {
    "value": 3.3985975,
    "description": "weight(locality:ametlla in 2) [PerFieldSimilarity], result of:",
    "details": [
      {
        "value": 3.3985975,
        "description": "score(freq=1.0), product of:",
        "details": [
          {
            "value": 2.2,
            "description": "boost",
            "details": []
          },
          {
            "value": 3.6686769,
            "description": "idf, computed as log(1 + (N - n + 0.5) / (n + 0.5)) from:",
            "details": [
              {
                "value": 2,
                "description": "n, number of documents containing term",
                "details": []
              },
              {
                "value": 97,
                "description": "N, total number of documents with field",
                "details": []
              }
            ]
          },
          {
            "value": 0.4210829,
            "description": "tf, computed as freq / (freq + k1 * (1 - b + b * dl / avgdl)) from:",
            "details": [
              {
                "value": 1.0,
                "description": "freq, occurrences of term within document",
                "details": []
              },
              {
                "value": 1.2,
                "description": "k1, term saturation parameter",
                "details": []
              },
              {
                "value": 0.75,
                "description": "b, length normalization parameter",
                "details": []
              },
              {
                "value": 9.0,
                "description": "dl, length of field",
                "details": []
              },
              {
                "value": 7.5360823,
                "description": "avgdl, average length of field",
                "details": []
              }
            ]
          }
        ]
      }
    ]
  }
}
但是,如果使用其全名,则不会。
我尝试将punctuation添加到token_chars,就像在https://stackoverflow.com/a/49362505上看到的那样,但是没有用。因此,我尝试将'添加为custom_token_chars,但此方法也不起作用。
/ localities_index / _settings
{
  "localities_index": {
    "settings": {
      "index": {
        "number_of_shards": "1",
        "provided_name": "localities_index",
        "creation_date": "1596537683568",
        "analysis": {
          "analyzer": {
            "autocomplete": {
              "filter": [
                "lowercase",
                "asciifolding"
              ],
              "tokenizer": "autocomplete"
            },
            "autocomplete_search": {
              "filter": [
                "lowercase",
                "asciifolding"
              ],
              "tokenizer": "lowercase"
            }
          },
          "tokenizer": {
            "autocomplete": {
              "token_chars": [
                "letter",
                "digit"
              ],
              "custom_token_chars": "'",
              "min_gram": "1",
              "type": "edge_ngram",
              "max_gram": "15"
            }
          }
        },
        "number_of_replicas": "1",
        "uuid": "lS3Ork2zSySYJbJYmx29aw",
        "version": {
          "created": "7040099"
        }
      }
    }
  }
}
/ localities_index / _mapping
{
  "localities_index": {
    "mappings": {
      "properties": {
        "alternative_names": {
          "type": "text",
          "analyzer": "autocomplete",
          "search_analyzer": "autocomplete_search"
        },
        "country_code": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "locality": {
          "type": "text",
          "analyzer": "autocomplete",
          "search_analyzer": "autocomplete_search"
        }
      }
    }
  }
}

最佳答案

您可以在自定义分析器中使用Apostrophe token filter并在您的字段(包含它们的位置)上使用它,并使用已在使用的match查询,因​​为它将使用在索引时间使用的同一分析器,您将获得预期的结果。

关于elasticsearch - Elasticsearch匹配查询与带有撇号的文档不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63245897/

相关文章:

elasticsearch - Elasticsearch 仅选择最大字段值不同的

javascript - p :autocomplete completemethod isn't fired with copy-pate with mouse events

json - 如何将bash和curl与转义的json一起使用?

elasticsearch - 升级到Elasticsearch 5.2

angularjs - elasticsearch 上的自动完成/提前输入 angularjs Bootstrap

c++ - Eclipse (Nvidia NSight 7.0) C++ 计时自动完成

silverlight - 为 SL3 和最新的 SL 工具包修复 Tim Heuer 的 EditableComboBox

c# - 逗号或分号分隔的自动完成文本框

ruby-on-rails - 使用 Elasticsearch/Tire 展平多态 AR 关系

elasticsearch - NEST(elasticsearch)多字段高亮