最近的项目需要用到ElasticSearch,在这里记录下常用的操作命令,版本基于ElasticSearch 7.1.0
准备数据
# inert document
PUT /rain-index-1/_doc/1
{
"name": "kily",
"age": "22",
"birthday": "2010-10-10"
}
PUT /rain-index-1/_doc/2
{
"name": "lucy",
"age": "21",
"birthday": "2011-11-11"
}
# insert document without id
POST /rain-index-2/_doc
{
"name": "chirry",
"age": "11",
"birthday": "2012-02-02"
}
POST /rain-index-2/_doc
{
"name": "rubby",
"age": "14",
"birthday": "2014-04-04"
}
# insert document in bulk mode
POST /rain-index-3/_bulk
{"index": {"_id": "1"}}
{"name": "axe", "age": "17", "birthday": "2017-07-07", "message": "hello world, hello earth, I like orange"}
{"index": {"_id": "2"}}
{"name": "piity", "age": "18", "birthday": "2018-08-08", "message": "hello world, hello earth, I like grape & grape juice"}
# insert document in bulk mode without index in url
POST _bulk
{"index": {"_index": "rain-index-4", "_id": "1"}}
{"name": "zura", "age": "17", "birthday": "2017-07-07", "message": "hello world, I like banana"}
{"index": {"_index": "rain-index-4", "_id": "2"}}
{"name": "peter", "age": "18", "birthday": "2018-08-08", "message": "hello world, I like apple & apple juice"}
集群相关
- 查看集群健康状态
GET _cluster/health
- 查看集群配置
GET /_cluster/settings
GET /_cluster/settings?include_defaults=true
- 查看节点信息
GET /_nodes
- 查看索引信息
GET /_cat/indices?v
索引相关
- 查看索引(可指定条件)
GET _cat/indices?v&h=health,status,index
- 删除索引(可通配符匹配)
DELETE rain-*
- 获取索引Mapping
GET /rain-index-1/_mapping
文档相关
删除文档
- 删除文档
DELETE /rain-index-1/_doc/1
# specify timeout, default is 1m and then a error response return
DELETE /rain-index-1/_doc/1?timeout=5m
- 删除文档(通过查询)
POST /rain-index-1/_delete_by_query
{
"query": {
"match": {
"name": "lucy"
}
}
}
简单查询
- 检查文档是否存在
HEAD /rain-index-1/_doc/1
- 获取指定文档
GET /rain-index-1/_doc/1
- 获取指定文档(仅仅返回_source内容,可添加筛选条件)
GET /rain-index-1/_source/1
GET /rain-index-1/_source/1?_source_includes=name,age
- 获取指定文档(筛选_source内容)
# shorter notation
GET /rain-index-4/_doc/1?_source=name,age,message
# complete notation
GET /rain-index-4/_doc/1?_source_includes=name,age
GET /rain-index-4/_doc/1?_source_excludes=birthday
- 获取多个指定文档
GET /rain-index-3/_mget
{
"ids" : ["1", "2"]
}
# with filter
GET /rain-index-3/_mget
{
"docs" : [
{
"_id" : "1",
"_source": ["name", "age", "message"]
},
{
"_id" : "2",
"_source": ["name", "age", "message"]
}
]
}
高级查询
- 通过
query string
查找,参考URI Search
GET /rain-index-1/_search?q=age:21
- 通过
term
做完全匹配(不分词)
GET /rain-index-1/_search
{
"query" : {
"term" : { "name" : "kily" }
}
}
- 通过
match
做匹配(分词)
GET /rain-index-3/_search
{
"query": {
"match": {
"message": "hello world"
}
},
"sort": ["birthday"],
"from": "0",
"size": "10",
}
- 通过
filter
做匹配(不计算相关性分数)
GET /rain-index-1/_search
{
"_source": ["name", "birthday"],
"query": {
"bool": {
"filter": [
{"term": {"name": "kily"}},
{"term": {"age": "22"}}
]
}
}
}
官方示例
- Term Query
GET /_search
{
"query": {
"term": {
"user": {
"value": "Kimchy",
"boost": 1.0
}
}
}
}
- Terms Query
GET /_search
{
"query": {
"terms" : { "user" : ["kimchy", "elasticsearch"]}
}
}
- Range Query
GET _search
{
"query": {
"range" : {
"age" : {
"gte" : 10,
"lte" : 20
}
}
}
}
- Ids Query
GET /_search
{
"query": {
"ids" : {
"values" : ["1", "4", "100"]
}
}
}
- Bool Query
POST _search
{
"query": {
"bool" : {
"must" : {
"term" : { "user" : "kimchy" }
},
"filter": {
"term" : { "tag" : "tech" }
},
"must_not" : {
"range" : {
"age" : { "gte" : 10, "lte" : 20 }
}
},
"should" : [
{ "term" : { "tag" : "wow" } },
{ "term" : { "tag" : "elasticsearch" } }
],
"minimum_should_match" : 1,
"boost" : 1.0
}
}
}
- 聚合
POST /exams/_search?size=0
{
"aggs" : {
"avg_grade" : { "avg" : { "field" : "grade" } }
}
}