ES常规语句

4/14/2020 ElasticSearch

摘要

System:Centos7.X
JDK Version:1.8
ElasticSearch Version:6.5.4

# 一:增

# 新增

注意:不能Auto indent

POST /_bulk
{"create":{"_index":"ccjjltx","_type":"_doc","_id":"1"}}
{"name":"ccj"}
{"index":{"_index":"ccjjltx","_type":"_doc"}}
{"name":"ccjjltx"}
1
2
3
4
5
结果
{
  "took" : 19,
  "errors" : false,
  "items" : [
    {
      "create" : {
        "_index" : "ccjjltx",
        "_type" : "_doc",
        "_id" : "1",
        "_version" : 3,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 2,
          "failed" : 0
        },
        "_seq_no" : 6,
        "_primary_term" : 1,
        "status" : 201
      }
    },
    {
      "index" : {
        "_index" : "ccjjltx",
        "_type" : "_doc",
        "_id" : "tZMHd3EBBcVjO8AAEG8F",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 2,
          "failed" : 0
        },
        "_seq_no" : 7,
        "_primary_term" : 1,
        "status" : 201
      }
    }
  ]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

# 二:删

# 清空表

POST ccjjltx/_doc/_delete_by_query
{
     "query": {
        "match_all": {}
    }
}
1
2
3
4
5
6
结果
{
  "took" : 82,
  "timed_out" : false,
  "total" : 2,
  "deleted" : 2,
  "batches" : 1,
  "version_conflicts" : 0,
  "noops" : 0,
  "retries" : {
    "bulk" : 0,
    "search" : 0
  },
  "throttled_millis" : 0,
  "requests_per_second" : -1.0,
  "throttled_until_millis" : 0,
  "failures" : [ ]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

使用***GET ccjjltx/_doc/_search***查询

结果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 0,
    "max_score" : null,
    "hits" : [ ]
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# 三:改

# 根据id新增字段

GET ccjjltx/_doc/3
1

# 根据id修改字段

POST ccjjltx/_doc/3/_update
{
   "script" : "ctx._source.views+=1"
}
1
2
3
4

使用***GET ccjjltx/_doc/3***查询

结果
{
  "_index" : "ccjjltx",
  "_type" : "_doc",
  "_id" : "3",
  "_version" : 3,
  "found" : true,
  "_source" : {
    "name" : "ccj",
    "views" : 1,
    "tags" : [
      "testing"
    ]
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 四:查

# 结果不需要元数据

GET ccjjltx/_doc/1/_source
1
结果
{
  "name" : "ccj"
}
1
2
3

# 结果只需要指定字段

GET task_center_dev/todo/_search?_source=taskId,taskType
1
结果
{
	"took": 1,
	"timed_out": false,
	"_shards": {
		"total": 5,
		"successful": 5,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": 1,
		"max_score": 1.0,
		"hits": [{
			"_index": "task_center_dev",
			"_type": "todo",
			"_id": "1245967770378698752",
			"_score": 1.0,
			"_source": {
				"taskType": 2,
				"taskId": "1245967770378698752"
			}
		}]
	}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
GET task_center_dev/todo/_search
{
  "_source": [
    "taskId",
    "taskType"
  ]
}
1
2
3
4
5
6
7
GET task_center_dev/todo/_search
{
  "_source": [
    "taskId",
    "taskType"
  ]
}
1
2
3
4
5
6
7
GET /task_center_dev/todo/_search
{
  "_source": {
    "includes": [
      "task*"
    ],
    "excludes": [
      "taskType"
    ]
  }
}
1
2
3
4
5
6
7
8
9
10
11
结果
{
	"took": 1,
	"timed_out": false,
	"_shards": {
		"total": 5,
		"successful": 5,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": 1,
		"max_score": 1.0,
		"hits": [{
			"_index": "task_center_dev",
			"_type": "todo",
			"_id": "1245967770378698752",
			"_score": 1.0,
			"_source": {
				"taskId": "1245967770378698752"
			}
		}]
	}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

# 文档是否存在

存在状态码:200 不存在状态码:404

HEAD ccjjltx/_doc/1
1

# 查询多个文档

GET /_mget
{
  "docs": [
    {
      "_index": "ccjjltx",
      "_type": "_doc",
      "_id": 1
    },
    {
      "_index": "ccjjltx",
      "_type": "_doc",
      "_id": 2,
      "_source": "name"
    }
  ]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
结果
{
  "docs" : [
    {
      "_index" : "ccjjltx",
      "_type" : "_doc",
      "_id" : "1",
      "found" : false
    },
    {
      "_index" : "ccjjltx",
      "_type" : "_doc",
      "_id" : "2",
      "_version" : 5,
      "found" : true,
      "_source" : {
        "name" : "ccj"
      }
    }
  ]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

# 查询带version

GET ccj/_doc/search?q=title:test&version=true
1

使用Query DSL语法也是同样的

GET ccj/_doc/_search
{
	"query" : {
		"term" : {
			"title" : "test"
		}
	},
	"version" : true
}
1
2
3
4
5
6
7
8
9
最后更新: 8/18/2020, 4:10:53 PM