对es来说,有两种基本的检索方式,一种是通过URI传递检索参数,另一种是通过request body检索。通过request body进行检索,可以在json结构中传递更多信息,同时增强了可读性。
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表示搜索的结果。
GET /bank/_search
{
"query": { "match_all": {} },
"sort": [
{ "account_number": "asc" }
]
}
说明,在url中的参数全部转化为json数据格式,然后进行请求。request中的请求参数设置可以参考Request Body Search。对于使用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"
}
}
}
}
es官方教程