使用HTTP操作ES
# 一、索引操作
# 1.1 索引-创建
请求:
curl --request PUT 'http://127.0.0.1:9200/shopping'
响应:
{
"acknowledged": true, //响应结果
"shards_acknowledged": true, //分片结果
"index": "shopping" //索引名称
}
2
3
4
5
重复请求的错误:
{
"error": {
"root_cause": [
{
"type": "resource_already_exists_exception",
"reason": "index [shopping/8xo2JSlXSE-La0TOP764XQ] already exists",
"index_uuid": "8xo2JSlXSE-La0TOP764XQ",
"index": "shopping"
}
],
"type": "resource_already_exists_exception",
"reason": "index [shopping/8xo2JSlXSE-La0TOP764XQ] already exists",
"index_uuid": "8xo2JSlXSE-La0TOP764XQ",
"index": "shopping"
},
"status": 400
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
创建索引库的分片数默认1片,在7.0.0之前的版本中,默认5片。
# 1.2 索引-查询单个
请求:
curl --request GET 'http://127.0.0.1:9200/shopping'
响应:
{
"shopping": { //索引名
"aliases": {}, //别名
"mappings": {}, //映射
"settings": { //设置
"index": { //索引设置
"creation_date": "1635563639085", //创建时间
"number_of_shards": "1", //主分片数量
"number_of_replicas": "1", //副分片数量
"uuid": "k8okReO5SWGkS8eD9GmQMQ", //唯一标识
"version": { //版本
"created": "7080099"
},
"provided_name": "shopping" //索引名称
}
}
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 1.3 索引-查询所有
请求:
curl --request GET 'http://127.0.0.1:9200/_cat/indices?v'
**_cat:**查看所有
**indices:**表示索引
响应:
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open shopping k8okReO5SWGkS8eD9GmQMQ 1 1 0 0 208b 208b
2
表头 | 含义 |
---|---|
health | 当前服务器健康状态:green(集群完整) yellow(单点正常、集群不完整) red(单点不正常) |
status | 索引打开、关闭状态 |
index | 索引名 |
uuid | 索引统一编号 |
pri | 主分片数量 |
rep | 副本数量 |
docs.count | 可用文档数量 |
docs.deleted | 文档删除状态(逻辑删除) |
store.size | 主分片和副分片整体占空间大小 |
pri.store.size | 主分片占空间大小 |
# 1.4 索引-删除
请求:
curl --request DELETE 'http://127.0.0.1:9200/shopping'
响应:
{
"acknowledged": true //响应结果
}
2
3
# 二、文档操作
# 2.1 文档-创建-POST
请求:
curl --request POST 'http://127.0.0.1:9200/shopping/_doc' \
--header 'Content-Type: application/json' \
--data-raw '{
"title":"华为手机",
"category":"华为",
"images":"http://www.gulixueyuan.com/xm.jpg",
"price":3999.00
}'
2
3
4
5
6
7
8
响应:
{
"_index": "shopping", //索引
"_type": "_doc", //文档类型
"_id": "kttLz3wBOuoxVoOHVKK6", //随机生成的ID
"_version": 1, //版本
"result": "created", //结果-创建成功
"_shards": { //分片
"total": 2, //分片-总数
"successful": 1, //分片-成功
"failed": 0 //分片-失败
},
"_seq_no": 1,
"_primary_term": 1
}
2
3
4
5
6
7
8
9
10
11
12
13
14
上面的数据创建后,由于没有指定唯一标识,默认情况下,ES服务器会随机生成一个。
# 2.2 文档-创建-POST-自定义ID
请求:
curl --request POST 'http://127.0.0.1:9200/shopping/_doc/1001' \
--header 'Content-Type: application/json' \
--data-raw '{
"title":"小米手机",
"category":"小米",
"images":"http://www.gulixueyuan.com/xm.jpg",
"price":3999.00
}'
2
3
4
5
6
7
8
响应:
{
"_index": "shopping",
"_type": "_doc",
"_id": "1001",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 2,
"_primary_term": 1
}
2
3
4
5
6
7
8
9
10
11
12
13
14
# 2.3 文档-创建-PUT-自定义ID
除了上述方式,还可以使用PUT方式创建
请求:
curl --request PUT 'http://127.0.0.1:9200/shopping/_doc/1002' \
--header 'Content-Type: application/json' \
--data-raw '{
"title":"小米手机",
"category":"小米",
"images":"http://www.gulixueyuan.com/xm.jpg",
"price":3999.00
}'
2
3
4
5
6
7
8
响应:
{
"_index": "shopping",
"_type": "_doc",
"_id": "1002",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 3,
"_primary_term": 1
}
2
3
4
5
6
7
8
9
10
11
12
13
14
或者使用下面的这种PUT方式
请求:
curl --request PUT 'http://127.0.0.1:9200/shopping/_create/1003' \
--header 'Content-Type: application/json' \
--data-raw '{
"title":"小米手机",
"category":"小米",
"images":"http://www.gulixueyuan.com/xm.jpg",
"price":3999.00
}'
2
3
4
5
6
7
8
响应:
{
"_index": "shopping",
"_type": "_doc",
"_id": "1003",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 4,
"_primary_term": 1
}
2
3
4
5
6
7
8
9
10
11
12
13
14
# 2.4 文档-主键查询
请求:
curl --request GET 'http://127.0.0.1:9200/shopping/_doc/1001'
响应:
{
"_index": "shopping",
"_type": "_doc",
"_id": "1001",
"_version": 1,
"_seq_no": 2,
"_primary_term": 1,
"found": true, //查询结果 true 表示找到 false 表示未找到
"_source": { //文档原信息
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
2
3
4
5
6
7
8
9
10
11
12
13
14
# 2.5 文档-全量检索
请求:
curl --request GET 'http://127.0.0.1:9200/shopping/_search'
响应:
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 5,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "kdtJz3wBOuoxVoOH5KI3",
"_score": 1.0,
"_source": {
"title": "华为手机",
"category": "华为",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "kttLz3wBOuoxVoOHVKK6",
"_score": 1.0,
"_source": {
"title": "华为手机",
"category": "华为",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "1001",
"_score": 1.0,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "1002",
"_score": 1.0,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "1003",
"_score": 1.0,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
}
]
}
}
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# 2.6 文档-全部参数修改
请求:
curl --request PUT 'http://127.0.0.1:9200/shopping/_doc/1001' \
--header 'Content-Type: application/json' \
--data-raw '{
"title":"小米手机",
"category":"小米",
"images":"http://www.gulixueyuan.com/xm.jpg",
"price":4999.00
}'
2
3
4
5
6
7
8
响应:
{
"_index": "shopping",
"_type": "_doc",
"_id": "1001",
"_version": 2, //版本
"result": "updated", //结果-update表示数据被更新
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 5,
"_primary_term": 1
}
2
3
4
5
6
7
8
9
10
11
12
13
14
# 2.7 文档-部分参数修改
请求:
curl --request POST 'http://127.0.0.1:9200/shopping/_update/1001' \
--header 'Content-Type: application/json' \
--data-raw '{
"doc":{
"title": "华为手机"
}
}'
2
3
4
5
6
7
响应:
{
"_index": "shopping",
"_type": "_doc",
"_id": "1001",
"_version": 3,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 6,
"_primary_term": 1
}
2
3
4
5
6
7
8
9
10
11
12
13
14
# 2.8 文档-主键删除(逻辑删除)
请求:
curl --request DELETE 'http://127.0.0.1:9200/shopping/_doc/1001'
响应:
{
"_index": "shopping",
"_type": "_doc",
"_id": "1001",
"_version": 4,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 7,
"_primary_term": 1
}
2
3
4
5
6
7
8
9
10
11
12
13
14
# 2.9 文档-条件删除
请求:
curl --request POST 'http://127.0.0.1:9200/shopping/_delete_by_query' \
--header 'Content-Type: application/json' \
--data-raw '{
"query": {
"match": {
"price": 4000
}
}
}'
2
3
4
5
6
7
8
9
响应:
{
"took": 973, //耗时
"timed_out": false, //是否超时
"total": 0, //总数
"deleted": 0, //删除数量
"batches": 0,
"version_conflicts": 0,
"noops": 0,
"retries": {
"bulk": 0,
"search": 0
},
"throttled_millis": 0,
"requests_per_second": -1.0,
"throttled_until_millis": 0,
"failures": []
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 2.10 文档-条件查询
请求:
curl --request GET 'http://127.0.0.1:9200/shopping/_search?q=category:小米'
响应:
{
"took": 27,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1.0779929,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "1002",
"_score": 1.0779929,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "1003",
"_score": 1.0779929,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
}
]
}
}
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
41
42
43
# 2.11 文档-条件查询-body
请求:
curl --request GET 'http://127.0.0.1:9200/shopping/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"query": {
"match": {
"category": "小米"
}
}
}'
2
3
4
5
6
7
8
9
match
匹配查询 会把查询条件进行分词,然后进行查询,多个词条之间是or的关系
响应:
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1.0779929,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "1002",
"_score": 1.0779929,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "1003",
"_score": 1.0779929,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
}
]
}
}
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
41
42
43
# 2.12 文档-多字段匹配查询
请求:
curl --request GET 'http://127.0.0.1:9200/shopping/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"query": {
"multi_match": {
"query": "米",
"fields": [
"title",
"category"
]
}
}
}'
2
3
4
5
6
7
8
9
10
11
12
13
multi_match
与 match
类似,不同的是它可以在多个字段中查询。
响应:
{
"took": 10,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 0.53899646,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "1002",
"_score": 0.53899646,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "1003",
"_score": 0.53899646,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
}
]
}
}
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
41
42
43
# 2.13 文档-关键字精准查询
请求:
curl --request GET 'http://127.0.0.1:9200/shopping/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"query": {
"term": {
"category":{
"value":"huawei"
}
}
}
}'
2
3
4
5
6
7
8
9
10
11
term
查询,精确的关键词匹配查询,不对查询条件进行分词。对中文无效。
响应:
{
"took": 507,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.8923234,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "n9uK1HwBOuoxVoOH66I4",
"_score": 1.8923234,
"_source": {
"title": "华为手机",
"category": "huawei",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
}
]
}
}
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
# 2.14 文档-多关键字精准查询
请求:
curl --request GET 'http://127.0.0.1:9200/shopping/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"query": {
"terms": {
"category": [
"huawei",
"xiaomi"
]
}
}
}'
2
3
4
5
6
7
8
9
10
11
12
terms
查询和 term
查询一样,但它允许你指定多值进行匹配。
如果这个字段包含了指定值中的任何一个值,那么这个文档满足条件
响应:
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "n9uK1HwBOuoxVoOH66I4",
"_score": 1.0,
"_source": {
"title": "华为手机",
"category": "huawei",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "oduO1HwBOuoxVoOHpaK3",
"_score": 1.0,
"_source": {
"title": "小米手机",
"category": "xiaomi",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
}
]
}
}
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
41
42
43
# 2.15 文档-查询全部
请求:
curl --request GET 'http://127.0.0.1:9200/shopping/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"query": {
"match_all": {}
}
}'
2
3
4
5
6
7
"match_all
" 查询类型,例如:match_all
、match
、term
、range
......
响应:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "kdtJz3wBOuoxVoOH5KI3",
"_score": 1.0,
"_source": {
"title": "华为手机",
"category": "华为",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "kttLz3wBOuoxVoOHVKK6",
"_score": 1.0,
"_source": {
"title": "华为手机",
"category": "华为",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
},
......
]
}
}
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
41
42
43
44
# 2.16 文档-分页查询
from
:当前页的起始索引,默认从 0 开始。 from = (pageNum - 1) * size
size
:每页显示多少条
请求:
curl --request GET 'http://127.0.0.1:9200/shopping/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"query": {
"match_all": {}
},
"from": 0,
"size": 2
}'
2
3
4
5
6
7
8
9
响应:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "kdtJz3wBOuoxVoOH5KI3",
"_score": 1.0,
"_source": {
"title": "华为手机",
"category": "华为",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "kttLz3wBOuoxVoOHVKK6",
"_score": 1.0,
"_source": {
"title": "华为手机",
"category": "华为",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
}
]
}
}
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
41
42
43
# 2.17 文档-分页查询-指定返回字段
请求:
curl --request GET 'http://127.0.0.1:9200/shopping/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"query": {
"match_all": {}
},
"from": 0,
"size": 2,
"_source": [
"title"
]
}'
2
3
4
5
6
7
8
9
10
11
12
指定返回字段,并分页。
响应:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 6,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "kdtJz3wBOuoxVoOH5KI3",
"_score": 1.0,
"_source": {
"title": "华为手机"
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "kttLz3wBOuoxVoOHVKK6",
"_score": 1.0,
"_source": {
"title": "华为手机"
}
}
]
}
}
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
也可以通过设置
includes
:来指定想要显示的字段excludes
:来指定不想要显示的字段
{
"_source":{
"includes":["title","category"]
},
"query":{
"query":{
"match_all":{}
}
}
}
2
3
4
5
6
7
8
9
10
{
"_source":{
"excludes":["title","category"]
},
"query":{
"query":{
"match_all":{}
}
}
}
2
3
4
5
6
7
8
9
10
# 2.18 文档-组合查询
bool
把各种其它查询通过must
(必须 )、must_not
(必须不)、should
(应该)的方式进行组合
# and
请求:
curl --request GET 'http://127.0.0.1:9200/shopping/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"query": {
"bool": {
"must": [
{
"match": {
"category": "小米"
}
},
{
"match": {
"price": 4999
}
}
]
}
}
}'
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
响应:
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 0,
"relation": "eq"
},
"max_score": null,
"hits": []
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# or
请求:
curl --request GET 'http://127.0.0.1:9200/shopping/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"query": {
"bool": {
"should": [
{
"match": {
"category": "小米"
}
},
{
"match": {
"category": "华为"
}
}
]
}
}
}'
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
响应:
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": 2.1778145,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "kdtJz3wBOuoxVoOH5KI3",
"_score": 2.1778145,
"_source": {
"title": "华为手机",
"category": "华为",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
},
......
{
"_index": "shopping",
"_type": "_doc",
"_id": "1003",
"_score": 1.5478239,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
}
]
}
}
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
41
42
43
44
# 2.19 文档-范围查询
range
查询找出那些落在指定区间内的数字或者时间。
操作符 | 说明 |
---|---|
gt | 大于> |
gte | 大于等于>= |
lt | 小于< |
lte | 小于等于<= |
请求:
curl --request GET 'http://127.0.0.1:9200/shopping/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"query": {
"range": {
"price": {
"gte": 3000,
"lte": 4000
}
}
}
}'
2
3
4
5
6
7
8
9
10
11
12
响应:
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 6,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "kdtJz3wBOuoxVoOH5KI3",
"_score": 1.0,
"_source": {
"title": "华为手机",
"category": "华为",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
},
......
{
"_index": "shopping",
"_type": "_doc",
"_id": "oduO1HwBOuoxVoOHpaK3",
"_score": 1.0,
"_source": {
"title": "小米手机",
"category": "xiaomi",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
}
]
}
}
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
41
42
43
44
# 2.20 文档-分页查询-排序
请求:
curl --request GET 'http://127.0.0.1:9200/shopping/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"query": {
"match_all": {}
},
"from": 0,
"size": 2,
"sort": {
"price": {
"order": "desc"
}
}
}'
2
3
4
5
6
7
8
9
10
11
12
13
14
响应:
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 6,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "kdtJz3wBOuoxVoOH5KI3",
"_score": null,
"_source": {
"title": "华为手机",
"category": "华为",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": "4999"
},
"sort": [
4999.0
]
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "kttLz3wBOuoxVoOHVKK6",
"_score": null,
"_source": {
"title": "华为手机",
"category": "华为",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
},
"sort": [
3999.0
]
}
]
}
}
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
41
42
43
44
45
46
47
48
49
# 2.21 文档-高亮查询
在使用 match
查询的同时,加上一个 highlight
属性:
pre_tags:前置标签
post_tags:后置标签
fields:需要高亮的字段
title:这里声明 title 字段需要高亮,后面可以为这个字段设置特有配置,也可以空
请求:
curl --request GET 'http://127.0.0.1:9200/shopping/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"query": {
"match_phrase": {
"category": "小米"
}
},
"highlight": {
"fields": {
"category": {}
}
}
}'
2
3
4
5
6
7
8
9
10
11
12
13
14
响应:
{
"took": 9,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 0.89231336,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "1002",
"_score": 0.89231336,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
},
"highlight": {
"category": [
"<em>小</em><em>米</em>"
]
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "1003",
"_score": 0.89231336,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
},
"highlight": {
"category": [
"<em>小</em><em>米</em>"
]
}
}
]
}
}
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
41
42
43
44
45
46
47
48
49
50
51
52
53
# 2.22 聚合查询
- 对某个字段取最大值
max
- 对某个字段取最小值
min
- 对某个字段求和
sum
- 对某个字段取平均值
avg
- 对某个字段的值进行去重之后再取总数
cardinality
stats
聚合,对某个字段一次性返回 count,max,min,avg 和 sum 五个指标
请求:
curl --request GET 'http://127.0.0.1:9200/shopping/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"aggs": {
"price_stats": { //名称,随便取
"stats": {
"field": "price"
}
}
},
"size": 0 //原始数据数量
}'
2
3
4
5
6
7
8
9
10
11
12
响应:
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 6,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"price_stats": {
"count": 6,
"min": 3999.0,
"max": 4999.0,
"avg": 4165.666666666667,
"sum": 24994.0
}
}
}
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
# 2.23 桶聚合查询
terms
聚合
请求:
curl --request GET 'http://127.0.0.1:9200/shopping/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"aggs": {
"price_terms": {
"terms": {
"field": "price"
}
}
},
"size": 0
}'
2
3
4
5
6
7
8
9
10
11
12
响应:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 6,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"price_terms": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 3999.0,
"doc_count": 5
},
{
"key": 4999.0,
"doc_count": 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
# 三、映射操作
接下来就需要建索引库(index)中的映射了,类似于数据库(database)中的表结构(table)。
创建数据库表需要设置字段名称,类型,长度,约束等;索引库也一样,需要知道这个类型
下有哪些字段,每个字段有哪些约束信息,这就叫做映射(mapping)。
# 3.1 创建映射
请求:
curl --request PUT 'http://127.0.0.1:9200/user/_mapping' \
--header 'Content-Type: application/json' \
--data-raw '{
"properties": {
"name": {
"type": "text",
"index": true
},
"sex": {
"type": "keyword",
"index": true
},
"tel": {
"type": "keyword",
"index": false
}
}
}'
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
type 类型
- String
- text 可分词
- keyword 不可分词
- Numerical
- 基本数据类型:long、integer、short、byte、double、float、half_float
- 浮点数的高精度类型:scaled_float
- Date:日期类型
- Array:数组类型
- Object:对象
index 是否索引 默认为true
true:字段会被索引,则可以用来进行搜索
false:字段不会被索引,不能用来搜索
store 是否将数据进行独立存储,默认为 false
analyzer 分词器
响应:
{
"acknowledged": true
}
2
3
# 3.2 查看映射
请求:
curl --request GET 'http://127.0.0.1:9200/user/_mapping'
响应:
{
"user": {
"mappings": {
"properties": {
"name": {
"type": "text"
},
"sex": {
"type": "keyword"
},
"tel": {
"type": "keyword",
"index": false
}
}
}
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
- 一、索引操作
- 1.1 索引-创建
- 1.2 索引-查询单个
- 1.3 索引-查询所有
- 1.4 索引-删除
- 二、文档操作
- 2.1 文档-创建-POST
- 2.2 文档-创建-POST-自定义ID
- 2.3 文档-创建-PUT-自定义ID
- 2.4 文档-主键查询
- 2.5 文档-全量检索
- 2.6 文档-全部参数修改
- 2.7 文档-部分参数修改
- 2.8 文档-主键删除(逻辑删除)
- 2.9 文档-条件删除
- 2.10 文档-条件查询
- 2.11 文档-条件查询-body
- 2.12 文档-多字段匹配查询
- 2.13 文档-关键字精准查询
- 2.14 文档-多关键字精准查询
- 2.15 文档-查询全部
- 2.16 文档-分页查询
- 2.17 文档-分页查询-指定返回字段
- 2.18 文档-组合查询
- 2.19 文档-范围查询
- 2.20 文档-分页查询-排序
- 2.21 文档-高亮查询
- 2.22 聚合查询
- 2.23 桶聚合查询
- 三、映射操作
- 3.1 创建映射
- 3.2 查看映射