博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Elasticsearch7 检索API
阅读量:3788 次
发布时间:2019-05-22

本文共 3738 字,大约阅读时间需要 12 分钟。

简介

对es来说,有两种基本的检索方式,一种是通过URI传递检索参数,另一种是通过request body检索。通过request body进行检索,可以在json结构中传递更多信息,同时增强了可读性。

内容

通过URI检索

GET /bank/_search?q=*&sort=account_number:asc&pretty

说明:上面将搜索出bank中所有的document,其中q=*将会匹配在index中的所有的document,sort=account_number:asc表示使用account_number排序。如果直接使用GET /bank/_search默认也可以将全部document进行检索。搜索的结果如下:

{    "took": 27,    "timed_out": false,    "_shards": {        "total": 1,        "successful": 1,        "skipped": 0,        "failed": 0    },    "hits": {        "total": {            "value": 3,            "relation": "eq"        },        "max_score": 1,        "hits": [            {                "_index": "test",                "_type": "test1",                "_id": "1",                "_score": 1,                "_source": {                    "title": "In the weeks that followed the creation of 996.ICU in March"                }            },            {                "_index": "test",                "_type": "test1",                "_id": "2",                "_score": 1,                "_source": {                    "title": "In the weeks that followed the creation of 996.ICU in March"                }            },            {                "_index": "test",                "_type": "test1",                "_id": "3",                "_score": 1,                "_source": {                    "title": "The 996.ICU page was soon blocked on multiple platforms including the messaging tool WeChat and the UC Browser."                }            }        ]    }}

 其中,took表示es检索所用的时间,单位是毫秒,time_out表示是否超时,_shards:表示我们搜索了多少切片,以及成功/失败搜索碎片的个数。hits表示搜索的结果。

通过request body检索

GET /bank/_search{  "query": { "match_all": {} },  "sort": [    { "account_number": "asc" }  ]}

 说明,在url中的参数全部转化为json数据格式,然后进行请求。request中的请求参数设置可以参考。对于使用requestbody进行检索,由于并不是所有的客户端都支持get请求中设置body,因此,可以通过post请求设置body检索,get和post中的body相同。其他简单的查询如下

1、设置查询的数量

GET /bank/_search{  "query": { "match_all": {} },  "size": 1}

2、查询从第10个到第19个

GET /bank/_search{  "query": { "match_all": {} },  "from": 10,  "size": 10}

3、根据balance降序排序

GET /bank/_search{  "query": { "match_all": {} },  "sort": { "balance": { "order": "desc" } }}

4、使用source指定返回字段

GET /bank/_search{  "query": { "match_all": {} },  "_source": ["account_number", "balance"]}

5、使用match指定满足条件的字段的值,即返回account_number的值是20的值

GET /bank/_search{  "query": { "match": { "account_number": 20 } }}

返回address中包含mill的地址

GET /bank/_search{  "query": { "match": { "address": "mill" } }}

返回address中包含mill或者lane的地址

GET /bank/_search{  "query": { "match": { "address": "mill lane" } }}

6、使用bool进行查询,bool可以将简单的查询条件组合成复杂的查询条件

must表示两个都必须存在

GET /bank/_search{  "query": {    "bool": {      "must": [        { "match": { "address": "mill" } },        { "match": { "address": "lane" } }      ]    }  }}

should表示其中一个条件满足既可以

GET /bank/_search{  "query": {    "bool": {      "should": [        { "match": { "address": "mill" } },        { "match": { "address": "lane" } }      ]    }  }}

must_not表示两个条件都不满足

GET /bank/_search{  "query": {    "bool": {      "should": [        { "match": { "address": "mill" } },        { "match": { "address": "lane" } }      ]    }  }}

可以将条件进行组合,比如查询年龄必需是40,状态不是id的数据

GET /bank/_search{  "query": {    "bool": {      "must": [        { "match": { "age": "40" } }      ],      "must_not": [        { "match": { "state": "ID" } }      ]    }  }}

7、通过filter字段进行过滤,比如下面,返回balances在20000到30000之间的数据。

GET /bank/_search{  "query": {    "bool": {      "must": { "match_all": {} },      "filter": {        "range": {          "balance": {            "gte": 20000,            "lte": 30000          }        }      }    }  }}

8、使用aggs进行聚合

GET /bank/_search{  "size": 0,  "aggs": {    "group_by_state": {      "terms": {        "field": "state.keyword"      }    }  }}

参考资料

转载地址:http://ndktn.baihongyu.com/

你可能感兴趣的文章
ANSYS——命令流学习(材料属性设置、建模的命令流)
查看>>
ANSYS——杆单元简介与示例(含新版本2019版本杆实常数设置、ANSYS help的使用、单元列表使用的举例)
查看>>
ANSYS——后处理中单元表(ELEMENT table)的作用、创建、使用
查看>>
在VScode上配置golang的开发环境
查看>>
leetcode每日一题---680. 验证回文字符串 Ⅱ
查看>>
leetcode每日一题---15. 三数之和
查看>>
leetcode每日一题---面试题 16.18. 模式匹配
查看>>
地主的钱袋
查看>>
招新成绩统计
查看>>
webpack
查看>>
go部署
查看>>
配置swagger--go语言
查看>>
打印杨辉三角
查看>>
java中String类中常用方法
查看>>
flutter学习笔记:第一个APP应用
查看>>
哲学家进餐问题
查看>>
Python-Opencv学习总结(一):图像读取和获取图像特征
查看>>
实验十三:导出与导入
查看>>
第十五周.
查看>>
基于MVC模式的用户登录
查看>>