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?prettyGET /_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                    }                  }                ]              }            }          }        }      ]    }  }}