[b]搜索API[/b]
GET /bank/_search?q=*&sort=account_number:asc&pretty
rest方式搜索bank索引下的所有数据,并且按照account_number 上升方式返回
类似下面的搜索方式
GET /bank/_search
body为:
{
"query": { "match_all": {} },
"sort": [
{ "account_number": "asc" }
]
}
[b]初步学习搜索语言[/b]
下面的搜索全部文档,按照balance降序排列,并返回11到20的文档,from的位置开始,size个文档
GET /bank/_search
{
"query": { "match_all": {} },
"sort": { "balance": { "order": "desc" } }
"from": 10,
"size": 10
}
[b]执行搜索[/b]
当不需要所有字段都返回的时候可以使用如下方式_source,定义了只返回两个字段
GET /bank/_search
{
"query": { "match_all": {} },
"_source": ["account_number", "balance"]
}
如果我们希望根据某个条件进行搜索可以使用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" } }
}
如下搜索address包含短语“mill lane”的文档
GET /bank/_search
{
"query": { "match_phrase": { "address": "mill lane" } }
}
bool 搜索可以组合小的搜索到一个大搜索中
如下搜索address包含mill和lane的文档
GET /bank/_search
{
"query": {
"bool": {
"must": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}
(must代表必须满足的意思)
相反,如下搜索address包含mill或者lane的文档
GET /bank/_search
{
"query": {
"bool": {
"should": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}
(should代表任何一个条件满足即可)
如下搜索address既不包括mill又不包括lane的文档
GET /bank/_search
{
"query": {
"bool": {
"must_not": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}
(must_not 代表排除的意思)
以上的三种类型可以合并使用
如下搜索age为40且state不是ID的文档
GET /bank/_search
{
"query": {
"bool": {
"must": [
{ "match": { "age": "40" } }
],
"must_not": [
{ "match": { "state": "ID" } }
]
}
}
}
[b]执行过滤[/b]
filter执行过滤(其实就是对非string的进行区间搜索过滤)
如果搜索20000<=blance && blance<=30000的文档
GET /bank/_search
{
"query": {
"bool": {
"must": { "match_all": {} },
"filter": {
"range": {
"balance": {
"gte": 20000,
"lte": 30000
}
}
}
}
}
}
[b]执行聚合[/b]
如下根据字段state聚合查找每种类型的数量(默认按照降序排列,返回前10条记录类似sql:SELECT state, COUNT(*) FROM bank GROUP BY state ORDER BY COUNT(*) DESC)
GET /bank/_search
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state"
}
}
}
}
(注意:设置size为0是因为我们不想看到hit的信息)
如下在上面的基础上,我们想要知道blance的平均值
GET /bank/_search
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state"
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}
在上面的基础上,我们希望按照平均值降序排列
GET /bank/_search
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state",
"order": {
"average_balance": "desc"
}
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}
如下我们先根据年龄,然后根据性别,然后求出balance的平均值
GET /bank/_search
{
"size": 0,
"aggs": {
"group_by_age": {
"range": {
"field": "age",
"ranges": [
{
"from": 20,
"to": 30
},
{
"from": 30,
"to": 40
},
{
"from": 40,
"to": 50
}
]
},
"aggs": {
"group_by_gender": {
"terms": {
"field": "gender.keyword"
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}
}
}
GET /bank/_search?q=*&sort=account_number:asc&pretty
rest方式搜索bank索引下的所有数据,并且按照account_number 上升方式返回
类似下面的搜索方式
GET /bank/_search
body为:
{
"query": { "match_all": {} },
"sort": [
{ "account_number": "asc" }
]
}
[b]初步学习搜索语言[/b]
下面的搜索全部文档,按照balance降序排列,并返回11到20的文档,from的位置开始,size个文档
GET /bank/_search
{
"query": { "match_all": {} },
"sort": { "balance": { "order": "desc" } }
"from": 10,
"size": 10
}
[b]执行搜索[/b]
当不需要所有字段都返回的时候可以使用如下方式_source,定义了只返回两个字段
GET /bank/_search
{
"query": { "match_all": {} },
"_source": ["account_number", "balance"]
}
如果我们希望根据某个条件进行搜索可以使用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" } }
}
如下搜索address包含短语“mill lane”的文档
GET /bank/_search
{
"query": { "match_phrase": { "address": "mill lane" } }
}
bool 搜索可以组合小的搜索到一个大搜索中
如下搜索address包含mill和lane的文档
GET /bank/_search
{
"query": {
"bool": {
"must": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}
(must代表必须满足的意思)
相反,如下搜索address包含mill或者lane的文档
GET /bank/_search
{
"query": {
"bool": {
"should": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}
(should代表任何一个条件满足即可)
如下搜索address既不包括mill又不包括lane的文档
GET /bank/_search
{
"query": {
"bool": {
"must_not": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}
(must_not 代表排除的意思)
以上的三种类型可以合并使用
如下搜索age为40且state不是ID的文档
GET /bank/_search
{
"query": {
"bool": {
"must": [
{ "match": { "age": "40" } }
],
"must_not": [
{ "match": { "state": "ID" } }
]
}
}
}
[b]执行过滤[/b]
filter执行过滤(其实就是对非string的进行区间搜索过滤)
如果搜索20000<=blance && blance<=30000的文档
GET /bank/_search
{
"query": {
"bool": {
"must": { "match_all": {} },
"filter": {
"range": {
"balance": {
"gte": 20000,
"lte": 30000
}
}
}
}
}
}
[b]执行聚合[/b]
如下根据字段state聚合查找每种类型的数量(默认按照降序排列,返回前10条记录类似sql:SELECT state, COUNT(*) FROM bank GROUP BY state ORDER BY COUNT(*) DESC)
GET /bank/_search
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state"
}
}
}
}
(注意:设置size为0是因为我们不想看到hit的信息)
如下在上面的基础上,我们想要知道blance的平均值
GET /bank/_search
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state"
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}
在上面的基础上,我们希望按照平均值降序排列
GET /bank/_search
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state",
"order": {
"average_balance": "desc"
}
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}
如下我们先根据年龄,然后根据性别,然后求出balance的平均值
GET /bank/_search
{
"size": 0,
"aggs": {
"group_by_age": {
"range": {
"field": "age",
"ranges": [
{
"from": 20,
"to": 30
},
{
"from": 30,
"to": 40
},
{
"from": 40,
"to": 50
}
]
},
"aggs": {
"group_by_gender": {
"terms": {
"field": "gender.keyword"
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}
}
}