本文基於ES6.4版本,我也是出於學習階段,對學習內容作個記錄,若是文中有錯誤,請指出。緩存
實驗數據:
index:bookapp
type:novelide
mappings:函數


{ "mappings": { "novel": { "dynamic": "false", "properties": { "word_count": { "type": "integer" }, "author": { "type": "keyword" }, "title": { "type": "text" }, "publish_date": { "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis", "type": "date" } } } } }
經過put建立索引,使用head可視化界面,數據以下:學習
Elasticsearch的查詢分爲:spa
一、子條件查詢:查詢特定字段的特定值code
Query context
查詢過程當中,除了判斷Document是否知足條件,還會計算出_score表示匹配程度,數值越大,證實匹配程度越高orm
一、查詢所有:/book/novel/_searchblog


"hits": { "total": 10, "max_score": 1.0, "hits": [ { "_index": "book", "_type": "novel", "_id": "5", "_score": 1.0, "_source": { "title": "永夜君王", "word_count": "110000", "publish_date": "2015-03-01", "author": "煙雨江南" } }, { "_index": "book", "_type": "novel", "_id": "8", "_score": 1.0, "_source": { "title": "萬古令", "word_count": "110000", "publish_date": "2015-03-01", "author": "聽奕" } }, { "_index": "book", "_type": "novel", "_id": "9", "_score": 1.0, "_source": { "title": "天帝傳", "word_count": "110000", "publish_date": "2015-03-01", "author": "飛天魚" } }, { "_index": "book", "_type": "novel", "_id": "10", "_score": 1.0, "_source": { "title": "劍來", "word_count": "110000", "publish_date": "2015-03-01", "author": "烽火戲諸侯" } }, { "_index": "book", "_type": "novel", "_id": "2", "_score": 1.0, "_source": { "title": "完美世界", "word_count": "130000", "publish_date": "2017-03-01", "author": "辰東" } }, { "_index": "book", "_type": "novel", "_id": "4", "_score": 1.0, "_source": { "title": "民國諜影", "word_count": "110000", "publish_date": "2019-03-01", "author": "尋青藤" } }, { "_index": "book", "_type": "novel", "_id": "6", "_score": 1.0, "_source": { "title": "遮天", "word_count": "110000", "publish_date": "2015-03-01", "author": "辰東" } }, { "_index": "book", "_type": "novel", "_id": "1", "_score": 1.0, "_source": { "title": "萬古神帝", "word_count": "30000", "publish_date": "2017-01-01", "author": "飛天魚" } }, { "_index": "book", "_type": "novel", "_id": "7", "_score": 1.0, "_source": { "title": "聖墟", "word_count": "110000", "publish_date": "2015-03-01", "author": "辰東" } }, { "_index": "book", "_type": "novel", "_id": "3", "_score": 1.0, "_source": { "title": "星辰變", "word_count": "100000", "publish_date": "2018-03-01", "author": "我吃西紅柿" } } ] }
二、查詢id爲1的數據:/book/novel/1排序


{ "_index": "book", "_type": "novel", "_id": "1", "_version": 1, "found": true, "_source": { "title": "萬古神帝", "word_count": "30000", "publish_date": "2017-01-01", "author": "飛天魚" } }
三、只查詢title和author字段:/1?_source=title,author


{ "_index": "book", "_type": "novel", "_id": "1", "_version": 1, "found": true, "_source": { "author": "飛天魚", "title": "萬古神帝" } }
四、只是顯示_source部分:/book/novel/1/_source


{ "title": "萬古神帝", "word_count": "30000", "publish_date": "2017-01-01", "author": "飛天魚" }
五、篩選單字段查詢:/book/novel/_search
{ "query": { "match": { "author": "飛天魚" } } }


"hits": { "total": 2, "max_score": 1.2039728, "hits": [ { "_index": "book", "_type": "novel", "_id": "9", "_score": 1.2039728, "_source": { "title": "天帝傳", "word_count": "110000", "publish_date": "2015-03-01", "author": "飛天魚" } }, { "_index": "book", "_type": "novel", "_id": "1", "_score": 0.6931472, "_source": { "title": "萬古神帝", "word_count": "30000", "publish_date": "2017-01-01", "author": "飛天魚" } } ] }
六、limit:咱們查詢到2條數據,若是咱們只想獲得第一條數據,能夠使用from和size聯合查詢
{ "query": { "match": { "author": "飛天魚" } }, "from": 0, "size": 1 }


"hits": { "total": 2, "max_score": 1.2039728, "hits": [ { "_index": "book", "_type": "novel", "_id": "9", "_score": 1.2039728, "_source": { "title": "天帝傳", "word_count": "110000", "publish_date": "2015-03-01", "author": "飛天魚" } } ] }
{ "query": { "match": { "author": "辰東" } }, "sort": [ { "word_count": { "order": "desc" } } ] }


"hits": { "total": 3, "max_score": null, "hits": [ { "_index": "book", "_type": "novel", "_id": "2", "_score": null, "_source": { "title": "完美世界", "word_count": "130000", "publish_date": "2017-03-01", "author": "辰東" }, "sort": [ 130000 ] }, { "_index": "book", "_type": "novel", "_id": "6", "_score": null, "_source": { "title": "遮天", "word_count": "110000", "publish_date": "2015-03-01", "author": "辰東" }, "sort": [ 110000 ] }, { "_index": "book", "_type": "novel", "_id": "7", "_score": null, "_source": { "title": "聖墟", "word_count": "110000", "publish_date": "2015-03-01", "author": "辰東" }, "sort": [ 110000 ] } ] }
八、其他匹配match_phrase:
query、match的方式本質上就是模糊查詢,並且中文會自動分詞到最大粒度,能夠看到會查詢到只要匹配任意一個字都是能夠的
{ "query": { "match": { "title": "萬古神帝" } } }


"hits": { "total": 3, "max_score": 2.439878, "hits": [ { "_index": "book", "_type": "novel", "_id": "1", "_score": 2.439878, "_source": { "title": "萬古神帝", "word_count": "30000", "publish_date": "2017-01-01", "author": "飛天魚" } }, { "_index": "book", "_type": "novel", "_id": "8", "_score": 2.4079456, "_source": { "title": "萬古令", "word_count": "110000", "publish_date": "2015-03-01", "author": "聽奕" } }, { "_index": "book", "_type": "novel", "_id": "9", "_score": 1.2039728, "_source": { "title": "天帝傳", "word_count": "110000", "publish_date": "2015-03-01", "author": "飛天魚" } } ] }
因此這裏有了其他匹配match_phrase,結果只有徹底包含"萬古神帝"的title才能夠被查詢到
{ "query": { "match_phrase": { "title": "萬古神帝" } } }


"hits": { "total": 1, "max_score": 2.439878, "hits": [ { "_index": "book", "_type": "novel", "_id": "1", "_score": 2.439878, "_source": { "title": "萬古神帝", "word_count": "30000", "publish_date": "2017-01-01", "author": "飛天魚" } } ] }
九、多條件查詢multi_match:查詢title或者author包含"萬古神帝"的數據
{ "query": { "multi_match": { "query": "萬古神天", "fields": ["title","author"] } } }


"hits": { "total": 4, "max_score": 2.4079456, "hits": [ { "_index": "book", "_type": "novel", "_id": "8", "_score": 2.4079456, "_source": { "title": "萬古令", "word_count": "110000", "publish_date": "2015-03-01", "author": "聽奕" } }, { "_index": "book", "_type": "novel", "_id": "1", "_score": 1.8299085, "_source": { "title": "萬古神帝", "word_count": "30000", "publish_date": "2017-01-01", "author": "飛天魚" } }, { "_index": "book", "_type": "novel", "_id": "9", "_score": 1.2039728, "_source": { "title": "天帝傳", "word_count": "110000", "publish_date": "2015-03-01", "author": "飛天魚" } }, { "_index": "book", "_type": "novel", "_id": "6", "_score": 1.1727304, "_source": { "title": "遮天", "word_count": "110000", "publish_date": "2015-03-01", "author": "辰東" } } ] }
十、語法查詢query_string:
{ "query": { "query_string": { "query": "萬古" } } }
這裏和match沒有區別,query能夠使用AND和OR,match的filed也能夠,注意這裏必定是大寫,小寫就被當作搜索的內容了
{ "query": { "query_string": { "query": "萬古 OR 劍來" } } }
{ "query": { "match": { "title": "萬古 OR 劍來" } } }
指定fields:
{ "query": { "query_string": { "query": "萬古 OR 劍來 OR 辰東 ", "fields": ["author","title"] } } }
十一、精確匹配term:
title爲text類型,author爲keyword類型,實驗發現查詢title只有是單個字的時候才能匹配(精確匹配查不到數據),而author必須是精確匹配
例如:title不支持精確匹配,支持模糊查詢(並且是單個字才能夠,多個字照樣查不到數據)
{ "query": { "term": { "title": "劍來" } } }
若是隻是查詢一個字就能夠
{ "query": { "term": { "title": "來" } } }


"hits": { "total": 1, "max_score": 1.3940737, "hits": [ { "_index": "book", "_type": "novel", "_id": "10", "_score": 1.3940737, "_source": { "title": "劍來", "word_count": "110000", "publish_date": "2015-03-01", "author": "烽火戲諸侯" } } ] }
查詢author字段:有三條數據
{ "query": { "term": { "author": "辰東" } } }


"hits": [ { "_index": "book", "_type": "novel", "_id": "7", "_score": 0.6931472, "_source": { "title": "聖墟", "word_count": "110000", "publish_date": "2015-03-01", "author": "辰東" } }, { "_index": "book", "_type": "novel", "_id": "2", "_score": 0.47000363, "_source": { "title": "完美世界", "word_count": "130000", "publish_date": "2017-03-01", "author": "辰東" } }, { "_index": "book", "_type": "novel", "_id": "6", "_score": 0.47000363, "_source": { "title": "遮天", "word_count": "110000", "publish_date": "2015-03-01", "author": "辰東" } } ] }
author不知道模糊查詢:下面結果爲null
{ "query": { "term": { "author": "東" } } }
十二、範圍查找range:包括integer和日期類型,日期支持now函數,也就是當前日期
{ "query": { "range": { "word_count": { "gt": 110000, "lte": 130000 } } } }


"hits": { "total": 1, "max_score": 1.0, "hits": [ { "_index": "book", "_type": "novel", "_id": "2", "_score": 1.0, "_source": { "title": "完美世界", "word_count": "130000", "publish_date": "2017-03-01", "author": "辰東" } } ] }
Filter context
查詢過程當中,只是判斷Document是否知足條件,只有yes or no。用來作數據過濾,並且ES還會對結果進行緩存,效率相對query更高一點
{ "query": { "bool": { "filter": { "term": { "word_count": 130000 } } } } }


"hits": { "total": 1, "max_score": 0.0, "hits": [ { "_index": "book", "_type": "novel", "_id": "2", "_score": 0.0, "_source": { "title": "完美世界", "word_count": "130000", "publish_date": "2017-03-01", "author": "辰東" } } ] }
二、複合條件查詢:組合子條件查詢
一、固定分數查詢:不支持match,支持filter
{ "query": { "constant_score": { "filter": { "match": { "title": "天帝傳" } } } } } { "query": { "constant_score": { "filter": { "match": { "title": "天帝傳" } }, "boost": 2 } } }
二、bool查詢:
should:就是or的關係
{ "query": { "bool": { "should": [ { "match": { "author": "辰東" } }, { "match": { "title": "天帝傳" } } ] } } }
must:至關於and
{ "query": { "bool": { "must": [ { "match": { "author": "辰東" } }, { "match": { "title": "天帝傳" } } ] } } }
must_not:至關於<>
{ "query": { "bool": { "must_not": { "term": { "author": "辰東" } } } } }
bool查詢也能夠使用filter:
{ "query": { "bool": { "must": [ { "match": { "author": "辰東" } }, { "match": { "title": "天帝傳" } } ], "filter": [ { "term": { "word_count": 110000 } } ] } } }
aggregations:
{ "aggs": { "group_by_author": { "terms": { "field": "author" } } } }


"aggregations": { "group_by_author": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "辰東", "doc_count": 3 }, { "key": "飛天魚", "doc_count": 2 }, { "key": "聽奕", "doc_count": 1 }, { "key": "尋青藤", "doc_count": 1 }, { "key": "我吃西紅柿", "doc_count": 1 }, { "key": "煙雨江南", "doc_count": 1 }, { "key": "烽火戲諸侯", "doc_count": 1 } ] } }
支持多聚合結果:
{ "aggs": { "group_by_author": { "terms": { "field": "author" } }, "group_by_word_count": { "terms": { "field": "word_count" } } } }
aggregations除了支持term,還有stats、min、max、avg等
{ "aggs": { "group_by_author": { "stats": { "field": "word_count" } } } }
"aggregations": { "group_by_author": { "count": 10, "min": 30000.0, "max": 130000.0, "avg": 103000.0, "sum": 1030000.0 } }
avg:
{ "aggs": { "group_by_author": { "avg": { "field": "word_count" } } } }