Elasticsearch基礎入門教程,經常使用的命令語句,可直接複製到kibana上使用,適用於6.x 7.x。
GET /_cat/health?v&pretty
GET /_cluster/health?pretty&level=indices
GET /_cat/indices?v&pretty
GET /_cat/shards?v&pretty
GET /_cat/allocation?v&pretty
GET /_nodes/process?pretty GET /_cat/nodes?v
GET /_nodes/node1/process?pretty
GET /index/_search_shards
GET /_stats/fielddata?fields=*&index=xx
GET /_analyze/?pretty { "analyzer": "ik_max_word", "text": "測試用例" }
GET /index/type/id/_termvectors?fields=title
PUT /_settings { "index": { "max_result_window": 100000000 } }
PUT /index/_settings { "index": { "max_result_window": 100000000 } }
在config/elasticsearch.yml文件,加上index.max_result_window: 100000000node
匹配全部sql
GET /index/_search { "query": { "match_all": {} } }
短語匹配,要求全部的分詞必須同時出如今文檔中,同時位置必須緊鄰一致。數組
GET /index/_search { "query": { "match_phrase": { "name": "quick brown fox" } } }
match在匹配時會對所查找的關鍵詞進行分詞,而後按分詞匹配查找。
match會將關鍵詞進行分詞分紅「my」和「cat」,查找時包含其中任一都可被匹配到。app
GET /index/_search { "query": { "match": { "name": "my cat" } }, "sort": [ { "age": "desc" } ] }
容許在match查詢的基礎上同時搜索多個字段,在多個字段中同時查一個。less
GET /index/_search { "query": { "multi_match": { "query": "full text search", "fields": [ "title", "body" ] } } }
from爲分頁偏移量,默認第一頁爲0,第n頁爲n*size。nosql
GET /index/_search { "query": { "match_all": {} }, "from": 0, "size": 2 }
GET /index/_search { "query": { "match_all": {} }, "_source": [ "name", "age" ] }
在每一個搜索結果中,高亮部分文本片斷,以便讓用戶知道爲什麼該文檔符合查詢條件。socket
GET /index/_search { "query": { "match_phrase": { "name": "zhangsan" } }, "highlight": { "fields": { "name": {} } } }
term不分詞檢索,搜索不會對搜索詞進行分詞拆解,主要用於精確匹配哪些值。
terms容許指定多個匹配條件,多個term,相似於in查詢。elasticsearch
GET /index/_search { "query": { "term": { "name": "xxx" } } } { "query": { "terms": { "tag": [ "search", "full_text", "nosql" ] } } } { "query": { "bool": { "must": [ { "term": { "tag": "search" } }, { "term": { "tag": "nosql" } } ] } } }
gt:大於 gte:大於等於 lt:小於 lte:小於等於測試
GET /index/_search { "query": { "range": { "age": { "gte": 20, "lt": 30 } } } }
bool 過濾能夠用來合併多個過濾條件查詢結果的布爾邏輯
must:多個查詢條件的徹底匹配,至關於 and。
must_not:多個查詢條件的相反匹配,至關於 not。
should:至少有一個查詢條件匹配, 至關於 or。
filter:必須匹配,根據過濾標準來排除或包含文檔。ui
GET /index/_search { "query": { "bool": { "must": { "match": { "name": "zhangsan" } }, "filter": { "range": { "age": { "gt": 25 } } } } } } { "query": { "bool": { "must": [ { "match": { "title": "IBM" } }, { "match": { "cluster_category": "其餘文章" } } ] } } }
注意:最後一個索引請求體後面需換行
GET /_msearch {"index":"index1"} {"query" : {"match_all" : {}}} {"index":"index2"} {"query" : {"match_all" : {}}}
注意:最後一個索引請求體後面需換行
GET /_msearch {"index":"index1"} {"query" : {"match_all" : {}}} {"index":"index2"} {"query" : {"match_all" : {}}}
GET /index/_search { "track_total_hits": true, "query":{"match_all":{}} }
POST /index/_delete_by_query { "query": { "match_all": {} } }
reindex會將一個索引的數據複製到另外一個已存在的索引,可是並不會複製原索引的mapping(映射)、shard(分片)、replicas(副本)等配置信息,可用於數據遷移。
size,可選,批量提交條數,能夠提升效率,建議每批提交5-15M的數據
type,可選,索引類型
query,可選,添加查詢來過濾文檔
sort,可選,排序
_source,可選,指定字段
remote,可選,es鏈接配置
POST _reindex { "source": { "index": "index1", "size": 1000, "type": "tweet", "query": { "term": { "xx": "xx" } }, "sort": { "date": "desc" }, "_source": [ "xx" ], "remote": { "host": "http://xxx.xxx.xxx:9200", "username": "xxx", "password": "xxx", "socket_timeout": "1m", "connect_timeout": "30s" } }, "dest": { "index": "index2" } }
嵌套對象將數組中的每一個對象索引爲單獨的隱藏文檔,這意味着能夠獨立於其餘對象查詢每一個嵌套對象。
POST /indexName/_update/id { "script": { "lang": "painless", "source": "ctx._source.comments.removeIf(it -> it.name == 'John');" } }
POST /indexName/_update/id { "script": { "source": "for(e in ctx._source.comments){if (e.name == 'steve') {e.age = 25; e.comment= 'good article...';}}" } }
POST /index/_search { "query": { "bool": { "must": [ { "nested": { "path": "comments", "query": { "bool": { "must": [ { "match": { "comments.name": "William" } }, { "match": { "comments.age": 34 } } ] } } } } ] } } }