以下操作之前,先看下服務啟動情況 http://127.0.0.1:9200/ 版本:6.3.1
1、索引操作
操作 | 方式 | 接口 | 參數 |
叢集健康 | GET | /_cat/health?v | - |
節點清單 | /_cat/nodes?v | ||
索引清單 | /_cat/indices?v | ||
建立索引 | PUT | /website | |
檢視設定 | /website/_settings | ||
設定索引 | {"number_of_replicas": "2"} | ||
複制索引 | POST | /_reindex | {"source":{"index":"website"}, "dest":{"index":"test002"}} |
關閉索引 | /website/_close | ||
打開索引 | /website/_open | ||
删除索引 | DELETE |
2、文檔操作
添加文檔 | /website/blog/001 | {"name": "tom"} | |
擷取文檔 | |||
檢查文檔是否存在 | HEAD | ||
一次取多個 | /website/blog/_mget | {"docs": [{"_id": "001"}, {"_id": "002"}] } | |
搜尋多個 | /website/_search | {"query":{"terms": {"_id":["001", "002"]}}} | |
更新文檔(doc方式) | /website/blog/001/_update | {"doc":{"name":"Jack" }} | |
更新文檔(腳本方式) | { "script":{"inline": "ctx._source.star += params.star", "lang":"painless","params":{ "star":100} }} | ||
查詢更新 | /website/_update_by_query | {"script":{"inline":"ctx._source.age = 23", "lang":"painless"}, "query":{"term":{"name":"Tom"} } } | |
删除文檔(指定ID) | |||
删除文檔(帶查詢條件) | {"query":{"term":{"name":"Tom"} } } | ||
批量操作(多行格式) | /_bulk | {"index":{ "_index": "books", "_type": "IT", "_id": "1" }}{"name": "Tom"} |
3、搜尋操作
搜尋查詢
POST | /website/_search
檢視所有 | {"query":{"match_all":{} } } |
精确比對 | {"query":{"constant_score":{"filter":{"term":{"price":549} } } } } |
詞項查詢(term query) | {"query":{"term":{"title":"java"} } } |
分詞查詢(match query) | {"query":{"match":{"title":"Core Java"} } } |
分詞查詢(全部比對) | {"query":{"match":{"title":{"query":"Core Java", "operator":"and"} } } } |
順序比對 | {"query":{"match_phrase":{"title":"Core Java"} } } |
字首比對 | {"query":{"match_phrase_prefix":{"title":"Core J"} } } |
多字段搜尋 | {"query":{"multi_match":{"query":"1986 deep", "fields":["title", "description"] } } } |
多詞查詢 | {"query":{"terms":{"title":["deep", "core"] } } } |
範圍查詢 | {"query":{"range":{"publish_time":{"gte":"2016-01-01", "lte":"2016-12-31", "format":"yyyy-MM-dd"} } } } |
存在字段查詢 | {"query":{"exists":{"field":"author"} } } |
字首查詢 | {"query":{"prefix":{"title":"cor"} } } |
通配符查詢("?“一個,”*"多個 | {"query":{"wildcard":{"title":"cor?"} } } |
正規表達式 | {"query":{"regexp":{"description":"[0-9]{4}"} } } |
模糊查詢 | {"query":{"fuzzy":{"description":"1987"} } } |
複合查詢(must, should, must_not, filter) | {"query":{"bool":{"must":{"term":{"title":"java"} }, "must_not":[{"term":{"title":"core"}} ] } } } |
腳本查詢 | {"query":{"bool":{"must":{"script":{"script":{"inline":"doc['price'].value>500", "lang":"painless"} } } } } } |
指定排序字段 | {"query":{"term":{"title":"java"} }, "sort":[{"price":{"order":"desc"}} ] } |
4、其他操作
分詞效果 | /website/_analyze | {"field":"title", "text":"Core Java", "analyzer": "ik_smart"} | |
設定分詞器 | /blog | {"settings" : {"index" : {"analysis.analyzer.default.type": "ik_max_word"} } } | |
分頁 | /website/_search?size=10&from=0 | ||
高亮 | {"query" : {"match_phrase" : {"about" : "rock climbing"} }, "highlight": {"fields" : {"about" : {} } } } | ||
數量 | /website/_count |
參考