Elasticsearch Query DSL笔记

1. DSL介绍

  Query DSL又叫查询表达式,是一种非常灵活又富有表现力的查询语言,采用JSON接口的方式实现丰富的查询,并使查询更加灵活、精确且更易于调试。

2.全文查询  

http://localhost:9201/bookdb_index1/bookdb_type/_search

  2.1 match_all  

  /_search查找整个ES中所有索引内容,/前面可以加上索引名,多个索引名之前用英文逗号分隔。

{
    "query": {
        "match_all": {}
    }
}

  2.2 match

  下面例子就是查找first_name为Jane的所有记录

{
    "query": {
        "match": {
            "first_name":"Jane"
        }
    }
}

  2.3 multi_match

  在多个字段上执行相同的match查询,下面的例子就表示查询first_name或last_name字段中包含Jane的记录

{
    "query": {
        "multi_match": {
            "query": "Jane",
            "fields": [
                "first_name",
                "last_name"
            ]
        }
    }
}

  2.4 query_string

  可以在查询里面使用AND或OR来完成复杂的查询。下面例子表示查找first_name为"Jane"或者"John"的记录

{
    "query": {
        "query_string": {
            "query": "Jane OR John",
            "fields": [
                "first_name"
            ]
        }
    }
}

  2.5 term

  term可以用来精确匹配 ,精确匹配的值可以是数字,时间,布尔值或者是设置了not_analyzed不分词的字符串

{
    "query": {
        "term": {
            "age": {
        "value" : 25
       } } } }

  term对输入的文本不进行分析,直接匹配输出结果,如果要匹配多个值,可以用terms。  

{
    "query": {
        "terms": {
            "age": [
                25,
                32
            ]
        }
    }
}

  2.6 range

  range用来查询落在指定区间的数字或者时间。下面例子 搜索age在25到35之间的数据。操作符主要有四个 gt大于,gte大于等于,lt小于,lte小于等于

{
    "query": {
        "range": {
            "age": {
                "gte": 25,
                "lte": 35
            }
        }
    }
}

  2.7 exists

  查询出存在某字段文档  

{
    "query": {
        "bool": {
            "must": [
                {
                    "exists": {
                        "field": "age"
                    }
                }
            ]
        }
    },
    "from":0,
    "size":2
}

  2.8 bool组合查询

  通常我们可以需要将很多个条件组合在一起查出最后的结果,这个时候需要使用ES提供的bool来实现。布尔查询支持的子查询共有四种,分别是:must,should,must_not和filter。

  must:类似于SQL中的AND,必须包含;

  must_not:类似于SQL中的NOT,必须不包含;

  should:文档应该匹配should子句查询的一个或多个;

  filter:过滤器,文档必须匹配该过滤条件,跟must子句的唯一区别是,filter不会对结果进行相关性评分_score,换言之当我们的业务中无相关性的要求时,建议查询的过程中多用filter。

  下面是组合查询的例子,我们要查询first_name为Jane且age为32且age不为43的所有数据

{
    "query": {
        "bool": {
            "filter": [
                {
                    "match": {
                        "first_name": "Jane"
                    }
                },
                {
                    "match": {
                        "age": 32
                    }
                }
            ],
            "must_not": {
                "match": {
                    "age": 43
                }
            }
        }
    }
}

  2.9 sort

  sort是排序,也是很常用的查询,这里举age倒叙查询的例子

{
    "query": {
        "match_all": {}
    },
    "sort": [
        {
            "age": {
                "order": "desc"
            }
        }
    ]
}

3.聚合查询

  3.1 分桶

  根据age字段的值进行分桶(有点类似于SQL中的group by),这里的age_bucket是给该桶起的名字  

{
    "query": {
        "match_all": {}
    },
    "aggs": {
        "age_bucket": {
            "terms": {
                "field": "age"
            }
        }
    }
}

  3.2度量

  计算出age字段的最大值(metric有点类似于SQL的avg、max、min),这里的max_age是给该度量起的名字  

{
    "query": {
        "match_all": {}
    },
    "aggs": {
        "max_bucket": {
            "max": {
                "field": "age"
            }
        }
    }
}

 4.业务应用

  参考:https://learnku.com/docs/elasticsearch73/7.3

 

posted @ 2020-06-09 17:38  风墓  阅读(411)  评论(0编辑  收藏  举报