Elasticsearch:当搜索查询以“0”结束时,match_phrase_prefix找不到项目

sr4lhrrt  于 2023-08-03  发布在  ElasticSearch
关注(0)|答案(1)|浏览(94)

在我的Elastic中,我有这样的条目:AB-001-123-B
当运行带有match_phrase_prefix的查询时,如下所示:

"query": {
        "bool": {
            "should": [
                {
                    "match_phrase_prefix": {
                        "code": "AB-00"
                    }
                }
            ]
        }
    },
...

字符串
我没有得到任何结果。将代码更改为

"query": {
        "bool": {
            "should": [
                {
                    "match_phrase_prefix": {
                        "code": "AB-001"
                    }
                }
            ]
        }
    },
...


它返回条目。当从代码中删除-00时,它也会返回该条目。
我对其他条目做了几次测试。当搜索短语以0结尾时,他似乎无法查询。
为什么会这样?有没有办法在查询中解决这个问题?我试过逃跑,没有任何效果。

wljmcqd8

wljmcqd81#

code字段是由standard分析器分析的text字段。这意味着AB-001-123-B被分析为以下标记:

GET _analyze
{
  "analyzer": "standard",
  "text": "AB-001-123-B"
}

Response =>
{
  "tokens" : [
    {
      "token" : "ab",
      "start_offset" : 0,
      "end_offset" : 2,
      "type" : "<ALPHANUM>",
      "position" : 0
    },
    {
      "token" : "001",
      "start_offset" : 3,
      "end_offset" : 6,
      "type" : "<NUM>",
      "position" : 1
    },
    {
      "token" : "123",
      "start_offset" : 7,
      "end_offset" : 10,
      "type" : "<NUM>",
      "position" : 2
    },
    {
      "token" : "b",
      "start_offset" : 11,
      "end_offset" : 12,
      "type" : "<ALPHANUM>",
      "position" : 3
    }
  ]
}

字符串
match_phrase_prefix并不适合您的用例。最好使用prefix查询来查询code.keys字段,如下所示:

"query": {
        "bool": {
            "should": [
                {
                    "prefix": {
                        "code.keys": "AB-001"
                    }
                }
            ]
        }
    },
...

相关问题