Elasticsearch基础入门教程,常用的命令语句,可直接复制到kibana上使用,适用于6.x 7.x。
1 集群/索引相关
1.1 查看集群状态
GET /_cat/health?v&pretty
1.2 查看集群的索引状态
GET /_cluster/health?pretty&level=indices
1.3 查看索引信息
GET /_cat/indices?v&pretty
1.4 查看分片信息
GET /_cat/shards?v&pretty
1.5 查看各节点的容量使用情况
GET /_cat/allocation?v&pretty
1.6 查看各节点信息
GET /_nodes/process?pretty
GET /_cat/nodes?v
1.7 查看某个节点信息
GET /_nodes/node1/process?pretty
1.8 查看某个索引分片信息
GET /index/_search_shards
1.9 查看某个索引的fielddata
GET /_stats/fielddata?fields=*&index=xx
1.10 查看es分词器的分词结果
GET /_analyze/?pretty
{
"analyzer": "ik_max_word",
"text": "测试用例"
}
1.11 查看索引中某个文档具体字段的分词结果
GET /index/type/id/_termvectors?fields=title
1.12 设置搜索的最大返回数(三种方式)
1.12.1 全局配置
PUT /_settings
{
"index": {
"max_result_window": 100000000
}
}
1.12.2 针对某个索引配置
PUT /index/_settings
{
"index": {
"max_result_window": 100000000
}
}
1.12.3 配置文件
在config/elasticsearch.yml文件,加上index.max_result_window: 100000000
2 搜索相关
2.1 match_all
匹配所有
GET /index/_search
{
"query": {
"match_all": {}
}
}
2.2 match_phrase
短语匹配,要求所有的分词必须同时出现在文档中,同时位置必须紧邻一致。
GET /index/_search
{
"query": {
"match_phrase": {
"name": "quick brown fox"
}
}
}
2.3 match
match在匹配时会对所查找的关键词进行分词,然后按分词匹配查找。
match会将关键词进行分词分成“my”和“cat”,查找时包含其中任一均可被匹配到。
GET /index/_search
{
"query": {
"match": {
"name": "my cat"
}
},
"sort": [
{
"age": "desc"
}
]
}
2.4 multi_match
允许在match查询的基础上同时搜索多个字段,在多个字段中同时查一个。
GET /index/_search
{
"query": {
"multi_match": {
"query": "full text search",
"fields": [
"title",
"body"
]
}
}
}
2.5 分页查询
from为分页偏移量,默认第一页为0,第n页为n*size。
GET /index/_search
{
"query": {
"match_all": {}
},
"from": 0,
"size": 2
}
2.6 指定查询结果的字段
GET /index/_search
{
"query": {
"match_all": {}
},
"_source": [
"name",
"age"
]
}
2.7 高亮显示查询结果
在每个搜索结果中,高亮部分文本片段,以便让用户知道为何该文档符合查询条件。
GET /index/_search
{
"query": {
"match_phrase": {
"name": "zhangsan"
}
},
"highlight": {
"fields": {
"name": {}
}
}
}
2.8 term与terms
term不分词检索,搜索不会对搜索词进行分词拆解,主要用于精确匹配哪些值。
terms允许指定多个匹配条件,多个term,类似于in查询。
GET /index/_search
{
"query": {
"term": {
"name": "xxx"
}
}
}
{
"query": {
"terms": {
"tag": [
"search",
"full_text",
"nosql"
]
}
}
}
{
"query": {
"bool": {
"must": [
{
"term": {
"tag": "search"
}
},
{
"term": {
"tag": "nosql"
}
}
]
}
}
}
2.9 range过滤
gt:大于 gte:大于等于 lt:小于 lte:小于等于
GET /index/_search
{
"query": {
"range": {
"age": {
"gte": 20,
"lt": 30
}
}
}
}
2.10 bool
bool 过滤可以用来合并多个过滤条件查询结果的布尔逻辑
must:多个查询条件的完全匹配,相当于 and。
must_not:多个查询条件的相反匹配,相当于 not。
should:至少有一个查询条件匹配, 相当于 or。
filter:必须匹配,根据过滤标准来排除或包含文档。
GET /index/_search
{
"query": {
"bool": {
"must": {
"match": {
"name": "zhangsan"
}
},
"filter": {
"range": {
"age": {
"gt": 25
}
}
}
}
}
}
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "IBM"
}
},
{
"match": {
"cluster_category": "其他文章"
}
}
]
}
}
}
2.11 多索引查询
注意:最后一个索引请求体后面需换行
GET /_msearch
{"index":"index1"}
{"query" : {"match_all" : {}}}
{"index":"index2"}
{"query" : {"match_all" : {}}}
2.12 多索引查询
注意:最后一个索引请求体后面需换行
GET /_msearch
{"index":"index1"}
{"query" : {"match_all" : {}}}
{"index":"index2"}
{"query" : {"match_all" : {}}}
2.13 搜索结果返回总数
GET /index/_search
{
"track_total_hits": true,
"query":{"match_all":{}}
}
3 删除数据
- 删除索引数据只能使用:deletebyquery,相比删除索引,deletebyquery删除数据只是逻辑删除;
- 真正的删除实际是段合并后的物理删除分段,也就是deletebyquery后,有一段时间磁盘空间不降反升。
POST /index/_delete_by_query
{
"query": {
"match_all": {}
}
}
4 reindex
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"
}
}
5 Nested嵌套类型
嵌套对象将数组中的每个对象索引为单独的隐藏文档,这意味着可以独立于其他对象查询每个嵌套对象。
5.1 Nested类型 删除操作
POST /indexName/_update/id
{
"script": {
"lang": "painless",
"source": "ctx._source.comments.removeIf(it -> it.name == 'John');"
}
}
5.2 Nested类型 修改操作
POST /indexName/_update/id
{
"script": {
"source": "for(e in ctx._source.comments){if (e.name == 'steve') {e.age = 25; e.comment= 'good article...';}}"
}
}
5.3 Nested类型 查询操作
POST /index/_search
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "comments",
"query": {
"bool": {
"must": [
{
"match": {
"comments.name": "William"
}
},
{
"match": {
"comments.age": 34
}
}
]
}
}
}
}
]
}
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。