加載樣本數(shù)據(jù)
curl -u elastic:changeme -XPOST 'localhost:9200/bank/account/_bulk?pretty&refresh' --data-binary "@accounts.json"
使用Search API
兩種基本方式使用搜索:
- REST request URI
GET /bank/_search?q=*&sort=account_number:asc&pretty
- REST request body
GET /bank/_search
{
"query":{"match_all":{}},
"sort":[{
{"account_number":"asc"}
}]
}
返回值的含義:
took - elastic執(zhí)行搜索的時(shí)間(以毫秒為單位)
timed_out - 搜索是否超時(shí)
_shards - 搜索的分片數(shù)量邑滨,以及搜索成功/失敗的分片數(shù)量
hits - 搜索返回的結(jié)果
hits.total - 符合搜索條件的Document數(shù)量
hits.hits - 實(shí)施搜索結(jié)果的數(shù)組(默認(rèn)為前10個(gè)文檔)
hits.sort - 排序結(jié)果關(guān)鍵字(如果按照分?jǐn)?shù)排序谦絮,則不顯示)
查詢(xún)語(yǔ)言
elastic提供了一種用于執(zhí)行查詢(xún)的Json風(fēng)格的特定域的語(yǔ)言
GET /bank/_search
{
"query":{"match_all": {}}
}
query部分代表查詢(xún)定義,match_all部分代表要查詢(xún)的類(lèi)型
可以使用其他參數(shù)影響查詢(xún)結(jié)果款侵,例如只返回一條記錄
GET /bank/_search
{
"query":{"match_all": {}},
"size":1
}
返回11到20行的記錄
GET /bank/_search
{
"query":{"match_all": {}},
"from":10,
"size":10
}
from參數(shù)用于指定要從哪個(gè)document索引下表開(kāi)始,size參數(shù)指定from參數(shù)開(kāi)始返回多少個(gè)document
GET /bank/_search
{
"query": { "match_all": {} },
"sort": { "balance": { "order": "desc" } }
}
執(zhí)行搜索
返回document的字段
GET /bank/_search
{
"query": { "match_all": {} },
"_source": ["account_number", "balance"]
}
返回account_number為20的記錄
GET /bank/_search
{
"query": { "match": { "account_number": 20 } }
}
查詢(xún)返回地址是mill的記錄
GET /bank/_search
{
"query": { "match": { "address": "mill" } }
}
查詢(xún)返回地址是mill或lane的記錄
GET /bank/_search
{
"query": { "match": { "address": "mill lane" } }
}
查詢(xún)返回地址中包含mill lane的記錄
GET /bank/_search
{
"query": { "match_phrase": { "address": "mill lane" } }
}
bool查詢(xún)?cè)试S使用bool邏輯將更下的查詢(xún)組合成較大的查詢(xún)(組合條件查詢(xún))
GET /bank/_search
{
"query": {
"bool": {
"must": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}
GET /bank/_search
{
"query": {
"bool": {
"should": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}
GET /bank/_search
{
"query": {
"bool": {
"must_not": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}
GET /bank/_search
{
"query": {
"bool": {
"must": [
{ "match": { "age": "40" } }
],
"must_not": [
{ "match": { "state": "ID" } }
]
}
}
}
執(zhí)行過(guò)濾
score是數(shù)值類(lèi)型唠粥,代表文檔與搜索查詢(xún)匹配的相對(duì)度量蒜焊。分?jǐn)?shù)越高,文檔越相關(guān)照棋,分?jǐn)?shù)越低资溃,文檔的相關(guān)性就越低。
查詢(xún)并不是總產(chǎn)生分?jǐn)?shù)烈炭,特別是當(dāng)它們僅用于“過(guò)濾”文檔集合時(shí)溶锭。
GET /bank/_search
{
"query": {
"bool": {
"must": { "match_all": {} },
"filter": {
"range": {
"balance": {
"gte": 20000,
"lte": 30000
}
}
}
}
}
}
執(zhí)行聚合
聚合提供從數(shù)據(jù)中分組和提取統(tǒng)計(jì)信息的功能
GET /bank/_search
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state.keyword"
}
}
}
}
相當(dāng)于SQL語(yǔ)句為:SELECT state, COUNT() FROM bank GROUP BY state ORDER BY COUNT() DESC。
設(shè)置size參數(shù)為0是因?yàn)橐榭达@示聚合的結(jié)果符隙。
按照state字段分組趴捅,并計(jì)算balance的平均值
GET /bank/_search
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state.keyword"
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}
按照平均balacne進(jìn)行排序
GET /bank/_search
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state.keyword",
"order": {
"average_balance": "desc"
}
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}
按照年齡區(qū)間和性別分組,并計(jì)算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"
}
}
}
}
}
}
}
}