啟動Elasticsearch
./bin/elasticsearch
以守護程式方式在後臺執行
./bin/elasticsearch -d
檢查Elasticsearch是否啟動成功
curl -XGET 'http://localhost:9200/' -H 'Content-Type: application/json'
建立叢集(多節點)
./bin/elasticsearch -d ./elasticsearch -Epath.data=data2 -Epath.logs=log2 -d ./elasticsearch -Epath.data=data3 -Epath.logs=log3 -d
啟動Kibana
./bin/kibana //壓縮包安裝 brew service start Kibaba //mac brew 安裝
使用 -i 顯示HTTP頭資訊
curl -i -XGET 'http://localhost:9200/' -H 'Content-Type: application/json'
建立索引
curl -X PUT "localhost:9200/megacorp/employee/1?pretty" -H 'Content-Type: application/json' -d' { "first_name" : "John", "last_name" : "Smith", "age" : 25, "about" : "I love to go rock climbing", "interests": [ "sports", "music" ] } //解釋: curl -X PUT "localhost:9200/索引名稱/型別名稱/特定僱員的ID?pretty" -H 'Content-Type: application/json' -d'
檢索文件
curl -X GET "localhost:9200/megacorp/employee/1?pretty"
刪除文件
curl -XDELETE "http://localhost:9200/megacorp/employee/1?pretty"
判斷文件是否存在
curl -XHEAD "http://localhost:9200/megacorp/employee/1?pretty" //成功返回200,失敗返回404
使用_search 搜尋所有文件
curl -XGET "http://localhost:9200/megacorp/employee/_search" //結果放在陣列hit之中
條件查詢,如查詢last_name=Smith的
curl -XGET "http://localhost:9200/megacorp/employee/_search?q=last_name:Smith&pretty"
使用DSL(領域特定語言)進行查詢(構造成JSON)
//放在query請求體中 curl -XGET "http://localhost:9200/megacorp/employee/_search?pretty" -H 'Content-Type: application/json' -d' { "query": { "match": { "last_name": "Smith" } } }'
使用過濾器
curl -XGET "http://localhost:9200/megacorp/employee/_search?pretty" -H 'Content-Type: application/json' -d' { "query": { "bool": { "must": { "match": { "last_name": "smith" } }, "filter": { "range": { "age": { "gt": 30 } } } } } }'
全文搜尋
GET /megacorp/employee/_search?pretty { "query": { "match": { "about": "rock climbing" } } } //返回結果中_score是相關性評分
短語搜尋(精確匹配詞語)
curl -XGET "http://localhost:9200/megacorp/employee/_search?pretty" -H 'Content-Type: application/json' -d' { "query": { "match_phrase": { "about": "rock climbing" } } }'
高亮搜尋
//當執行該查詢時,返回結果與之前一樣,與此同時結果中還多了一個叫做 highlight 的部分。這個部分包含了 about 屬性匹配的文字片段,並以 HTML 標籤 <em></em> 封裝 curl -XGET "http://localhost:9200/megacorp/employee/_search?pretty" -H 'Content-Type: application/json' -d' { "query": { "match_phrase": { "about": "rock climbing" } }, "highlight": { "fields": { "about": {} } } }'
聚合(分析,類似於group by)
curl -X GET "localhost:9200/megacorp/employee/_search?pretty" -H 'Content-Type: application/json' -d' { "aggs": { "all_interests": {//聚合欄位 "terms": { "field": "interests" } } } } //該操作會報錯,不允許聚合,是因為該欄位沒有進行最佳化(類似加索引),沒有最佳化的欄位ES是禁止聚合/排序操作的,所以需要將要聚合的子彈新增最佳化
新增最佳化
curl -X PUT "localhost:9200/megacorp/_mapping?pretty" -H 'Content-Type: application/json' -d' { "properties": { "interests": { "type": "text", "fielddata": true } } }
只對查詢到的資料聚合
curl -XGET "http://localhost:9200/megacorp/employee/_search?pretty" -H 'Content-Type: application/json' -d' { "query": { "match": { "last_name": "smith" } }, "aggs": { "all_interests": { "terms": { "field": "interests" } } } }'
聚合求平均值
curl -XGET "http://localhost:9200/megacorp/employee/_search?pretty" -H 'Content-Type: application/json' -d' { "aggs": { "all_interests": { "terms": { "field": "interests" }, "aggs": { "avg_age": { "avg": { "field": "age" } } } } } }'
獲取叢集健康狀態
curl -XGET "http://localhost:9200/_cluster/health?pretty"
建立索引時設定主分片數量和副本數量
curl -XPUT "http://localhost:9200/blogs?pretty" -H 'Content-Type: application/json' -d' { "settings": { "number_of_shards": 3, "number_of_replicas": 1 } }'
修改副本分片數量
PUT /blogs/_settings?pretty { "number_of_replicas" : 2 }
建立索引(使用POST 自動生成ID)
curl -XPOST "http://localhost:9200/website/blog/?pretty" -H 'Content-Type: application/json' -d' { "title": "My second blog entry", "text": "Still trying this out...", "date": "2014/01/01" }'
返回文件的一部分
curl -XGET "http://localhost:9200/website/blog/123?_source=title,text&pretty"
只想要_source欄位,不需要任何後設資料
curl -XGET "http://localhost:9200/website/blog/123/_source?pretty"
索引唯一文件
//方法一:POST //方法二:PUT,但是判斷沒有相同索引存在的時候才建立索引 //2.1 使用?op_type=create引數 curl -XPUT "http://localhost:9200/website/blog/123?op_type=create" -H 'Content-Type: application/json' -d' { "title": "My second blog entry", "text": "Still trying this out...", "date": "2014/01/01" }' //2.2 使用_create引數 PUT /website/blog/123/_create { "title": "My second blog entry", "text": "Still trying this out...", "date": "2014/01/01" } //失敗返回409,成功返回201
刪除文件
curl -XDELETE "http://localhost:9200/website/blog/123?pretty"
文件部分更新
curl -XPOST "http://localhost:9200/website/blog/1/_update?pretty" -H 'Content-Type: application/json' -d' { "doc": { "tags": ["testing"], "views": 0 } }'
使用指令碼更新
curl -X POST "localhost:9200/website/blog/1/_update?pretty" -H 'Content-Type: application/json' -d' { "script" : "ctx._source.views+=1" }
批次取回資料
curl -XGET "http://localhost:9200/_mget?pretty" -H 'Content-Type: application/json' -d' { "docs" : [ { "_index" : "website", "_type" : "blog", "_id" : 2 }, { "_index" : "website", "_type" : "pageviews", "_id" : 1, "_source": "views" } ] }'
空搜尋
curl -XGET "http://localhost:9200/_search?pretty"
設定超時時間
curl -XGET "http://localhost:9200/_search??timeout=10ms"
多索引多型別
/_search
在所有的索引中搜尋所有的型別
/gb/_search
在 gb 索引中搜尋所有的型別
/gb,us/_search
在 gb 和 us 索引中搜尋所有的文件
/g,u/_search
在任何以 g 或者 u 開頭的索引中搜尋所有的型別
/gb/user/_search
在 gb 索引中搜尋 user 型別
/gb,us/user,tweet/_search
在 gb 和 us 索引中搜尋 user 和 tweet 型別
/_all/user,tweet/_search
在所有的索引中搜尋 user 和 tweet 型別分頁
curl -XGET "http://localhost:9200/_search?size=5&from=10&pretty"
分析器(分詞器)
curl -XGET "http://localhost:9200/_analyze?pretty" -H 'Content-Type: application/json' -d' { "analyzer": "standard", "text": "Text to analyze" }'
檢視對映
curl -XGET "http://localhost:9200/website/_mapping/"
空查詢
curl -XGET "http://localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d' {}' //等價於 curl -XGET "http://localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d' { "query": { "match_all": {} } }'
查詢結構
{ QUERY_NAME: { FIELD_NAME: { ARGUMENT: VALUE, ARGUMENT: VALUE, ... } } } { "match": { "tweet": "elasticsearch" } }
驗證查詢
curl -XGET "http://localhost:9200/gb/tweet/_validate/query?pretty" -H 'Content-Type: application/json' -d' { "query": { "tweet": { "match": "really powerful" } } }'
尋找錯誤資訊
curl -X GET "localhost:9200/gb/tweet/_validate/query?explain&pretty" -H 'Content-Type: application/json' -d' { "query": { "tweet": { "match": "really powerful" } } }
排序
curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d' { "query": { "bool": { "filter": { "term": { "user_id": 1 } } } }, "sort": { "date": { "order": "desc" } } }
多級排序
curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d' { "query": { "bool": { "must": { "match": { "tweet": "manage text search" } }, "filter": { "term": { "user_id": 2 } } } }, "sort": [{ "date": { "order": "desc" } }, { "_score": { "order": "desc" } }] }'
本作品採用《CC 協議》,轉載必須註明作者和本文連結