为什么这个 elasticsearch 查询没有 return 任何东西

Why this elasticsearch query doesnt return anything

我执行了以下 elasticsearch 查询。

GET amasyn/_search
{
    "query": {
        "bool" : {
            "filter" : {
                "term": {"ordernumber": "112-9550919-9141020"}
            }
        }
    }
}

但是没有任何命中

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}

但我有一份文档在索引中包含此 ordernumberordernumber 是一个文本字段。

当我通过将 term 替换为 match 来更改上述查询时,我得到的总命中数是给定查询的命中数。 请解释这里发生了什么以及如何解决这个问题。

这是因为您使用了 ordernumber 类型为文本的字段,所以它正在被分析。请通过此答案参考文本和关键字之间的区别

通过这种方式,您可以为 ordernumber 字段定义文本和关键字。

映射

{
    "mappings": {
        "properties": {
            "ordernumber": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            }
        }
    }
}

然后您可以使用如下术语查询:

{
    "query": {
        "bool" : {
            "filter" : {
                "term": {"ordernumber.keyword": "112-9550919-9141020"}
            }
        }
    }
}

请查看 textkeyword 字段是如何为您的文本标记化的。

标准分析仪

当您将字段定义为 text 时使用此分析器。

{
    "analyzer": "standard",
    "text" : "112-9550919-9141020"
}

结果:

 {
        "tokens": [
            {
                "token": "112",
                "start_offset": 0,
                "end_offset": 3,
                "type": "<NUM>",
                "position": 0
            },
            {
                "token": "9550919",
                "start_offset": 4,
                "end_offset": 11,
                "type": "<NUM>",
                "position": 1
            },
            {
                "token": "9141020",
                "start_offset": 12,
                "end_offset": 19,
                "type": "<NUM>",
                "position": 2
            }
        ]
    }

关键字分析器

当您将字段定义为 keyword 时使用此分析器。

{
    "analyzer": "keyword",
    "text" : "112-9550919-9141020"
}

结果

 {
        "tokens": [
            {
                "token": "112-9550919-9141020",
                "start_offset": 0,
                "end_offset": 19,
                "type": "word",
                "position": 0
            }
        ]
    }