# Elastic Search的各种查询
# term与terms
term的查询代表完全匹配,搜索之前不会对搜索关键词进行分词,直接去文档分词库中去匹配内容。
- term查询 (POST /book/novel/_search)
- 请求
{
"from": 0,
"size": 5,
"query": {
"term": {
"name": {
"value": "西游记"
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
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" : 6,
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "book",
"_type" : "novel",
"_id" : "ftqNN3oBwiPJbe8gGw2O",
"_score" : 0.2876821,
"_source" : {
"name" : "西游记",
"author" : "吴承恩",
"count" : 1000000,
"on-sale" : "2000-01-10",
"descr" : "九九八十一难"
}
},
{
"_index" : "book",
"_type" : "novel",
"_id" : "gNqNN3oBwiPJbe8gPw0j",
"_score" : 0.2876821,
"_source" : {
"name" : "西游记",
"author" : "吴承恩",
"count" : 1000000,
"on-sale" : "2000-01-10",
"descr" : "九九八十一难"
}
},
{
"_index" : "book",
"_type" : "novel",
"_id" : "f9qNN3oBwiPJbe8gOw0r",
"_score" : 0.18232156,
"_source" : {
"name" : "西游记",
"author" : "吴承恩",
"count" : 1000000,
"on-sale" : "2000-01-10",
"descr" : "九九八十一难"
}
},
{
"_index" : "book",
"_type" : "novel",
"_id" : "gdqNN3oBwiPJbe8gQw0t",
"_score" : 0.18232156,
"_source" : {
"name" : "西游记",
"author" : "吴承恩",
"count" : 1000000,
"on-sale" : "2000-01-10",
"descr" : "九九八十一难"
}
},
{
"_index" : "book",
"_type" : "novel",
"_id" : "gtqNN3oBwiPJbe8gRw1M",
"_score" : 0.18232156,
"_source" : {
"name" : "西游记",
"author" : "吴承恩",
"count" : 1000000,
"on-sale" : "2000-01-10",
"descr" : "九九八十一难"
}
}
]
}
}
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
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
80
81
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
80
81
# terms查询 (POST /book/novel/_search) 与term类似,应用于某字段多值匹配查询时使用(类似mysql中的in查询)。
- 请求
{
"query": {
"terms": {
"name": [
"西游记",
"红楼梦"
]
}
}
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
- 响应
{
"took" : 6,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 7,
"max_score" : 1.0,
"hits" : [
{
"_index" : "book",
"_type" : "novel",
"_id" : "f9qNN3oBwiPJbe8gOw0r",
"_score" : 1.0,
"_source" : {
"name" : "西游记",
"author" : "吴承恩",
"count" : 1000000,
"on-sale" : "2000-01-10",
"descr" : "九九八十一难"
}
},
{
"_index" : "book",
"_type" : "novel",
"_id" : "gdqNN3oBwiPJbe8gQw0t",
"_score" : 1.0,
"_source" : {
"name" : "西游记",
"author" : "吴承恩",
"count" : 1000000,
"on-sale" : "2000-01-10",
"descr" : "九九八十一难"
}
},
{
"_index" : "book",
"_type" : "novel",
"_id" : "ftqNN3oBwiPJbe8gGw2O",
"_score" : 1.0,
"_source" : {
"name" : "西游记",
"author" : "吴承恩",
"count" : 1000000,
"on-sale" : "2000-01-10",
"descr" : "九九八十一难"
}
},
{
"_index" : "book",
"_type" : "novel",
"_id" : "gtqNN3oBwiPJbe8gRw1M",
"_score" : 1.0,
"_source" : {
"name" : "西游记",
"author" : "吴承恩",
"count" : 1000000,
"on-sale" : "2000-01-10",
"descr" : "九九八十一难"
}
},
{
"_index" : "book",
"_type" : "novel",
"_id" : "g9qNN3oBwiPJbe8gSg3s",
"_score" : 1.0,
"_source" : {
"name" : "西游记",
"author" : "吴承恩",
"count" : 1000000,
"on-sale" : "2000-01-10",
"descr" : "九九八十一难"
}
},
{
"_index" : "book",
"_type" : "novel",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "红楼梦",
"author" : "施耐庵",
"count" : 1000001,
"on-sale" : "2001-01-10",
"descr" : "刘姥姥进大观园"
}
},
{
"_index" : "book",
"_type" : "novel",
"_id" : "gNqNN3oBwiPJbe8gPw0j",
"_score" : 1.0,
"_source" : {
"name" : "西游记",
"author" : "吴承恩",
"count" : 1000000,
"on-sale" : "2000-01-10",
"descr" : "九九八十一难"
}
}
]
}
}
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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# match查询
- match查询属于高层查询,它会根据你查询的字段类型不同,采用不同的查询方式。
- 在查询日期时,如果基于字符串查询则会自动转为日期或数值对待。
- 如果查询的内容不能分词(keyword),match查询不会对你指定的查询关键字进行分词。
- 如果查询的内容可以被分词(text),match会将你指定的查询内容根据一定的方式去分词,再去分词库中匹配指定的内容。
- match查询实际底层是多个term查询,将多个term查询的结果封装到了一起。
# match_all查询 不指定查询条件 查询全部内容 (POST /book/novel/_search)
- 请求
{
"query": {
"match_all": {}
}
}
1
2
3
4
5
2
3
4
5
- 响应
{
"took" : 69,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 7,
"max_score" : 1.0,
"hits" : [
{
"_index" : "book",
"_type" : "novel",
"_id" : "f9qNN3oBwiPJbe8gOw0r",
"_score" : 1.0,
"_source" : {
"name" : "西游记",
"author" : "吴承恩",
"count" : 1000000,
"on-sale" : "2000-01-10",
"descr" : "九九八十一难"
}
},
{
"_index" : "book",
"_type" : "novel",
"_id" : "gdqNN3oBwiPJbe8gQw0t",
"_score" : 1.0,
"_source" : {
"name" : "西游记",
"author" : "吴承恩",
"count" : 1000000,
"on-sale" : "2000-01-10",
"descr" : "九九八十一难"
}
},
{
"_index" : "book",
"_type" : "novel",
"_id" : "ftqNN3oBwiPJbe8gGw2O",
"_score" : 1.0,
"_source" : {
"name" : "西游记",
"author" : "吴承恩",
"count" : 1000000,
"on-sale" : "2000-01-10",
"descr" : "九九八十一难"
}
},
{
"_index" : "book",
"_type" : "novel",
"_id" : "gtqNN3oBwiPJbe8gRw1M",
"_score" : 1.0,
"_source" : {
"name" : "西游记",
"author" : "吴承恩",
"count" : 1000000,
"on-sale" : "2000-01-10",
"descr" : "九九八十一难"
}
},
{
"_index" : "book",
"_type" : "novel",
"_id" : "g9qNN3oBwiPJbe8gSg3s",
"_score" : 1.0,
"_source" : {
"name" : "西游记",
"author" : "吴承恩",
"count" : 1000000,
"on-sale" : "2000-01-10",
"descr" : "九九八十一难"
}
},
{
"_index" : "book",
"_type" : "novel",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "红楼梦",
"author" : "施耐庵",
"count" : 1000001,
"on-sale" : "2001-01-10",
"descr" : "刘姥姥进大观园"
}
},
{
"_index" : "book",
"_type" : "novel",
"_id" : "gNqNN3oBwiPJbe8gPw0j",
"_score" : 1.0,
"_source" : {
"name" : "西游记",
"author" : "吴承恩",
"count" : 1000000,
"on-sale" : "2000-01-10",
"descr" : "九九八十一难"
}
}
]
}
}
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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# match查询 指定一个Field查询条件 筛选查询内容 (POST /book/novel/_search)
- 请求
{
"query": {
"match": {
"descr": "九九八十一"
}
}
}
1
2
3
4
5
6
7
2
3
4
5
6
7
- 响应
{
"took" : 9,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 6,
"max_score" : 1.9750715,
"hits" : [
{
"_index" : "book",
"_type" : "novel",
"_id" : "gtqNN3oBwiPJbe8gRw1M",
"_score" : 1.9750715,
"_source" : {
"name" : "西游记",
"author" : "吴承恩",
"count" : 1000000,
"on-sale" : "2000-01-10",
"descr" : "九九八十一难"
}
},
{
"_index" : "book",
"_type" : "novel",
"_id" : "g9qNN3oBwiPJbe8gSg3s",
"_score" : 1.9750715,
"_source" : {
"name" : "西游记",
"author" : "吴承恩",
"count" : 1000000,
"on-sale" : "2000-01-10",
"descr" : "九九八十一难"
}
},
{
"_index" : "book",
"_type" : "novel",
"_id" : "ftqNN3oBwiPJbe8gGw2O",
"_score" : 1.1507283,
"_source" : {
"name" : "西游记",
"author" : "吴承恩",
"count" : 1000000,
"on-sale" : "2000-01-10",
"descr" : "九九八十一难"
}
},
{
"_index" : "book",
"_type" : "novel",
"_id" : "gNqNN3oBwiPJbe8gPw0j",
"_score" : 1.1507283,
"_source" : {
"name" : "西游记",
"author" : "吴承恩",
"count" : 1000000,
"on-sale" : "2000-01-10",
"descr" : "九九八十一难"
}
},
{
"_index" : "book",
"_type" : "novel",
"_id" : "f9qNN3oBwiPJbe8gOw0r",
"_score" : 0.72928625,
"_source" : {
"name" : "西游记",
"author" : "吴承恩",
"count" : 1000000,
"on-sale" : "2000-01-10",
"descr" : "九九八十一难"
}
},
{
"_index" : "book",
"_type" : "novel",
"_id" : "gdqNN3oBwiPJbe8gQw0t",
"_score" : 0.72928625,
"_source" : {
"name" : "西游记",
"author" : "吴承恩",
"count" : 1000000,
"on-sale" : "2000-01-10",
"descr" : "九九八十一难"
}
}
]
}
}
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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# 布尔match查询 基于一个Field匹配查询条件 采用and或者or的方式连接筛选查询内容 (POST /book/novel/_search)
- 请求
{
"query": {
"match": {
"descr": {
"query": "刘姥姥 四大名著",
"operator": "and"
}
}
}
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
- 响应
{
"took" : 23,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 6.1083026,
"hits" : [
{
"_index" : "book",
"_type" : "novel",
"_id" : "1",
"_score" : 6.1083026,
"_source" : {
"name" : "红楼梦",
"author" : "施耐庵",
"count" : 1000001,
"on-sale" : "2001-01-10",
"descr" : "四大名著刘姥姥进大观园"
}
}
]
}
}
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
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
- 请求
{
"query": {
"match": {
"descr": {
"query": "刘姥姥 四大名著",
"operator": "or"
}
}
}
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
- 响应
{
"took" : 6,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 6.1083026,
"hits" : [
{
"_index" : "book",
"_type" : "novel",
"_id" : "1",
"_score" : 6.1083026,
"_source" : {
"name" : "红楼梦",
"author" : "施耐庵",
"count" : 1000001,
"on-sale" : "2001-01-10",
"descr" : "四大名著刘姥姥进大观园"
}
},
{
"_index" : "book",
"_type" : "novel",
"_id" : "2",
"_score" : 1.7894151,
"_source" : {
"name" : "红楼梦",
"author" : "曹雪芹",
"count" : 123456789,
"on-sale" : "2011-12-13",
"descr" : "刘姥姥进大观园。"
}
}
]
}
}
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
41
42
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
# multi_match查询 (POST /book/novel/_search)
match针对一个Field做检索,multi_match针对多个Field对应一个text进行检索。
请求
{
"query": {
"multi_match": {
"query": "四大名著 红楼梦",
"fields": ["name","descr"]
}
}
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
- 响应
{
"took" : 23,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 4.581227,
"hits" : [
{
"_index" : "book",
"_type" : "novel",
"_id" : "1",
"_score" : 4.581227,
"_source" : {
"name" : "红楼梦",
"author" : "施耐庵",
"count" : 1000001,
"on-sale" : "2001-01-10",
"descr" : "四大名著刘姥姥进大观园"
}
},
{
"_index" : "book",
"_type" : "novel",
"_id" : "2",
"_score" : 2.9424875,
"_source" : {
"name" : "红楼梦",
"author" : "曹雪芹",
"count" : 123456789,
"on-sale" : "2011-12-13",
"descr" : "刘姥姥进大观园。"
}
}
]
}
}
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
41
42
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
# id查询 (GET /book/novel/1)
- 响应
{
"_index" : "book",
"_type" : "novel",
"_id" : "1",
"_version" : 4,
"found" : true,
"_source" : {
"name" : "红楼梦",
"author" : "施耐庵",
"count" : 1000001,
"on-sale" : "2001-01-10",
"descr" : "四大名著刘姥姥进大观园"
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# ids查询 (GET /book/novel/_search) 类似mysql的in查询
- 请求
{
"query": {
"ids": {
"values": ["1","2"]
}
}
}
1
2
3
4
5
6
7
2
3
4
5
6
7
- 响应
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 1.0,
"hits" : [
{
"_index" : "book",
"_type" : "novel",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "红楼梦",
"author" : "曹雪芹",
"count" : 123456789,
"on-sale" : "2011-12-13",
"descr" : "刘姥姥进大观园。"
}
},
{
"_index" : "book",
"_type" : "novel",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "红楼梦",
"author" : "施耐庵",
"count" : 1000001,
"on-sale" : "2001-01-10",
"descr" : "四大名著刘姥姥进大观园"
}
}
]
}
}
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
41
42
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
# prefix(前缀)通过查询条件分词查询 (POST /book/novel/_search)
- 请求
{
"query": {
"prefix": {
"descr": {
"value": "大名"
}
}
}
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
- 响应
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [
{
"_index" : "book",
"_type" : "novel",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "红楼梦",
"author" : "施耐庵",
"count" : 1000001,
"on-sale" : "2001-01-10",
"descr" : "四大名著刘姥姥进大观园"
}
}
]
}
}
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
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
# fuzzy(模糊)通过查询条件大概分词查询匹配结果 (POST /book/novel/_search)
- 请求
{
"query": {
"fuzzy": {
"descr": {
"value": "lao",
"prefix_length": 1
}
}
}
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
- 响应
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.2435398,
"hits" : [
{
"_index" : "book",
"_type" : "novel",
"_id" : "4",
"_score" : 1.2435398,
"_source" : {
"name" : "红楼梦",
"author" : "曹雪芹",
"count" : 123456789,
"on-sale" : "2011-12-13",
"descr" : "liu lao lao。"
}
}
]
}
}
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
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
# wildcard查询 (POST /book/novel/_search) 通配查询,和Mysql中的like查询类似,可以在字符串中指定通配符 * 和占位符 ?
- 请求
{
"query": {
"wildcard": {
"descr": {
"value": "*laolao"
}
}
}
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
- 返回
{
"took" : 7,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [
{
"_index" : "book",
"_type" : "novel",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"name" : "红楼梦",
"author" : "曹雪芹",
"count" : 123456789,
"on-sale" : "2011-12-13",
"descr" : "liulaolao进大观园。"
}
}
]
}
}
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
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
# range查询 (POST /book/novel/_search) 范围查询
- 请求
{
"query": {
"range": {
"count": {
"gte": 1000001,
"lte": 1000002
}
}
}
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
- 返回
{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [
{
"_index" : "book",
"_type" : "novel",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "红楼梦",
"author" : "施耐庵",
"count" : 1000001,
"on-sale" : "2001-01-10",
"descr" : "四大名著刘姥姥进大观园"
}
}
]
}
}
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
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
# regexp查询 (POST /book/novel/_search) 正则查询,通过正则表达式匹配内容查询
- 请求
{
"query": {
"regexp": {
"descr": "123[0-9]{6}"
}
}
}
1
2
3
4
5
6
7
2
3
4
5
6
7
- 返回
{
"took" : 13,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [
{
"_index" : "book",
"_type" : "novel",
"_id" : "5",
"_score" : 1.0,
"_source" : {
"name" : "红楼梦",
"author" : "曹雪芹",
"count" : 123456789,
"on-sale" : "2011-12-13",
"descr" : "123000000liu lao lao。"
}
}
]
}
}
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
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
# 深分页Scroll
ES对from+size是有限制的,from和size二者之和不能超过1W。
原理:
- (form+size)ES查询数据方式:
- 将用户指定的关键词进行分词。
- 将词汇去分词库中进行检索,得到多个文档的id。
- 去各个分片中去拉去对应的数据(耗时较长)。
- 将数据根据score进行排序(耗时较长)。
- 根据from的值将查到的数据舍弃一部分返回结果。
- (scroll+size)ES查询数据方式:
- 将用户指定的关键词进行分词。
- 将词汇去分词库中进行检索,得到多个文档的id。
- 将文档的id存放在一个ES的上下文中。
- 根据指定的size数去ES中检索对应个数的数据,如果存在多于的id则会从上下文中移除。
- 如果需要下一页数据则会直接去ES的上下文中寻找后续数据内容。
- 循环前两步骤最终输出结果。
- (form+size)ES查询数据方式:
- Scroll查询方式不适合做实时查询。
- 执行scroll查询,返回第一页数据,并将文档id信息存放在ES上下文中,指定生存时间1m (POST /book/novel/_search?scroll=10m)
- 请求
{
"query": {
"match_all": {}
},
"size": 6,
"sort": [
{
"count": {
"order": "desc"
}
}
]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
- 响应
{
"_scroll_id" : "DnF1ZXJ5VGhlbkZldGNoBQAAAAAAANxPFnFmc2VvM2pmU1JhWjhwNGVTdUpLQ3cAAAAAAADcUBZxZnNlbzNqZlNSYVo4cDRlU3VKS0N3AAAAAAAA3FEWcWZzZW8zamZTUmFaOHA0ZVN1SktDdwAAAAAAANxSFnFmc2VvM2pmU1JhWjhwNGVTdUpLQ3cAAAAAAADcUxZxZnNlbzNqZlNSYVo4cDRlU3VKS0N3",
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 12,
"max_score" : null,
"hits" : [
{
"_index" : "book",
"_type" : "novel",
"_id" : "5",
"_score" : null,
"_source" : {
"name" : "红楼梦",
"author" : "曹雪芹",
"count" : 123456789,
"on-sale" : "2011-12-13",
"descr" : "123000000liu lao lao。"
},
"sort" : [
123456789
]
},
{
"_index" : "book",
"_type" : "novel",
"_id" : "2",
"_score" : null,
"_source" : {
"name" : "红楼梦",
"author" : "曹雪芹",
"count" : 123456789,
"on-sale" : "2011-12-13",
"descr" : "刘姥姥进大观园。"
},
"sort" : [
123456789
]
},
{
"_index" : "book",
"_type" : "novel",
"_id" : "4",
"_score" : null,
"_source" : {
"name" : "红楼梦",
"author" : "曹雪芹",
"count" : 123456789,
"on-sale" : "2011-12-13",
"descr" : "liu lao lao。"
},
"sort" : [
123456789
]
},
{
"_index" : "book",
"_type" : "novel",
"_id" : "3",
"_score" : null,
"_source" : {
"name" : "红楼梦",
"author" : "曹雪芹",
"count" : 123456789,
"on-sale" : "2011-12-13",
"descr" : "liulaolao进大观园。"
},
"sort" : [
123456789
]
},
{
"_index" : "book",
"_type" : "novel",
"_id" : "1",
"_score" : null,
"_source" : {
"name" : "红楼梦",
"author" : "施耐庵",
"count" : 1000001,
"on-sale" : "2001-01-10",
"descr" : "四大名著刘姥姥进大观园"
},
"sort" : [
1000001
]
},
{
"_index" : "book",
"_type" : "novel",
"_id" : "f9qNN3oBwiPJbe8gOw0r",
"_score" : null,
"_source" : {
"name" : "西游记",
"author" : "吴承恩",
"count" : 1000000,
"on-sale" : "2000-01-10",
"descr" : "九九八十一难"
},
"sort" : [
1000000
]
}
]
}
}
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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
- 根据scroll查询下一页数据 (POST /_search/scroll)
- 请求
{
"scroll_id":"DnF1ZXJ5VGhlbkZldGNoBQAAAAAAANxPFnFmc2VvM2pmU1JhWjhwNGVTdUpLQ3cAAAAAAADcUBZxZnNlbzNqZlNSYVo4cDRlU3VKS0N3AAAAAAAA3FEWcWZzZW8zamZTUmFaOHA0ZVN1SktDdwAAAAAAANxSFnFmc2VvM2pmU1JhWjhwNGVTdUpLQ3cAAAAAAADcUxZxZnNlbzNqZlNSYVo4cDRlU3VKS0N3",
"scroll": "10m"
}
1
2
3
4
2
3
4
- 响应
{
"_scroll_id" : "DnF1ZXJ5VGhlbkZldGNoBQAAAAAAANxPFnFmc2VvM2pmU1JhWjhwNGVTdUpLQ3cAAAAAAADcUBZxZnNlbzNqZlNSYVo4cDRlU3VKS0N3AAAAAAAA3FEWcWZzZW8zamZTUmFaOHA0ZVN1SktDdwAAAAAAANxSFnFmc2VvM2pmU1JhWjhwNGVTdUpLQ3cAAAAAAADcUxZxZnNlbzNqZlNSYVo4cDRlU3VKS0N3",
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 12,
"max_score" : null,
"hits" : [
{
"_index" : "book",
"_type" : "novel",
"_id" : "gdqNN3oBwiPJbe8gQw0t",
"_score" : null,
"_source" : {
"name" : "西游记",
"author" : "吴承恩",
"count" : 1000000,
"on-sale" : "2000-01-10",
"descr" : "九九八十一难"
},
"sort" : [
1000000
]
},
{
"_index" : "book",
"_type" : "novel",
"_id" : "ftqNN3oBwiPJbe8gGw2O",
"_score" : null,
"_source" : {
"name" : "西游记",
"author" : "吴承恩",
"count" : 1000000,
"on-sale" : "2000-01-10",
"descr" : "九九八十一难"
},
"sort" : [
1000000
]
},
{
"_index" : "book",
"_type" : "novel",
"_id" : "KPwBPXoBpklwLylkcN6G",
"_score" : null,
"_source" : {
"name" : "西游记",
"author" : "吴承恩",
"count" : 1000000,
"on-sale" : "2000-01-10",
"descr" : "九九八十一难"
},
"sort" : [
1000000
]
},
{
"_index" : "book",
"_type" : "novel",
"_id" : "gtqNN3oBwiPJbe8gRw1M",
"_score" : null,
"_source" : {
"name" : "西游记",
"author" : "吴承恩",
"count" : 1000000,
"on-sale" : "2000-01-10",
"descr" : "九九八十一难"
},
"sort" : [
1000000
]
},
{
"_index" : "book",
"_type" : "novel",
"_id" : "g9qNN3oBwiPJbe8gSg3s",
"_score" : null,
"_source" : {
"name" : "西游记",
"author" : "吴承恩",
"count" : 1000000,
"on-sale" : "2000-01-10",
"descr" : "九九八十一难"
},
"sort" : [
1000000
]
},
{
"_index" : "book",
"_type" : "novel",
"_id" : "gNqNN3oBwiPJbe8gPw0j",
"_score" : null,
"_source" : {
"name" : "西游记",
"author" : "吴承恩",
"count" : 1000000,
"on-sale" : "2000-01-10",
"descr" : "九九八十一难"
},
"sort" : [
1000000
]
}
]
}
}
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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
- 删除scroll在ES上下文中的数据,路径结尾携带scroll_id即可 (DELETE /_search/DnF1ZXJ5VGhlbkZldGNoBQAAAAAAANxPFnFmc2VvM2pmU1JhWjhwNGVTdUpLQ3cAAAAAAADcUBZxZnNlbzNqZlNSYVo4cDRlU3VKS0N3AAAAAAAA3FEWcWZzZW8zamZTUmFaOHA0ZVN1SktDdwAAAAAAANxSFnFmc2VvM2pmU1JhWjhwNGVTdUpLQ3cAAAAAAADcUxZxZnNlbzNqZlNSYVo4cDRlU3VKS0N3)
- 响应
{
"succeeded" : true,
"num_freed" : 5
}
1
2
3
4
2
3
4
# delete-by-query
- 根据term,match等查询方式去删除大量的文档
- Ps: 如果需要删除的内容是index下的大部分数据,则推荐创建一个全新的index,将保留的文档内容添加到全新的索引中。
- delete-by-query (POST /book/novel/_delete_by_query)
- 请求
{
"query":{
"range":{
"count":{
"gte": 1000001
}
}
}
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
- 响应
{
"took" : 13,
"timed_out" : false,
"total" : 1,
"deleted" : 1,
"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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17