1,對那些有唱歌興趣的用戶按年齡進行分組,並求出每個組的年齡平均值,並按平均年齡的降序進行排序
GET /megacorp/employee
{ "size" : 0, "query" : { "match":{ "interests" :"changge" } }, "aggs" : { "age_of_group":{ "terms":{ "field":"age", "order":{ "age_of_avg":"desc" } }, "aggs":{ "age_of_avg":{ "avg":{ "field":"age" } } } } } }
2,term 查詢和terms查詢
term query 會去倒排索引中尋找確切的term,它並不知道分詞器的存在,這種查詢適合keyword,numeric,date的查詢
GET /megacorp/employee
{ "query" : { "term":{ "interests" :"changge" } } }
3,terms 查詢某個字段含有多個關鍵字的文檔
GET /megacorp/employee
{ "query" : { "terms":{ "interests" :["changge","hejiu","sport"] } } }
注意,分頁查詢是從0開始的,用的是from和size
match 查詢:先進行分詞,再查詢
GET /megacorp/employee
{
"query" : { "match":{ "name" :"changge" } } }
返回分數為1,則為精確查詢
multi_match可以匹配多個字段
GET /megacorp/employee
{ "query" : { "multi_match":{ "query" :"changge", "field" :["changge","sport"] } } }
短語匹配查詢,需要完全匹配這個短語
GET /megacorp/employee { "query" : { "match_phrase":{ "interests" :["changge,sport"] } } }
排序,sort節點和query節點平級
GET /megacorp/employee { "query" : { "match_all":{} }, "sort" : [ { "age":{ "order":"desc" } } ] }
范圍查詢
GET /megacorp/employee { "query" : { "range":{ "birthday":{ "from":"1990-10-10", "to":"2020-01-12" } } } } 是否包含邊界值,包含下界不包含上屆 { "query" : { "range":{ "age":{ "from":20, "to":26, "include_lower":true, "include_upper":false } } } }
wildcard 查詢,是通配符查詢,*表示0個或多個,? 表示1個字符
GET /megacorp/employee { "query" : { "wildcard":{ "name":"zhao*" } } } { "query" : { "wildcard":{ "name":"li?a" } } }
fuzzy 實現模糊查詢,change 也能被找出來
GET /megacorp/employee { "query" : { "fuzzy":{ "interests":"chonge" } } }
高亮顯示
GET /megacorp/employee { "query" : { "match":{ "interests":"change" } }, "highlight" : { "fields":{ "interests":{} } } }