ElasticSearch有关查询的那些事
Posted ??lv:D^
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ElasticSearch有关查询的那些事相关的知识,希望对你有一定的参考价值。
ElasticSearch有关查询的那些事
上一篇文章介绍了一下ElasticSearch的一些基础的索引,文档的CRUD方法,接下来介绍一些花式查询的操作
修改
在说查询之前先补充一下之前没讲到的修改功能的内容
全量修改
现在先重新创建一个新的索引以及下的文档以便观察的更直观一点
创建索引laptop
PUT http://localhost:9200/laptop
在laptop下创建文档 id号为114514,1919,810
POST http://localhost:9200/laptop/doc/114514
http://localhost:9200/laptop/doc/1919
http://localhost:9200/laptop/doc/810
Body插入的JSON串(3个)
{
"brand":"平果",
"model":"军工级主板,航天级cpu",
"price":"1999",
"memory":{
"level_1":"512G",
"level_2":"1T",
"level_3":"2T"
}
},
{
"brand":"大米",
"model":"散热出色的笔记本",
"price":"2999",
"memory":{
"level_1":"512G",
"level_2":"1T",
"level_3":"2T"
}
},
{
"brand":"三新",
"model":"可能会爆炸",
"price":"3999",
"memory":{
"level_1":"512G",
"level_2":"1T",
"level_3":"2T"
}
}
创建好啦 现在开始进行全量修改的操作,作为三新的厂商我觉得这个model型号太难听了,而且我都内存要再升级加多一个level_4的3T版本,我们应该这样操作
PUT http://localhost:9200/laptop/_doc/810
最后查询发现全局更改成功
局部修改
如果我们只是觉得价钱太贵了想降下价而去修改全局这会不会未免太浪费了,那应该如何去改单个价格?
POST http://localhost:9200/laptop/_update/810
修改过后发现价钱已经改变
查询
条件查询
如果我在库中只想挑选三新的电脑应该如何操作,接下来就用条件查询了,插入json形式的请求体获得想要的数据
GET http://localhost:9200/laptop/_search
BODY RAW JSON
{
"query":{
"match":{
"brand":"三新"
}
}
}
最后得到的结果
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.3862942,
"hits": [
{
"_index": "laptop",
"_type": "_doc",
"_id": "810",
"_score": 1.3862942,
"_source": {
"brand": "三新",
"model": "sanxing pro max",
"price": "3888",
"memory": {
"level_1": "512G",
"level_2": "1T",
"level_3": "2T",
"level_4": "3T"
}
}
}
]
}
}
分页查询
分页查询也是相似的操作
GET http://localhost:9200/laptop/_search
BODY RAW JSON
{
"query":{
//match_all 指查询全部文档
"match_all":{
}
},
//from 从第几页开始
"from" : 0,
//size 每一页看到几个数据
"size" : 2
}
执行后的查询结果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "laptop",
"_type": "_doc",
"_id": "114514",
"_score": 1.0,
"_source": {
"brand": "平果",
"model": "军工级主板,航天级cpu",
"price": "1999",
"memory": {
"level_1": "512G",
"level_2": "1T",
"level_3": "2T"
}
}
},
{
"_index": "laptop",
"_type": "_doc",
"_id": "1919",
"_score": 1.0,
"_source": {
"brand": "大米",
"model": "散热出色的笔记本",
"price": "2999",
"memory": {
"level_1": "512G",
"level_2": "1T",
"level_3": "2T"
}
}
}
]
}
}
哇上面的数据好多 我只想知道他们的品牌和价钱就够了,那价格_source就OK啦
BODY RAW JSON
{
"query":{
//match_all 指查询全部文档
"match_all":{
}
},
//from 从第几页开始
"from" : 0,
//size 每一页看到几个数据
"size" : 2
"_source":["brand","price"]
}
执行后的查询结果
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "laptop",
"_type": "_doc",
"_id": "114514",
"_score": 1.0,
"_source": {
"price": "1999",
"brand": "平果"
}
},
{
"_index": "laptop",
"_type": "_doc",
"_id": "1919",
"_score": 1.0,
"_source": {
"price": "2999",
"brand": "大米"
}
}
]
}
}
那么再给他们的价钱排一下顺序咯 我喜欢从大到小desc,小到大就是asc咯
BODY RAW JSON
{
"query":{
//match_all 指查询全部文档
"match_all":{
}
},
//from 从第几页开始
"from" : 0,
//size 每一页看到几个数据
"size" : 2,
"_source":["brand","price"],
"sort":{
"price":{
"order":"desc"
}
}
}
//接下来得到的结果会报错
因为是price是一个字符串无法将字符串自动转数字进行排序,大家要注意这一点
聚合查询
还是一样的GET请求url,此时我想按品牌来分组就要使用到聚合查询啦
GET http://localhost:9200/laptop/_search
BODY RAW JSON
{
"aggs":{//聚合操作
"price_group":{//名称,随意起名
"terms":{//分组
"field":"brand" //分组字段
}
}
}
}
接下来还有映射查询,多条件查询,范围查询等操作,有兴趣的同学可以上官网去查询相关的文档。
[Getting Started | Elasticsearch Reference 6.0] | Elastic
以上是关于ElasticSearch有关查询的那些事的主要内容,如果未能解决你的问题,请参考以下文章
使用Elasticsearch7.10.2新版本,新手需要注意的那些事