一、索引樣例數據
下載樣例數據集連接 下載後解壓到ES的bin目錄,而後加載到elasticsearch集羣html
curl -XPOST 127.0.0.1:9200/bank/account/_bulk?pretty --data-binary @accounts.jsongit
若是accounts.json文件和bin目錄並列:curl -XPOST 127.0.0.1:9200/bank/account/_bulk?pretty --data-binary @..\accounts.jsongithub
查看索引:curl localhost:9200/_cat/indices?vjson
上面結果,說明咱們成功bulk 1000個文檔到bank索引中了緩存
刪除索引bank:curl -XDELETE http://127.0.0.1:9200/bank網絡
二、搜索數據API
有兩種方式:一種方式是經過 REST 請求 URI ,發送搜索參數;另外一種是經過REST 請求體,發送搜索參數。而請求體容許你包含更容易表達和可閱讀的JSON格式。curl
2.一、經過 REST 請求 URI
curl localhost:9200/bank/_search?prettyelasticsearch
pretty,參數告訴elasticsearch,返回形式打印JSON結果ide
2.二、經過REST 請求體
curl -XPOST localhost:9200/bank/_search?pretty -d "{\"query\": {\"match_all\": {} }}"函數
query:告訴咱們定義查詢
match_all:運行簡單類型查詢指定索引中的全部文檔
除了指定查詢參數,還能夠指定其餘參數來影響最終的結果。
2.三、match_all & 只返回前兩個文檔:
curl -XPOST localhost:9200/bank/_search?pretty -d "{\"query\": {\"match_all\": {} }, \"size\" : 2}"
若是不指定size,默認是返回10條文檔信息
2.四、match_all & 返回第11到第20的10個文檔信息
curl -XPOST localhost:9200/bank/_search?pretty -d "{\"query\": {\"match_all\": {} }, \"from\" : 10, \"size\" : 10}"
from:指定文檔索引從哪裏開始,默認從0開始
size:從from開始,返回多個文檔
這feature在實現分頁查詢頗有用
2.五、match_all and 根據account 的balance字段 降序排序 & 返回10個文檔(默認10個)
curl -XPOST localhost:9200/bank/_search?pretty -d "{\"query\": {\"match_all\": {} }, \"sort\" : {\"balance\" : {\"order\" : \"desc\" }}}"
2.六、好比只返回account_number 和balance兩個字段
默認的,咱們搜索返回完整的JSON文檔。而source(_source字段搜索點擊量)。若是咱們不想返回完整的JSON文檔,咱們能夠使用source返回指定字段。
好比只返回account_number 和balance兩個字段
curl -XPOST localhost:9200/bank/_search?pretty -d "{\"query\": {\"match_all\": {} }, \"_source\": [\"account_number\", \"balance\"]}"
match 查詢,可做爲基本字段搜索查詢
2.七、返回 account_number=20:
curl -XPOST localhost:9200/bank/_search?pretty -d "{\"query\": {\"match\": {\"account_number\": 20 } }}"
2.八、返回 address=mill:
curl -XPOST localhost:9200/bank/_search?pretty -d "{\"query\": {\"match\": {\"address\": \"mill\" } }}"
2.九、返回 address=mill or address=lane:
curl -XPOST localhost:9200/bank/_search?pretty -d "{\"query\": {\"match\": {\"address\": \"mill lane\" } }}"
2.十、返回 短語匹配 address=mill lane:
curl -XPOST localhost:9200/bank/_search?pretty -d "{\"query\": {\"match_phrase\": {\"address\": \"mill lane\" } }}"
2.十一、布爾值(bool)查詢
返回 匹配address=mill & address=lane:
must:要求全部條件都要知足(相似於&&)
curl -XPOST localhost:9200/bank/_search?pretty -d "{\"query\": {\"bool\": {\"must\": [{\"match\": {\"address\": \"mill\" }},{\"match\": {\"address\": \"lane\" }}]}}}"
2.十二、返回 匹配address=mill or address=lane
curl -XPOST localhost:9200/bank/_search?pretty -d "{\"query\": {\"bool\": {\"should\": [{\"match\": {\"address\": \"mill\" }},{\"match\": {\"address\": \"lane\" }}]}}}"
should:任何一個知足就能夠(相似於||)
2.1三、返回 不匹配address=mill & address=lane
curl -XPOST localhost:9200/bank/_search?pretty -d "{\"query\": {\"bool\": {\"must_not\": [{\"match\": {\"address\": \"mill\" }},{\"match\": {\"address\": \"lane\" }}]}}}"
must_not:全部條件都不能知足(相似於! (&&))
2.1四、返回 age=40 & state!=ID
curl -XPOST localhost:9200/bank/_search?pretty -d "{\"query\": {\"bool\": {\"must\": [{\"match\": {\"address\": \"mill\" }}],\"must_not\": [{\"match\": {\"state\": \"ID\" }}]}}}"
三、執行過濾器
文檔中score(_score字段是搜索結果)。score是一個數字型的,是一種相對方法匹配查詢文檔結果。分數越高,搜索關鍵字與該文檔相關性越高;越低,搜索關鍵字與該文檔相關性越低。
在elasticsearch中全部的搜索都會觸發相關性分數計算。若是咱們不使用相關性分數計算,那要使用另外一種查詢能力,構建過濾器。
過濾器是相似於查詢的概念,除了得以優化,更快的執行速度的兩個主要緣由:
一、過濾器不計算得分,因此他們比執行查詢的速度
二、過濾器可緩存在內存中,容許重複搜索
爲了便於理解過濾器,先介紹過濾器搜索(like match_all, match, bool, etc.),能夠與其餘的普通查詢搜索組合一個過濾器。
range filter,容許咱們經過一個範圍值來過濾文檔,通常用於數字或日期過濾
使用過濾器搜索返回 balances[ 20000,30000]。換句話說,balance>=20000 && balance<=30000
POST /bank/_search?pretty { "query": { "bool": { "must": { "match": { "age": 39 }}, "must_not": { "match": { "employer":"Digitalus" }}, "filter": { "range": { "balance": { "gte": 20000, "lte": 30000 } } } } } }
三、執行聚合
聚合提供從你的數據中分組和提取統計能力, 相似於關係型數據中的SQL GROUP BY和SQL 聚合函數。
在Elasticsearch中,你有能力執行搜索返回命中結果,同時拆分命中結果,而後統一返回結果。當你使用簡單的API運行搜索和多個聚合,而後返回全部結果避免網絡帶寬過大的狀況是高效的。