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

【ElasticSearch】精确匹配text字段 用match加.keyword 或 term & terms匹配数组内的多个元素


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
            }
          }
        }
      }
    ]
  }
}

*另外,还可以指定 text数组内部元素为keyword类型,然后就可以用term精准匹配数组内的值了。

查询语句

"query": {
    "bool": {
      "must": [
        {
          "term": {
            "bgIds": {
              "value": "2"
            }
          }
        }
      ]
    }
}

查询结果


4.terms匹配数组内的多个元素

参考:https://www.elastic.co/guide/en/elasticsearch/reference/7.14/query-dsl-terms-query.html#query-dsl-terms-lookup-example


terms在aggregation中用于分组,在query中可以精确匹配数组中的多个元素

  • 下面语句 能匹配到bgIds数组中有4或2的数据
"query": {
    "bool": {
      "must": [
        {
          "terms": {
            "bgIds": [
              "4",
              "2"
            ],
            "boost": 1
          }
        }
      ]
    }
}

posted @ 2021-09-29 21:50  musecho  阅读(1596)  评论(0编辑  收藏  举报