elasticsearch - Elasticsearch匹配查询与文档不匹配

标签 elasticsearch

我正在为地区自动完成功能(一种简单的Google Maps版本)构建搜索器。我的意思是,对于给定的城市(例如费城),我们可以使用其实际名称(Philadelphia),也可以使用其他名称(Philly)。

我正在使用的查询似乎一切正常:

{
  "query": {
    "bool": {
      "must": {
        "multi_match": {
          "query": "Philly",
          "type": "best_fields",
          "fields": [
            "locality",
            "alternative_names"
          ],
          "operator": "and"
        }
      },
      "filter": {
        "term": {
          "country_code": "US"
        }
      }
    }
  }
}

我发现的问题与西类牙的一个城市有关: Majadahonda

/ localities_index / localities / 2387
{
  "_index": "localities_index",
  "_type": "localities",
  "_id": "2387",
  "_version": 1,
  "_seq_no": 133,
  "_primary_term": 4,
  "found": true,
  "_source": {
    "country_code": "es",
    "locality": "Majadahonda",
    "alternative_names": []
  }
}

您可以搜索Majadahond并匹配它(请参见下面的部分名称示例查询)
{
    "query": {
        "match": {
            "locality": {
                "query" : "Majadahond"
            }
        }
    }
}

/ localities_index / localities / 2387 / _explain
{
  "_index": "localities_index",
  "_type": "localities",
  "_id": "2387",
  "matched": true,
  "explanation": {
    "value": 7.568702,
    "description": "weight(locality:majadahond in 112) [PerFieldSimilarity], result of:",
    "details": [
      {
        "value": 7.568702,
        "description": "score(freq=1.0), product of:",
        "details": [
          {
            "value": 2.2,
            "description": "boost",
            "details": []
          },
          {
            "value": 7.8130527,
            "description": "idf, computed as log(1 + (N - n + 0.5) / (n + 0.5)) from:",
            "details": [
              {
                "value": 1,
                "description": "n, number of documents containing term",
                "details": []
              },
              {
                "value": 3708,
                "description": "N, total number of documents with field",
                "details": []
              }
            ]
          },
          {
            "value": 0.44032967,
            "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": 8.341694,
                "description": "avgdl, average length of field",
                "details": []
              }
            ]
          }
        ]
      }
    ]
  }
}

但是,如果使用其全名,则不会。我找不到解释。所以我尝试了一个更简单的查询,只是舍弃了其他问题:
{
    "query": {
        "match": {
            "locality": {
                "query" : "Majadahonda"
            }
        }
    }
}

/ localities_index / localities / 2387 / _explain
{
  "_index": "localities_index",
  "_type": "localities",
  "_id": "2387",
  "matched": false,
  "explanation": {
    "value": 0.0,
    "description": "no matching term",
    "details": []
  }
}

/ localities_index / _settings
{
  "localities_index": {
    "settings": {
      "index": {
        "number_of_shards": "1",
        "blocks": {
          "read_only_allow_delete": "false"
        },
        "provided_name": "localities_index",
        "creation_date": "1591341622712",
        "analysis": {
          "analyzer": {
            "autocomplete": {
              "filter": [
                "lowercase",
                "asciifolding"
              ],
              "tokenizer": "autocomplete"
            },
            "autocomplete_search": {
              "filter": [
                "lowercase",
                "asciifolding"
              ],
              "tokenizer": "lowercase"
            }
          },
          "tokenizer": {
            "autocomplete": {
              "token_chars": [
                "letter",
                "digit"
              ],
              "min_gram": "2",
              "type": "edge_ngram",
              "max_gram": "15"
            }
          }
        },
        "number_of_replicas": "1",
        "uuid": "zgdfv5bBS8-XkK5Aoh9XZA",
        "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"
        }
      }
    }
  }
}

最佳答案

您的egde_gram token 生成器限制为10个字符,因此最后一个 token 为majadahond(长度为10),而没有尾随的a:

enter image description here

因此,您可能需要增加max_gram设置。

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

相关文章:

.net - elasticsearch c#/.net客户端推荐

php - Laravel 上的 Elasticsearch 关系

使用 NGram Tokenizer 时,ElasticSearch 不遵守最大 NGram 长度

sorting - ES按距离排序时出现错误

elasticsearch - 获取id为 'ID1'或 'ID2'的文档

elasticsearch - 如何使用 Elasticsearch 从文本中找到类似的标签

elasticsearch - Elasticsearch 6-在一个索引中使用一种还是两种类型?

elasticsearch - 追加到嵌套对象字段

mysql - 使用 Node.js 搜索 PDF 文本并返回片段

Elasticsearch 问题类型移除