【ElasticSearch】精確匹配text字段 用match加.keyword 或 term


【ElasticSearch】精確匹配text字段 用match加.keyword 或 term


1.錯誤示范

由於記憶混淆,記成了使用match_phrase對text字段精確匹配。

#測試match_phrase
GET /test/external/_search
{
  "query":{
    "bool": {
      "must": [
        {
          "match_phrase": {
            "nodealias": "92新增"
          }
        }
      ]
    }
  }
}

結果

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 3.7249355,
    "hits" : [
      {
        "_index" : "test",
        "_type" : "external",
        "_id" : "EcTGtHsBO1AHMH7HW",
        "_score" : 3.7249355,
        "_source" : {
          "key_id" : "url_http_18536276217",
          "nodealias" : "92新增app配置",
          "taskFinishTime" : "1630825478282",
          "result" : {
            "http_request" : {
              "responseTime" : 89.0
            }
          }
        }
      }
    ]
  }
}

2.使用match字段+.keyword

GET /test/external/_search
{
  "query":{
    "bool": {
      "must": [
        {
          "match": {
            "nodealias.keyword": "92新增app配置"
          }
        }
      ]
    }
  }
}

結果

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.9924302,
    "hits" : [
      {
        "_index" : "test",
        "_type" : "external",
        "_id" : "EcTGtHsBO1AHMH7HW",
        "_score" : 1.9924302,
        "_source" : {
          "key_id" : "url_http_18536276217",
          "nodealias" : "92新增app配置",
          "taskFinishTime" : "1630825478282",
          "result" : {
            "http_request" : {
              "responseTime" : 89.0
            }
          }
        }
      }
    ]
  }
}

3.將字段設為keyword類型后,就可以使用term精確匹配text字段

可以看到key_id是keyword類型的:


GET /test/external/_search
{
  "query":{
    "bool": {
      "must": [
        {
          "term": {
            "key_id": "url_http_18536276217"
          }
        }
      ]
    }
  }
}

結果

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.9924302,
    "hits" : [
      {
        "_index" : "test",
        "_type" : "external",
        "_id" : "EcTGtHsBO1AHMH7HW",
        "_score" : 1.9924302,
        "_source" : {
          "key_id" : "url_http_18536276217",
          "nodealias" : "92新增app配置",
          "taskFinishTime" : "1630825478282",
          "result" : {
            "http_request" : {
              "responseTime" : 89.0
            }
          }
        }
      }
    ]
  }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM