Elasticsearch 精确查询:术语查询、多术语查询、主键查询、范围查询、存在查询、前缀查询、正则查询与通配符查询

发布于:2024-04-27 ⋅ 阅读:(28) ⋅ 点赞:(0)

为了深入理解Elasticsearch的各种精确查询方法,我们将通过一系列示例来演示术语查询、多术语查询、主键查询、范围查询、存在查询、前缀查询、正则查询和通配符查询的应用场景。首先,我们为示例准备一组数据:

POST person/_doc/1
{
  "id": "1",
  "sex": true,
  "name": "张三",
  "born": "2020-09-18 00:02:20",
  "location": {
    "lat": 41.12,
    "lon": -71.34
  }
}

POST person/_bulk
{"index":{"_id":"3"}}
{"id":"3","name":"王五","sex":true,"born":"2020-09-14 00:02:20","location":{"lat":11.12,"lon":-71.34}}
{"index":{"_id":"4"}}
{"id":"4","name":"李四","sex":false,"born":"2020-10-14 00:02:20", "location":{"lat":11.12,"lon":-71.34}}
{"index":{"_id":"5"}}
{"id":"5","name":"黄六","sex":false,"born":"2020-11-14 00:02:20", "location":{"lat":11.12,"lon":-71.34}}
POST person/_bulk
{"index":{"_id":"2"}}
{"id":"2","name":"赵二","sex":true,"born":"2020-09-14 00:02:20","location":{"lat":11.12,"lon":-71.34}}
{"update":{"_id":"5"}}
{ "doc" : {"sex" : "false","born" : "2020-01-01 00:02:20"} }
{"delete":{"_id":"5"}}

接下来,我们将逐一展示各类查询方法的使用。

术语查询(Term Query)

术语查询用于匹配字段中确切的单个值。例如,查找名为“张三”的人:

POST person/_search
{
  "query": {
    "term": {
      "name.keyword": {
        "value": "张三"
      }
    }
  }
}

多术语查询(Terms Query)

多术语查询允许同时匹配多个确切值。查找名为“张三”或“王五”的人:

POST person/_search
{
  "query": {
    "terms": {
      "name.keyword": [
        "张三",
        "王五"
      ]
    }
  }
}

主键查询(IDs Query)

主键查询通过文档ID直接查找特定文档。查找ID为“1”和“2”的文档:

POST person/_search
{
  "query": {
    "ids": {
      "values": ["1","2"]
    }
  }
}

范围查询(Range Query)

范围查询用于筛选出某个字段值在指定区间内的文档。查找出生日期在“2020年9月11日至9月13日”之间的人员:

POST person/_search
{
  "query": {
    "range": {
      "born": {
        "gte": "2020/09/11 00:00:00",
        "lte": "2020/09/13 00:00:00",
        "format": "yyyy/MM/dd HH:mm:ss"
      }
    }
  }
}

POST person/_search
{
  "query": {
    "range": {
      "born": {
        "gte": "2020/09/11 08:00:00",
        "lte": "2020/09/13 08:00:00",
        "format": "yyyy/MM/dd HH:mm:ss",
        "time_zone": "+08:00"
      }
    }
  }
}

存在查询(Existence Query)

存在查询用于筛选出具有特定字段的文档。在以下示例中,我们首先为文档添加“age”字段,然后查找包含该字段的文档:

POST person/_doc/5
{
  "id": "5",
  "sex": true,
  "name": "刘大",
  "born": "2020-02-18 00:02:20",
  "age": 20,
  "location": {
    "lat": 21.12,
    "lon": -71.34
  }
}

POST person/_doc/5
{
  "id": "5",
  "sex": true,
  "name": "刘大",
  "born": "2020-02-18 00:02:20",
  "age": 20,
  "location": {
    "lat": 21.12,
    "lon": -71.34
  }
}

POST person/_search
{
  "query": {
    "exists": {
      "field": "age"
    }
  }
}

前缀查询(Prefix Query)

前缀查询适用于查找字段值以特定前缀开头的文档。首先创建一个索引并设置“address”字段启用前缀索引,然后插入示例数据,最后进行前缀查询:

PUT prefix-test
{
  "mappings": {
    "properties": {
      "address": {
        "type": "text",
        "index_prefixes": {
          "min_chars" : 1,
          "max_chars" : 5
        }
      }
    }
  }
}

PUT prefix-test/_bulk
{"index":{"_id":"1"}}
{"id":"1","address":"wuhan qingshan"}
{"index":{"_id":"2"}}
{"id":"2","address":"guangzhou baiyun"}
{"index":{"_id":"3"}}
{"id":"3","address":"beijing chaoyang"}

POST prefix-test/_search
{
  "query": {
    "prefix": {
      "address": {
        "value": "baiy"
      }
    }
  }
}

请注意,如果字段类型为keyword而非text,则不能使用index_prefixes参数。keyword字段的前缀搜索会比较耗费性能,不宜大量使用。

正则查询(Regexp Query)

正则查询利用正则表达式模式匹配字段值。查找名字中包含“大”字的人:

POST person/_search
{
  "query": {
    "regexp": {
      "name.keyword": ".*大.*"
    }
  }
}

通配符查询(Wildcard Query)

通配符查询使用通配符字符(如?*)匹配字段值。查找名字以任意单个字符后接“大”字的人:

POST person/_search
{
  "query": {
    "wildcard": {
      "name.keyword": "?大"
    }
  }
}

正则查询和通配符查询虽然使用简便,但其性能开销较大,大量使用时需谨慎。

以上就是Elasticsearch中常用的精确查询方法,包括术语查询、多术语查询、主键查询、范围查询、存在查询、前缀查询、正则查询和通配符查询的示例。根据实际需求选择合适的查询类型,可以高效地从Elasticsearch索引中检索所需数据。