Elasticsearch术语查询到数字令牌

时间:2019-06-17 12:50:10

标签: elasticsearch term

我需要向包含字符串中数字部分的Elasticsearch数据库解释术语查询的一些奇怪行为。查询非常简单:

{
    "query": {
        "bool": {
            "should": [
                {
                    "term": {
                        "address.street": "8 kvetna"
                    }
                }
            ]
        }
    }
}

问题在于 8 kvetna 会返回空结果。我试图对其进行_analyze分析,使其生成常规令牌,例如8,k,kv,kve...。此外,我很确定数据库中的值是8 kvetna。 这是该字段的映射:

{
    "settings": {
        "index": {
            "refresh_interval": "1m",
            "number_of_shards": "1",
            "number_of_replicas": "1",
            "analysis": {
                "filter": {
                    "autocomplete_filter": {
                        "type": "edge_ngram",
                        "min_gram": "1",
                        "max_gram": "20"
                    }
                },
                "analyzer": {
                    "autocomplete": {
                        "filter": [
                            "lowercase",
                            "asciifolding",
                            "autocomplete_filter"
                        ],
                        "type": "custom",
                        "tokenizer": "standard"
                    }
                    "default": {
                        "filter": [
                            "lowercase",
                            "asciifolding"
                        ],
                        "type": "custom",
                        "tokenizer": "standard"
                    }
                }
            }
        }
    },
    "mappings": {
        "doc": {
            "dynamic": "strict",
            "_all": {
                "enabled": false
            },
            "properties": {
                "address": {
                    "properties": {
                        "city": {
                            "type": "text",
                            "analyzer": "autocomplete"
                        },
                        "street": {
                            "type": "text",
                            "analyzer": "autocomplete"
                        }
                    }
                }
            }
        }
    }
}

什么导致了这个奇怪的结果?我不明白感谢您的帮助。

1 个答案:

答案 0 :(得分:3)

到目前为止很好的开始!唯一的问题是您正在使用term查询,而应该使用match查询。 term查询将尝试与8 kvetna进行完全匹配,而这并不是您想要的。以下查询将起作用:

{
  "query": {
    "bool": {
      "should": [
        {
          "match": {                               <--- change this
            "address.street": "8 kvetna"
          }
        }
      ]
    }
  }
}