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 } } ] } } } } ] } } }