本篇內(nèi)容主要講解“elasticsearch語法有哪些”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“elasticsearch語法有哪些”吧!
創(chuàng)新新互聯(lián),憑借10余年的做網(wǎng)站、網(wǎng)站建設(shè)經(jīng)驗(yàn),本著真心·誠心服務(wù)的企業(yè)理念服務(wù)于成都中小企業(yè)設(shè)計(jì)網(wǎng)站有上千多家案例。做網(wǎng)站建設(shè),選創(chuàng)新互聯(lián)。
語法
/ / /
命令
查詢所有索引 | GET /_cat/indices GET /_cat/indices?v |
創(chuàng)建索引 | PUT /user |
刪除索引 | DELETE /user |
添加文檔 | |
刪除文檔 | |
查詢文檔 | GET /user/_search |
name字段包含“張三”或“李四”的 | GET /user/_search |
匹配所有文檔 | GET /user/_search { "query": { "match_all":{} } } |
所有name字段,包含“張三”的 | GET /user/_search { "query": { "match_phrase":{"name": "張三"} } } |
bool查詢 所有數(shù)據(jù)中,name字段同時(shí) 包含”張三“、”李四“的 | GET /user/_search { "query": { "bool": { "must": [ {"match": {"name": "張三"}}, {"match": {"name": "李四"}}, ], } } }
|
bool查詢 所有數(shù)據(jù)中,name字段 包含”張三“或”李四“的 | GET /user/_search { "query": { "bool": { "should": [ {"match": {"name": "張三"}}, {"match": {"name": "李四"}}, ], } } }
|
bool查詢 所有數(shù)據(jù)中,name字段同時(shí) 不包含”張三“也不包含”李四“的 | GET /user/_search { "query": { "bool": { "must_not": [ {"match": {"name": "張三"}}, {"match": {"name": "李四"}}, ], } } }
|
bool查詢 所有數(shù)據(jù)中,name字段 包含”張三“,但不包含”李四“的 | GET /user/_search { "query": { "bool": { "must": [ {"match": {"name": "張三"}}, ], "must_not": [ {"match": {"name": "李四"}}, ], } } } |
過濾查詢 所有數(shù)據(jù)中,10<=age<=20的 | GET /user/_search { "query": { "bool": { "must": [ {"match_all": {}}, ], "filter": { "range": {"age": {"gte":10, "lte": 20}} } } } } |
聚合查詢 根據(jù)state分組,倒序,統(tǒng)計(jì)state、count(id) 等同于: SELECT state, COUNT(*) FROM bank GROUP BY state ORDER BY COUNT(*) DESC 說明: size=0 是為了不要查詢文檔,只要聚合結(jié)果 | GET /user/_search |
聚合查詢 根據(jù)state分組,倒序,統(tǒng)計(jì)state、 count(id)、avg(balance) 等同于: SELECT state, COUNT(*), avg(balance) FROM bank GROUP BY state ORDER BY COUNT(*) DESC | GET /user/_search { "size": 0, "aggs": { "group_by_state": { "terms": { "field": "state.keyword" }, "aggs": { "average_balance": { "avg": { "field": "balance" } } } } } } |
聚合查詢 根據(jù)state分組,balance平均值倒序,統(tǒng)計(jì)state、 count(id)、avg(balance) 等同于: SELECT state, COUNT(*), avg(balance) FROM bank GROUP BY state ORDER BY avg(balance) DESC | GET /user/_search { "size": 0, "aggs": { "group_by_state": { "terms": { "field": "state.keyword", "order": { "average_balance": "desc" } }, "aggs": { "average_balance": { "avg": { "field": "balance" } } } } } } |
聚合查詢 根據(jù)age年齡段分組,然后按照性別分組, 然后獲取balance平均值 | GET /user/_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" } } } } } } } } |
到此,相信大家對(duì)“elasticsearch語法有哪些”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!