elasticsearch中关键字类型为文本时如何使用术语查询

r1zhe5dt  于 2021-06-15  发布在  ElasticSearch
关注(0)|答案(1)|浏览(423)

嗨,我已经在elasticsearch for claimnumber字段中创建了一个Map,当我使用term query搜索术语时,它可以很好地用于充满数字的文本,但不适用于字母和数字的文本组合,例如,适用于“123456”,不适用于“cl123456”
下面的Map

{
  "duckcreek" : {
    "mappings" : {
      "properties" : {
        "@timestamp" : {
          "type" : "date"
        },
        "@version" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
          "claimnumber" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
          "id" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "policynumber" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "url" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}

为数字工作

GET duckcreek/_search
{
  "query": {
    "term": {
      "claimnumber": {
        "value": "99520"
      }
    }
  }
}

不使用数字文本

GET duckcreek/_search
{
  "query": {
    "term": {
      "claimnumber": {
        "value": "CL123456"
      }
    }
  }
}

请提出解决方案?

fnx2tebb

fnx2tebb1#

在使用analyze api对文本字符串执行分析时

GET /_analyze

{
  "analyzer" : "standard",
  "text" : "CL123456"
}

生成的令牌是:

{
            "tokens": [
                {
                    "token": "cl123456",
                    "start_offset": 0,
                    "end_offset": 8,
                    "type": "<ALPHANUM>",
                    "position": 0
                }
            ]
        }

搜索查询

{
  "query": {
    "term": {
      "title.keyword": {     <-- note this
        "value": "CL123456"
      }
    }
  }
}

搜索结果

"hits": [
      {
        "_index": "matchprase1",
        "_type": "_doc",
        "_id": "2",
        "_score": 0.6931471,
        "_source": {
          "title": "CL123456"
        }
      }
    ]

相关问题