Elasticsearch 查询

524 阅读2分钟
原文链接: mp.weixin.qq.com

Elasticsearch 查询

1. es 6.3 以上,可以使用官方自带的sql模块

[sql 查询文档] https://www.elastic.co/guide/en/elasticsearch/reference/current/xpack-sql.html#xpack-sql

Ip:port/_xpack/sql?format=txt, Content-Type=application/json

    {

    "query": "show tables" // query value 就是sql

    }

2. 其他的可以使用es-sql插件 好像也蛮方便,[插件地址] https://github.com/NLPchina/elasticsearch-sql

3. 本文主要介绍 ES DSL查询, [DSL文档] https://www.elastic.co/guide/cn/elasticsearch/guide/current/_most_important_queries.html

Ip:port/Index/_search, Content-Type=application/json

3.1 query

DSL queries查询 filters过滤
查询速度
查询类型 匹配相关度 _source 精确查询是否满足 Yes/No
是否缓存
3.1.1 match_all

match_all 查询简单的 匹配所有文档。在没有指定查询方式时,它是默认的查询

    {

    "query": {

    "match_all": {}

    }

    }

3.1.2 match,match_phrase

match分词后查询, match_phase 则按照短语查询

⚠️:

  • 多个字段查询,需要使用bool 组合查询

    {

    "query": {

    "match": {

    "name": "张三 李四" // 张三 李四 为or关系

    }

    }

    }

    {

    "query": {

    "match_phase": {

    "name": "张三"

    }

    }

    }

3.1.3 multi_match

multi_match 查询可以在多个字段上执行相同的 match 查询

    {

    "query": {

    "multi_match": {

    "query": "张三",

    "fields": ["name", "desc*"] // 可使用`*`进行模糊匹配

    }

    }

    }

3.1.4 range

range 查询找出那些落在指定区间内的数字或者时间, gt=大于,gte=大于等于,lt=小于, lte=小于等于

    {

    "range": {

    "age": {

    "gte": 20,

    "lt": 30

    }

    }

    }

3.1.5 term,terms

查询被用于精确值 匹配,这些精确值可能是数字、时间、布尔或者那些 not_analyzed 的字符串,terms 允许多值匹配

⚠️ :

  • term不分词进行查询;

  • match分词后进行查询, 然后按照 _score显示;

  • match_phrase按照短语查询,存在这个短语才会被显示

    {

    "query": {

    "term": {

    "name":["张三"]

    }

    }

    }

    {

    "query": {

    "terms": {

    "name":["张三", "李四"]

    }

    }

    }

3.1.6 exists,missing

exists 查询和 missing 查询被用于查找那些指定字段中有值 (exists) 或无值 (missing) 的文档。这与SQL中的 ISNULL (missing) 和 NOT ISNULL (exists) 在本质上具有共性

    {

    "query": {

    "exists": {

    "field":"gender"

    }

    }

    }

3.1.7 bool

如果需要多个字段查询,需要使用组合多查询 bool,支持must=and、 must_not=not、should=or、filter=equal

⚠️:

  • 如果没有 must 语句,那么至少需要能够匹配其中的一条  should 语句。但,如果存在至少一条  must 语句,则对  should 语句的匹配没有要求。

    {

    "bool": {

    "must": [

    {"match": {"name": "三毛"} }

    ],

    "must_not": [

    {"match": {"gender": "man"} }

    ],

    "should": [

    { "match": { "tag": "beautiful"} },

    { "range": { "age": { "gte": 16 } } }

    ]

    }

    }

3.2. sort

Es 默认通过_score desc 排序

[排序文档] https://www.elastic.co/guide/cn/elasticsearch/guide/current/_Sorting.html

    {

    "query": {

    "match_all":{}

    },

    "sort": [

    {"age":{"order": "desc"}}

    ] // 也可以 "sort": "age" 默认 asc 排序

    }

3.3. from, size

偏移量, 返回数量

    {

    "query": {

    "match_all":{}

    },

    "from":0,

    "size":10

    }

3.4. _source

支持includes,excludes

    {

    "query": {

    "match_all":{}

    },

    "from":0,

    "size":10,

    "_source":{

    "includes":["age", "name"]

    }// 如果没有excludes, 可以直接 "_source": ["age", "name"]

    }

3.5 返回字段

key mean
took 耗费多少毫秒
timed_out 是否超时
_shards 分片,对于搜索请求,会打到所有的primary shard
hits 查询的所有结果集对象
hits.total 查询结果数
hits.max_score 匹配度
hits.hits 结果集
hits.hits._index 该文档Indx
hits.hits._type 该文档type
hits.hits._id 该文档的ID
hits.hits._score 该文档的匹配度
hits.hits._scource 具体的json内容