【ElasticSearch】02 查询操作

准备样本:

Elasticsearch 提供了基于 JSON 提供完整的查询 DSL 来定义查询
查询条件还适用于删除操作
 
创建索引:
# PUT
http://127.0.0.1:9200/student/

推5个文档进索引:

# POST 
http://localhost:9200/student/_doc/1001
{
    "name":"zhangsan",
    "nickname":"zhangsan",
    "sex":"男",
    "age":30
}

# POST 
http://localhost:9200/student/_doc/1002
{
    "name":"lisi",
    "nickname":"lisi",
    "sex":"男",
    "age":20
}

# POST 
http://localhost:9200/student/_doc/1003
{
    "name":"wangwu",
    "nickname":"wangwu",
    "sex":"女",
    "age":40
}

# POST 
http://localhost:9200/student/_doc/1004
{
    "name":"zhaoliu",
    "nickname":"zhaoliu",
    "sex":"女",
    "age":30
}

# POST 
http://localhost:9200/student/_doc/1005
{
    "name":"qianqi",
    "nickname":"qianqi",
    "sex":"女",
    "age":50
}

 

 

查询操作:

查询索引下的所有文档:

http://127.0.0.1:9200/student/_search

结果:

{
    "took": 4,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "student",
                "_type": "_doc",
                "_id": "ui1dlX4BrIYsUbCa81Wh",
                "_score": 1.0,
                "_source": {
                    "name": "Cloud9",
                    "gender": "male",
                    "age": 24
                }
            },
            {
                "_index": "student",
                "_type": "_doc",
                "_id": "1001",
                "_score": 1.0,
                "_source": {
                    "name": "Cloud9",
                    "gender": "male",
                    "age": 24
                }
            },
            {
                "_index": "student",
                "_type": "_doc",
                "_id": "1002",
                "_score": 1.0,
                "_source": {
                    "name": "Cloud9",
                    "gender": "male",
                    "age": 24
                }
            }
        ]
    }
}

 

 

查询参数:

默认查询无条件,等同于这个条件:

{
    "query": {
        "match_all": {}
    }
}
# "query":这里的 query 代表一个查询对象,里面可以有不同的查询属性
# "match_all":查询类型,例如:match_all(代表查询所有), match,term , range 等等
# {查询条件}:查询条件会根据类型的不同,写法也有差异

 

匹配K键"name"为”zhangsan“的文档

{
    "query": {
        "match": {
            "name": "zhangsan"
        }
    }
}

匹配多个字段取值查询

{
    "query": {
        "multi_match": {
            "query": "zhangsan",
            "fields": [
                "name",
                "nickname"
            ]
        }
    }
}

精准查询

{
    "query": {
        "term": {
            "name": {
                "value": "zhangsan"
            }
        }
    }
}

多字段精准查询

{
    "query": {
        "terms": {
            "name": [
                "zhangsan",
                "lisi"
            ]
        }
    }
}

筛选指定字段展示:

{
    "_source": [
        "name",
        "nickname"
    ],
    "query": {
        "terms": {
            "nickname": [
                "zhangsan"
            ]
        }
    }
}

包含,或者排除字段:

{
    "_source": {
        "excludes": [ "name", "nickname" ],
        "includes": ["name","nickname"]
    },
    "query": {
        "terms": {
            "nickname": [
                "zhangsan"
            ]
        }
    }
}

 

组合条件查询

must,必须匹配

must_not, 必须不匹配

should, 应该匹配

{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "name": "zhangsan"
                    }
                }
            ],
            "must_not": [
                {
                    "match": {
                        "age": "40"
                    }
                }
            ],
            "should": [
                {
                    "match": {
                        "sex": "男"
                    }
                }
            ]
        }
    }
}

 

范围查询

gt 大于

lt 小于

gte 大于等于

lte 小于等于

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

模糊查询

{
    "query": {
        "fuzzy": {
            "title": {
                "value": "zhangsan"
            }
        }
    }
}

查询排序

{
    "query": {
        "match": {
            "name": "zhangsan"
        }
    },
    "sort": [
        { "age": { "order": "desc" } }
    ]
}

多个字段查询排序

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

 

查询高亮显示:
Elasticsearch 可以对查询内容中的关键字部分,进行标签和样式(高亮)的设置。
在使用 match 查询的同时,加上一个 highlight 属性:
 pre_tags:前置标签
 post_tags:后置标签
 fields:需要高亮的字段
 title:这里声明 title 字段需要高亮,后面可以为这个字段设置特有配置,也可以空
{
    "query": {
        "match": {
            "name": "zhangsan"
        }
    },
    "highlight": {
        "pre_tags": "<font color='red'>",
        "post_tags": "</font>",
        "fields": {
            "name": {}
        }
    }
}

 

分页查询
from:当前页的起始索引,默认从 0 开始。 from = (pageNum - 1) * size
size:每页显示多少条
{
    "query": {
        "match_all": {}
    },
    "sort": [
        { "age": { "order": "desc" } }
    ],
    "from": 0,
    "size": 2
}

 

聚合查询

取最大值

# 获取age值最大的文档
{
"aggs": { "max_age": { "max": { "field": "age" } } }, "size": 0 }

取最小值

{
    "aggs": {
        "min_age": {
            "min": { "field": "age" }
        }
    },
    "size": 0
}

求和

{
    "aggs": {
        "sum_age": { "sum": { "field": "age" } }
    },
    "size": 0
}

求平均

{
    "aggs": {
        "avg_age": {  "avg": { "field": "age" } }
    },
    "size": 0
}

 

对某个字段的值进行去重之后再取总数
{
    "aggs": {
        "distinct_age": { "cardinality": { "field": "age" } }
    },
    "size": 0
}

 

state属性,一次性返回所有聚合值,max,min,sum,avg,count

{
    "aggs": {
        "stats_age": {
            "stats": {
                "field": "age"
            }
        }
    },
    "size": 0
}

 

桶聚合操作

{
    "aggs": {
        "age_groupby": {
            "terms": {
                "field": "age"
            }
        }
    },
    "size": 0
}

 

 

 

 
posted @ 2022-01-26 17:34  emdzz  阅读(62)  评论(0编辑  收藏  举报