【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
            }
          }
        }
      }
    ]
  }
}
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值