進入Kibana的控制台:http://localhost:5601/app/kibana#/dev_tools/
先放一些測試數據進去,不想一條一條,就用bulk
注意格式

正確格式:

解釋:ES期望的格式是:一行action(index, create, delete and update),一行source(也就是具體數據),這期間不能有多余的換行符,也就是說不要為了美觀而把JSON格式化。
所以我插入的批量數據:執行即可
POST _bulk { "index" : { "_index" : "assay", "_type" : "article", "_id" : "1" }} {"title" : "The fox and the lion 狐狸與獅子","post_date" : "2018-08-22T11:33:00+0800","content" : "When the fox first saw the lion he was terribly frightened. He ran away, and hid himself in the woods. The second time, however, he came near the lion. He stopped at a safe distance, and watched him pass by. The third time they came near one another.The fox went straight up to the lion, and stayed the whole day with him. He asked the lion how his family was, and when they would meet again."} { "index" : { "_index" : "assay", "_type" : "article", "_id" : "2" }} {"title" : "A Child's Dream of a Star","post_date" : "2018-08-22T11:34:00+0800","content" : "There was once a child, and he strolled about a good deal, and thought of a number of things. He had a sister, who was a child too, and his constant companion. These two used to wonder all day long. They wondered at the beauty of the flowers; they wondered at the height and blueness of the sky; they wondered at the depth of the bright water; they wondered at the goodness and the power of God who made the lovely world."} { "index" : { "_index" : "assay", "_type" : "article", "_id" : "3" }} {"title" : "The goose with the golden eggs","post_date" : "2018-08-22T11:35:00+0800","content" : "One morning a countryman went to his goose's nest, and saw a yellow and glittering1 egg there.He took the egg home. To his delight, he found that it was an egg of pure gold.Every morning the same thing occurred, and he soon became rich by selling his eggs. The countryman became more and more greedy. He wanted to get all the gold at once, so he killed the goose, when he looked inside, he found nothing in its body."} { "index" : { "_index" : "assay", "_type" : "article", "_id" : "4" }} {"title" : "The Man Selling the Idol","post_date" : "2018-08-22T11:36:00+0800","content" : "A man carves an idol1 and takes it to the fair. No one buys it, so he begins to shout in order to canvass2 the customer. He says that this idol can bring in wealth and good luck. One man says to the seller, ‘Hello, My friend, if this is so, you should have the advantages that the idol can bring, why do you want to sell it? The seller says’, ‘What I want is that I can get cash in at once. The profit from the idol is so slow.’"} { "index" : { "_index" : "assay", "_type" : "article", "_id" : "5" }} {"title" : "The Neighbour and the Snake","post_date" : "2018-08-23T11:33:00+0800","content" : "A snake, having made his hole close to the door of a cottage, inflicted1 a sever2 bite on the cottager's little son. So the child died.This caused much sorrow to his parents. The father decided3 to kill the snake. The next day, on its coming out of its hole for food, he took up his ax, but, making too much haste to hit the snake, missed its head, and cut off only the end of its tail.After some time the cottager, lest the snake should also bite him, tried to make peace, and placed some bread and salt beside its hole, the snake, slightly hissing4, said, ‘From now on there can be no peace between us; for whenever I see you I shall remember the loss of my tail, and whenever you see me you will be thinking of the death of your son.’"} { "index" : { "_index" : "assay", "_type" : "article", "_id" : "6" }} {"title" : "The ass and a lap-dog","post_date" : "2018-08-24T11:33:00+0800","content" : "A man had an ass1 and lovely lap-dog.The ass was left in a stable, and had plenty of oats and day to eat, just as any other ass would. The lap-dog knew many tricks, and was a great favourite with his master.The master seldom went out to dine or to super without bringing him something nice to eat when he jumped about him in a pleasant manner. The ass, on the contrary, had much work to do, in grinding the corn-mill and in carrying wood from the forest or burdens from the farm,. He often co plained about his own hard fate, and contrasted it with the luxury and idleness of the lap-dog. At last one day he broke the chain and ran into his master's house, kicking up his heels without measuring and jumping as well as he could, he next tried to jump about his master as he had seen the lap-dog do, but he broke the table, and smashed all the dishes on it to pieces. He then tried to lick his master, and jumped upon his back."}
執行結果

接下來先去Management創建Index Pattern,然后去Discover查看一下數據,我們就可以步入正題了

正題:ES的查詢
# 搜索特定索引 GET /assay/_search?q=title:dog # 搜索索引下面的多個類型 GET /assay/article,book/_search?q=title:dog # 搜索多個索引 GET /assay,twitter/_search?q=message:Elasticsearch # 搜索所有索引 GET /_all/_search?q=out # 搜索所有索引所有類型 GET /_search?q=jumped
帶有請求體的搜索
# 搜索title字段含有dog的記錄 GET /assay/_search { "query" : { "term" : { "title" : "dog" } } }
分頁查詢
# 分頁查詢,from代表偏移量,size代表返回數量 GET /_search { "from" : 0, "size" : 3, "query" : { "term" : { "content" : "the" } } }
注意,默認情況,from+size不能超過index.max_result_window的值(默認10000)
排序
按照post_time排序(asc升序,desc降序)
GET /assay/_search { "sort" : [ { "post_date" : {"order" : "desc"}}, "_score" ], "query" : { "term" : { "content" : "the" } } }
按照mode排序(min-最小值,max-最大值,sum-總和,avg-平均值,median-中位數)
POST _bulk { "index" : { "_index" : "goods", "_type" : "sears", "_id" : "1" }} {"name" : "crab-螃蟹","price" : [129,144,139],"post_date" : "2018-08-23T15:12:12+0800"} { "index" : { "_index" : "goods", "_type" : "sears", "_id" : "2" }} {"name" : "lobster-龍蝦","price" : [105,118,125],"post_date" : "2018-08-23T15:12:12+0800"} { "index" : { "_index" : "goods", "_type" : "sears", "_id" : "3" }} {"name" : "abalone-鮑魚","price" : [99,159,88],"post_date" : "2018-08-23T15:12:12+0800"} GET /goods/_search { "sort" : [ { "price" : {"order" : "desc", "mode" : "avg"}} ] }
這里我新加了幾條數據,記得去Management里面配置好索引,然后查詢,這個是按照價格的平均值降序排序的
返回數據列過濾
1. 不顯示原始數據
GET /goods/_search { "_source": false, "sort" : [ { "price" : {"order" : "desc", "mode" : "avg"}} ] }
2.顯示部分
GET /goods/_search { "_source": ["name","price"], "sort" : [ { "price" : {"order" : "desc", "mode" : "avg"}} ] }
3.包含&排除(更精確的控制)
GET /goods/_search { "_source": { "includes": ["name"], "excludes": "post_date" }, "sort" : [ { "price" : {"order" : "desc", "mode" : "avg"}} ] }
腳本
GET /goods/_search { "query" : { "match_all": {} }, "script_fields": { "test1": { "script" : { "lang": "painless", "source": "doc['price'].value * 2" } }, "test2":{ "script" : { "lang": "painless", "source": "doc['price'].value * params.factor", "params" : { "factor" : 5.0 } } } } }
結果:

..
GET /goods/_search { "query" : { "match_all": {} }, "script_fields": { "test1": { "script": "params['_source']['name']" } } }
結果:

注意:doc[''] 和 params['_source']都能獲取文檔字段,不同的是doc相對來說快一些,但是會占用更多的內存,但是總體來說推薦用doc
Post Filter
在聚合計算之后,post_filter用於搜索請求末尾的搜索匹配
先初始化一些數據
PUT /clothes { "mappings": { "fashion": { "properties": { "brand": { "type": "keyword"}, "color": { "type": "keyword"}, "model": { "type": "keyword"} } } } } POST _bulk { "index" : { "_index" : "clothes", "_type" : "fashion", "_id" : "1" }} {"brand" : "ANTA","color" : "red", "model": "S"} { "index" : { "_index" : "clothes", "_type" : "fashion", "_id" : "2" }} {"brand" : "ANTA","color" : "blue", "model": "M"} { "index" : { "_index" : "clothes", "_type" : "fashion", "_id" : "3" }} {"brand" : "ANTA","color" : "black", "model": "L"} { "index" : { "_index" : "clothes", "_type" : "fashion", "_id" : "4" }} {"brand" : "YISHION","color" : "red", "model": "S"} { "index" : { "_index" : "clothes", "_type" : "fashion", "_id" : "5" }} {"brand" : "YISHION","color" : "blue", "model": "M"} { "index" : { "_index" : "clothes", "_type" : "fashion", "_id" : "6" }} {"brand" : "YISHION","color" : "black", "model": "L"}
你可以查詢以純的紅色衣服
GET /clothes/_search { "query": { "bool": { "filter": [ { "term": { "color": "red" }}, { "term": { "brand": "YISHION" }} ] } } }
結果:

你可以
GET /clothes/_search { "query": { "bool": { "filter": [ { "term": { "brand": "YISHION" }} ] } }, "aggs": { "models": { "terms": { "field": "model" } } } }
結果:
{ "took": 1, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 3, "max_score": 0, "hits": [ { "_index": "clothes", "_type": "fashion", "_id": "5", "_score": 0, "_source": { "brand": "YISHION", "color": "blue", "model": "M" } }, { "_index": "clothes", "_type": "fashion", "_id": "4", "_score": 0, "_source": { "brand": "YISHION", "color": "red", "model": "S" } }, { "_index": "clothes", "_type": "fashion", "_id": "6", "_score": 0, "_source": { "brand": "YISHION", "color": "black", "model": "L" } } ] }, "aggregations": { "models": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "L", "doc_count": 1 }, { "key": "M", "doc_count": 1 }, { "key": "S", "doc_count": 1 } ] } } }
post_filter
GET /clothes/_search { "query": { "bool": { "filter": { "term": { "brand": "YISHION" } } } }, "aggs": { "colors": { "terms": { "field": "color" } }, "color_red": { "filter": { "term": { "color": "red" } }, "aggs": { "models": { "terms": { "field": "model" } } } } }, "post_filter": { "term": { "color": "red" } } }
結果:
{ "took": 11, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 0, "hits": [ { "_index": "clothes", "_type": "fashion", "_id": "4", "_score": 0, "_source": { "brand": "YISHION", "color": "red", "model": "S" } } ] }, "aggregations": { "color_red": { "doc_count": 1, "models": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "S", "doc_count": 1 } ] } }, "colors": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "black", "doc_count": 1 }, { "key": "blue", "doc_count": 1 }, { "key": "red", "doc_count": 1 } ] } } }
