【问题标题】:How to use term query when the keyword type is text in elasticsearchelasticsearch中关键字类型为文本时如何使用term查询
【发布时间】:2020-08-24 13:48:27
【问题描述】:

您好,我在 elasticsearch 中为 claimnumber 字段创建了一个映射,当我使用术语查询搜索术语时,它适用于充满数字的文本,但不适用于字母和数字的文本组合,例如适用于 “123456” 不适用于 “CL123456”

下面的映射

{
  "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"
      }
    }
  }
}

请提出解决此问题的解决方案?

【问题讨论】:

  • 感谢您接受答案????你也可以给答案投票吗

标签: elasticsearch elasticsearch-dsl


【解决方案1】:

在使用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"
        }
      }
    ]

【讨论】:

    猜你喜欢
    • 2022-08-18
    • 2021-07-13
    • 1970-01-01
    • 1970-01-01
    • 2022-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多