Elastic实战Rest查询

索引信息

查看索引信息

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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
[root@localhost elasticsearch-6.0.0 ]$ curl -XGET "http://127.0.0.1:9200/db_test?pretty=true"
{
"db_test" : {
"aliases" : { },
"mappings" : {
"tb_opus" : {
"properties" : {
"aliasName" : {
"type" : "text",
"fields" : {
"origin" : {
"type" : "keyword"
},
"pinyin" : {
"type" : "text",
"term_vector" : "with_positions_offsets",
"analyzer" : "ik_pinyin_analyzer"
},
"suggest" : {
"type" : "completion",
"analyzer" : "ik_max_word",
"preserve_separators" : true,
"preserve_position_increments" : true,
"max_input_length" : 50
}
},
"analyzer" : "ik_max_word"
},
"appId" : {
"type" : "long"
},
"author" : {
"type" : "text",
"fields" : {
"origin" : {
"type" : "keyword"
},
"pinyin" : {
"type" : "text",
"term_vector" : "with_positions_offsets",
"analyzer" : "ik_pinyin_analyzer"
},
"suggest" : {
"type" : "completion",
"analyzer" : "ik_max_word",
"preserve_separators" : true,
"preserve_position_increments" : true,
"max_input_length" : 50
}
},
"analyzer" : "ik_smart"
},
"createTime" : {
"type" : "date"
},
"enabled" : {
"type" : "integer"
},
"favoriteTotal" : {
"type" : "long"
},
"fingerprint" : {
"type" : "keyword"
},
"id" : {
"type" : "long"
},
"images" : {
"type" : "keyword"
},
"introduce" : {
"type" : "text",
"fields" : {
"pinyin" : {
"type" : "text",
"boost" : 10.0,
"term_vector" : "with_positions_offsets",
"analyzer" : "ik_pinyin_analyzer"
}
},
"analyzer" : "ik_smart"
},
"issueTime" : {
"type" : "long"
},
"opusName" : {
"type" : "text",
"fields" : {
"origin" : {
"type" : "keyword"
},
"pinyin" : {
"type" : "text",
"term_vector" : "with_positions_offsets",
"analyzer" : "ik_pinyin_analyzer"
},
"suggest" : {
"type" : "completion",
"analyzer" : "ik_max_word",
"preserve_separators" : true,
"preserve_position_increments" : true,
"max_input_length" : 50
}
},
"analyzer" : "ik_max_word"
},
"score" : {
"type" : "double"
},
"sort" : {
"type" : "long"
},
"tags" : {
"type" : "text",
"fields" : {
"origin" : {
"type" : "keyword"
},
"pinyin" : {
"type" : "text",
"term_vector" : "with_positions_offsets",
"analyzer" : "ik_pinyin_analyzer"
},
"suggest" : {
"type" : "completion",
"analyzer" : "ik_max_word",
"preserve_separators" : true,
"preserve_position_increments" : true,
"max_input_length" : 50
}
},
"analyzer" : "ik_max_word"
},
"type" : {
"type" : "integer"
},
"updateTime" : {
"type" : "date"
},
"userId" : {
"type" : "long"
},
"uuid" : {
"type" : "keyword"
},
"viewTotal" : {
"type" : "long"
}
}
}
},
"settings" : {
"index" : {
"number_of_shards" : "1",
"provided_name" : "db_test",
"creation_date" : "1514353694378",
"analysis" : {
"filter" : {
"mix_pinyin" : {
"lowercase" : "true",
"padding_char" : " ",
"first_letter" : "prefix",
"keep_original" : "true",
"remove_duplicated_term" : "true",
"type" : "pinyin",
"keep_full_pinyin" : "true"
}
},
"analyzer" : {
"ik_pinyin_analyzer" : {
"filter" : [
"mix_pinyin",
"word_delimiter"
],
"type" : "custom",
"tokenizer" : "ik_max_word"
},
"default" : {
"tokenizer" : "ik_max_word"
}
}
},
"number_of_replicas" : "1",
"uuid" : "seg7yr55R121l8tRW3r7uA",
"version" : {
"created" : "6000099"
}
}
}
}
}

搜索示例

多主键查询

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
[root@localhost elasticsearch-6.0.0 ]$ curl -XPOST "http://127.0.0.1:9200/db_test/tb_opus/_search?pretty=true" -H 'Content-Type: application/json' -d'
{
"_source": {
"include": ["id", "opusName"]
},
"query": {
"ids": {
"type": "tb_opus",
"values": ["1", "2", "3"]
}
}
}
}'

{
"took" : 7,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 1.0,
"hits" : [
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"opusName" : "秒速5厘米",
"id" : 1
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"opusName" : "致我们终将逝去的青春",
"id" : 2
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"opusName" : "对不起,我爱你",
"id" : 3
}
}
]
}
}

term查询未分词的指定列

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
[root@localhost elasticsearch-6.0.0 ]$ curl -XPOST "http://127.0.0.1:9200/db_test/tb_opus/_search?pretty=true" -H 'Content-Type: application/json' -d'
{
"_source": {
"include": ["id", "author", "opusName"]
},
"query": {
"term": {
"author.origin": ["八月长安"]
}
}
}'

{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 5,
"max_score" : 4.801036,
"hits" : [
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "558",
"_score" : 4.801036,
"_source" : {
"author" : "八月长安",
"opusName" : "你好,旧时光:陪你到青春最后",
"id" : 558
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "661",
"_score" : 4.801036,
"_source" : {
"author" : "八月长安",
"opusName" : "最好的我们",
"id" : 661
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "662",
"_score" : 4.801036,
"_source" : {
"author" : "八月长安",
"opusName" : "橘生淮南·暗恋",
"id" : 662
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "663",
"_score" : 4.801036,
"_source" : {
"author" : "八月长安",
"opusName" : "暗恋",
"id" : 663
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "664",
"_score" : 4.801036,
"_source" : {
"author" : "八月长安",
"opusName" : "被偷走的那五年",
"id" : 664
}
}
]
}
}

terms查询多个未分词的指定列

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
114
115
116
[root@localhost elasticsearch-6.0.0 ]$ curl -XPOST "http://127.0.0.1:9200/db_test/tb_opus/_search?pretty=true" -H 'Content-Type: application/json' -d'
{
"_source": {
"include": ["id", "author", "opusName"]
},
"query": {
"terms": {
"author.origin": ["八月长安", "籽月"]
}
}
}'

{
"took" : 8,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 8,
"max_score" : 1.0,
"hits" : [
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "558",
"_score" : 1.0,
"_source" : {
"author" : "八月长安",
"opusName" : "你好,旧时光:陪你到青春最后",
"id" : 558
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "661",
"_score" : 1.0,
"_source" : {
"author" : "八月长安",
"opusName" : "最好的我们",
"id" : 661
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "662",
"_score" : 1.0,
"_source" : {
"author" : "八月长安",
"opusName" : "橘生淮南·暗恋",
"id" : 662
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "663",
"_score" : 1.0,
"_source" : {
"author" : "八月长安",
"opusName" : "暗恋",
"id" : 663
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "664",
"_score" : 1.0,
"_source" : {
"author" : "八月长安",
"opusName" : "被偷走的那五年",
"id" : 664
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "668",
"_score" : 1.0,
"_source" : {
"author" : "籽月",
"opusName" : "夏有乔木:雅望天堂",
"id" : 668
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "669",
"_score" : 1.0,
"_source" : {
"author" : "籽月",
"opusName" : "初晨,是我故意忘记你",
"id" : 669
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "670",
"_score" : 1.0,
"_source" : {
"author" : "籽月",
"opusName" : "月光满满预见你",
"id" : 670
}
}
]
}
}

前缀查询

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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
[root@localhost elasticsearch-6.0.0 ]$ curl -XPOST "http://127.0.0.1:9200/db_test/tb_opus/_search?pretty=true" -H 'Content-Type: application/json' -d'
{
"_source": {
"include": ["id", "author", "opusName"]
},
"query": {
"prefix": {
"author.origin": "九"
}
}
}'

{
"took" : 8,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 13,
"max_score" : 1.0,
"hits" : [
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "548",
"_score" : 1.0,
"_source" : {
"author" : "九夜茴",
"opusName" : "曾少年",
"id" : 548
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "549",
"_score" : 1.0,
"_source" : {
"author" : "九夜茴",
"opusName" : "花开半夏",
"id" : 549
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "550",
"_score" : 1.0,
"_source" : {
"author" : "九夜茴",
"opusName" : "匆匆那年",
"id" : 550
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "551",
"_score" : 1.0,
"_source" : {
"author" : "九夜茴,林特特,韩梅梅等",
"opusName" : "世界那么大,我想去看看",
"id" : 551
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "8",
"_score" : 1.0,
"_source" : {
"author" : "九把刀",
"opusName" : "那些年我们一起追的女孩",
"id" : 8
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "765",
"_score" : 1.0,
"_source" : {
"author" : "九鹭非香",
"opusName" : "百界歌",
"id" : 765
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "761",
"_score" : 1.0,
"_source" : {
"author" : "九鹭非香",
"opusName" : "苍兰诀",
"id" : 761
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "762",
"_score" : 1.0,
"_source" : {
"author" : "九鹭非香",
"opusName" : "司命",
"id" : 762
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "763",
"_score" : 1.0,
"_source" : {
"author" : "九鹭非香",
"opusName" : "你在遥远星空中",
"id" : 763
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "764",
"_score" : 1.0,
"_source" : {
"author" : "九鹭非香",
"opusName" : "与凤行",
"id" : 764
}
}
]
}
}

通配符查询(wildcard)

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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
[root@localhost elasticsearch-6.0.0 ]$ curl -XPOST "http://127.0.0.1:9200/db_test/tb_opus/_search?pretty=true" -H 'Content-Type: application/json' -d'
{
"_source": {
"include": ["id", "author", "opusName"]
},
"query": {
"wildcard": {
"author.origin": "*月*"
}
}
}'

{
"took" : 11,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 24,
"max_score" : 1.0,
"hits" : [
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "558",
"_score" : 1.0,
"_source" : {
"author" : "八月长安",
"opusName" : "你好,旧时光:陪你到青春最后",
"id" : 558
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "613",
"_score" : 1.0,
"_source" : {
"author" : "四月莲",
"opusName" : "玻璃青春",
"id" : 613
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "661",
"_score" : 1.0,
"_source" : {
"author" : "八月长安",
"opusName" : "最好的我们",
"id" : 661
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "161",
"_score" : 1.0,
"_source" : {
"author" : "沧月",
"opusName" : "忘川",
"id" : 161
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "610",
"_score" : 1.0,
"_source" : {
"author" : "菲雨月",
"opusName" : "青春,终究被搁浅",
"id" : 610
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "662",
"_score" : 1.0,
"_source" : {
"author" : "八月长安",
"opusName" : "橘生淮南·暗恋",
"id" : 662
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "663",
"_score" : 1.0,
"_source" : {
"author" : "八月长安",
"opusName" : "暗恋",
"id" : 663
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "664",
"_score" : 1.0,
"_source" : {
"author" : "八月长安",
"opusName" : "被偷走的那五年",
"id" : 664
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "668",
"_score" : 1.0,
"_source" : {
"author" : "籽月",
"opusName" : "夏有乔木:雅望天堂",
"id" : 668
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "669",
"_score" : 1.0,
"_source" : {
"author" : "籽月",
"opusName" : "初晨,是我故意忘记你",
"id" : 669
}
}
]
}
}

正则查询(Regex)

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
[root@localhost elasticsearch-6.0.0 ]$  curl -XPOST "http://127.0.0.1:9200/db_test/tb_opus/_search?pretty=true" -H 'Content-Type: application/json' -d'
{
"_source": {
"include": ["id", "author", "opusName"]
},
"query": {
"regexp": {
"author.origin": "[沧|籽]月"
}
}
}'

{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 5,
"max_score" : 1.0,
"hits" : [
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "161",
"_score" : 1.0,
"_source" : {
"author" : "沧月",
"opusName" : "忘川",
"id" : 161
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "668",
"_score" : 1.0,
"_source" : {
"author" : "籽月",
"opusName" : "夏有乔木:雅望天堂",
"id" : 668
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "669",
"_score" : 1.0,
"_source" : {
"author" : "籽月",
"opusName" : "初晨,是我故意忘记你",
"id" : 669
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "670",
"_score" : 1.0,
"_source" : {
"author" : "籽月",
"opusName" : "月光满满预见你",
"id" : 670
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "452",
"_score" : 1.0,
"_source" : {
"author" : "沧月",
"opusName" : "羽·苍穹之烬",
"id" : 452
}
}
]
}
}

span查询

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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
[root@localhost elasticsearch-6.0.0 ]$ curl -XPOST "http://127.0.0.1:9200/db_test/tb_opus/_search?pretty=true" -H 'Content-Type: application/json' -d'
{
"_source": {
"include": ["id", "author", "opusName"]
},
"query": {
"span_or": {
"clauses":[
{"span_term" : {"author": "长安"}},
{"span_term" : {"author": "笛"}}
]
}
}
}'

{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 17,
"max_score" : 9.539912,
"hits" : [
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "558",
"_score" : 9.539912,
"_source" : {
"author" : "八月长安",
"opusName" : "你好,旧时光:陪你到青春最后",
"id" : 558
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "661",
"_score" : 9.539912,
"_source" : {
"author" : "八月长安",
"opusName" : "最好的我们",
"id" : 661
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "662",
"_score" : 9.539912,
"_source" : {
"author" : "八月长安",
"opusName" : "橘生淮南·暗恋",
"id" : 662
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "663",
"_score" : 9.539912,
"_source" : {
"author" : "八月长安",
"opusName" : "暗恋",
"id" : 663
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "664",
"_score" : 9.539912,
"_source" : {
"author" : "八月长安",
"opusName" : "被偷走的那五年",
"id" : 664
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "443",
"_score" : 9.539912,
"_source" : {
"author" : "笛安",
"opusName" : "西决",
"id" : 443
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "444",
"_score" : 9.539912,
"_source" : {
"author" : "笛安",
"opusName" : "姐姐的丛林",
"id" : 444
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "445",
"_score" : 9.539912,
"_source" : {
"author" : "笛安",
"opusName" : "一个女人的悲欢离合:东霓",
"id" : 445
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "446",
"_score" : 9.539912,
"_source" : {
"author" : "笛安",
"opusName" : "南方有令秧",
"id" : 446
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "447",
"_score" : 9.539912,
"_source" : {
"author" : "笛安",
"opusName" : "妩媚航班",
"id" : 447
}
}
]
}
}

匹配查询

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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
[root@localhost elasticsearch-6.0.0 ]$ curl -XPOST "http://127.0.0.1:9200/db_test/tb_opus/_search?pretty=true" -H 'Content-Type: application/json' -d'
{
"_source": {
"include": ["id", "tags"]
},
"query": {
"match": {
"tags": {
"query": "青春 爱情",
"operator": "AND"
}
}
}
}'

{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 6,
"max_score" : 6.3930693,
"hits" : [
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "2",
"_score" : 6.3930693,
"_source" : {
"id" : 2,
"tags" : [
"爱情",
"青春"
]
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "8",
"_score" : 6.3930693,
"_source" : {
"id" : 8,
"tags" : [
"爱情",
"青春"
]
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "24",
"_score" : 6.3930693,
"_source" : {
"id" : 24,
"tags" : [
"爱情",
"青春"
]
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "114",
"_score" : 6.3930693,
"_source" : {
"id" : 114,
"tags" : [
"青春",
"爱情"
]
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "11",
"_score" : 5.4129186,
"_source" : {
"id" : 11,
"tags" : [
"动画",
"青春",
"爱情"
]
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "1",
"_score" : 4.142658,
"_source" : {
"id" : 1,
"tags" : [
"新海诚",
"动画",
"爱情",
"青春"
]
}
}
]
}
}
[root@localhost elasticsearch-6.0.0 ]$ curl -XPOST "http://127.0.0.1:9200/db_test/tb_opus/_search?pretty=true" -H 'Content-Type: application/json' -d'
{
"_source": {
"include": ["id", "tags"]
},
"query": {
"match": {
"tags": {
"query": "青春 爱情",
"operator": "OR"
}
}
}
}'

{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 65,
"max_score" : 6.3930693,
"hits" : [
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "2",
"_score" : 6.3930693,
"_source" : {
"id" : 2,
"tags" : [
"爱情",
"青春"
]
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "8",
"_score" : 6.3930693,
"_source" : {
"id" : 8,
"tags" : [
"爱情",
"青春"
]
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "24",
"_score" : 6.3930693,
"_source" : {
"id" : 24,
"tags" : [
"爱情",
"青春"
]
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "114",
"_score" : 6.3930693,
"_source" : {
"id" : 114,
"tags" : [
"青春",
"爱情"
]
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "11",
"_score" : 5.4129186,
"_source" : {
"id" : 11,
"tags" : [
"动画",
"青春",
"爱情"
]
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "1",
"_score" : 4.142658,
"_source" : {
"id" : 1,
"tags" : [
"新海诚",
"动画",
"爱情",
"青春"
]
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "409",
"_score" : 3.6897516,
"_source" : {
"id" : 409,
"tags" : [
"励志",
"成长",
"青春"
]
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "522",
"_score" : 2.4852023,
"_source" : {
"id" : 522,
"tags" : [
"爱情"
]
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "12",
"_score" : 2.4852023,
"_source" : {
"id" : 12,
"tags" : [
"爱情"
]
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "13",
"_score" : 2.4852023,
"_source" : {
"id" : 13,
"tags" : [
"爱情"
]
}
}
]
}
}

匹配查询多个field

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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
[root@localhost elasticsearch-6.0.0 ]$ curl -XPOST "http://127.0.0.1:9200/db_test/tb_opus/_search?pretty=true" -H 'Content-Type: application/json' -d'
{
"_source": {
"include": ["id", "tags", "opusName"]
},
"query": {
"multi_match": {
"fields": ["tags", "opusName"],
"query": "青春 爱情",
"operator": "AND"

}
}
}'

{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 7,
"max_score" : 6.3930693,
"hits" : [
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "2",
"_score" : 6.3930693,
"_source" : {
"opusName" : "致我们终将逝去的青春",
"id" : 2,
"tags" : [
"爱情",
"青春"
]
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "8",
"_score" : 6.3930693,
"_source" : {
"opusName" : "那些年我们一起追的女孩",
"id" : 8,
"tags" : [
"爱情",
"青春"
]
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "24",
"_score" : 6.3930693,
"_source" : {
"opusName" : "带我去远方",
"id" : 24,
"tags" : [
"爱情",
"青春"
]
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "114",
"_score" : 6.3930693,
"_source" : {
"opusName" : "匆匆那年",
"id" : 114,
"tags" : [
"青春",
"爱情"
]
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "11",
"_score" : 5.4129186,
"_source" : {
"opusName" : "言叶之庭",
"id" : 11,
"tags" : [
"动画",
"青春",
"爱情"
]
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "568",
"_score" : 5.225136,
"_source" : {
"opusName" : "致青春:我们曾经向往的爱情",
"id" : 568,
"tags" : [
"纯爱"
]
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "1",
"_score" : 4.142658,
"_source" : {
"opusName" : "秒速5厘米",
"id" : 1,
"tags" : [
"新海诚",
"动画",
"爱情",
"青春"
]
}
}
]
}
}

前缀匹配查询

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
[root@localhost elasticsearch-6.0.0 ]$  curl -XPOST "http://127.0.0.1:9200/db_test/tb_opus/_search?pretty=true" -H 'Content-Type: application/json' -d'
{
"_source": {
"include": ["id", "opusName"]
},
"query": {
"match_phrase_prefix": {
"opusName": "如果"
}
}
}'

{
"took" : 6,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 5,
"max_score" : 5.3439174,
"hits" : [
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "647",
"_score" : 5.3439174,
"_source" : {
"opusName" : "如果还有爱",
"id" : 647
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "626",
"_score" : 4.80206,
"_source" : {
"opusName" : "如果蜗牛有爱情",
"id" : 626
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "683",
"_score" : 4.80206,
"_source" : {
"opusName" : "如果巴黎不快乐",
"id" : 683
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "645",
"_score" : 4.3599715,
"_source" : {
"opusName" : "如果爱犯了错",
"id" : 645
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "547",
"_score" : 2.3843746,
"_source" : {
"opusName" : "如果你曾奋不顾身爱上一个人",
"id" : 547
}
}
]
}
}

query_string查询

查询关键词同时包含”蜗牛”和”爱情” 并且评分不小于80 分类号为30的文档

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
[root@localhost elasticsearch-6.0.0 ]$ curl -XPOST "http://127.0.0.1:9200/db_test/tb_opus/_search?pretty=true" -H 'Content-Type: application/json' -d'
{
"_source": {
"include": ["id", "opusName", "tags", "score", "type"]
},
"query": {
"query_string": {
"query": "蜗牛 爱情 -score:{* TO 80} type:30",
"fields":[
"opusName^5", "tags"
],
"default_operator": "AND"
}
}
}'

{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 52.146034,
"hits" : [
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "626",
"_score" : 52.146034,
"_source" : {
"score" : 91,
"opusName" : "如果蜗牛有爱情",
"id" : 626,
"type" : 30,
"tags" : [
"都市",
"丁墨"
]
}
}
]
}
}

区间查询

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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
[root@localhost elasticsearch-6.0.0 ]$ curl -XPOST "http://127.0.0.1:9200/db_test/tb_opus/_search?pretty=true" -H 'Content-Type: application/json' -d'
{
"_source": {
"include": ["id", "score"]
},
"from": 0,
"size": 5,
"query": {
"range": {
"score": {
"from" : 13,
"to": 15,
"include_lower": true,
"include_upper": false
}
}
}
}'

{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 11,
"max_score" : 1.0,
"hits" : [
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "630",
"_score" : 1.0,
"_source" : {
"score" : 13,
"id" : 630
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "647",
"_score" : 1.0,
"_source" : {
"score" : 14,
"id" : 647
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "659",
"_score" : 1.0,
"_source" : {
"score" : 13,
"id" : 659
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "529",
"_score" : 1.0,
"_source" : {
"score" : 14,
"id" : 529
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "697",
"_score" : 1.0,
"_source" : {
"score" : 14,
"id" : 697
}
}
]
}
}
```

bool查询

``` bash
[root@localhost elasticsearch-6.0.0 ]$ curl -XPOST "http://127.0.0.1:9200/db_test/tb_opus/_search?pretty=true" -H 'Content-Type: application/json' -d'
{
"_source": {
"include": ["id", "opusName", "tags", "score"]
},
"query": {
"bool": {
"must": [{
"term" : {"opusName":"爱情"}
}],
"must_not": [{
"range": {
"score": { "from": 80, "to": 100}
}
}],
"should": [{
"term" : {"tags":"青春"}
}]
}
}
}'

{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 5,
"max_score" : 5.156911,
"hits" : [
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "468",
"_score" : 5.156911,
"_source" : {
"score" : 55,
"opusName" : "爱情的开关",
"id" : 468,
"tags" : [
"爱情",
"匪我思存"
]
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "504",
"_score" : 5.156911,
"_source" : {
"score" : 39,
"opusName" : "因为爱情",
"id" : 504,
"tags" : [
"家庭"
]
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "275",
"_score" : 3.8527093,
"_source" : {
"score" : 54,
"opusName" : "你的味蕾,我的爱情",
"id" : 275,
"tags" : [ ]
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "568",
"_score" : 3.5531719,
"_source" : {
"score" : 77,
"opusName" : "致青春:我们曾经向往的爱情",
"id" : 568,
"tags" : [
"纯爱"
]
}
},
{
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "688",
"_score" : 3.5531719,
"_source" : {
"score" : 69,
"opusName" : "蜗婚:距离爱情一平米",
"id" : 688,
"tags" : [
"婚恋",
"白槿湖"
]
}
}
]
}
}

分页排序查询

分页查询所有数据,结果以更新时间降序、主键编号升序排列

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@icloud-store ~]#  curl -XPOST "http://127.0.0.1:9200/db_test/tb_opus/_search?pretty=true" -H 'Content-Type: application/json' -d'
{
"query": {
"match_all": {

}
},
"from": 0,
"size": 3,
"sort": {
"updateTime": {
"order": "desc"
},
"id": {
"order": "asc"
}
}
}'

关键词筛选查询

查询与宫崎骏相关的作品

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
[root@icloud-store ~]# curl -XPOST "http://127.0.0.1:9200/db_test/tb_opus/_search?pretty=true" -H 'Content-Type: application/json' -d'
{
"query": {
"query_string": {
"query": "宫崎骏"
}
},
"highlight": {
"pre_tags": [
"<b>"
],
"post_tags": [
"</b>"
],
"fields": {
"opusName": {
"order": "score"
},
"tags": {
"order": "score"
},
"introduce": {
"order": "score"
}
}
},
"from": 0,
"size": 3
}'

搜索推荐查询

根据关键词进行搜索推荐(前缀匹配)

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
[root@localhost elasticsearch-6.0.0 ]$ curl -XPOST "http://127.0.0.1:9200/db_test/tb_opus/_search?pretty=true" -H 'Content-Type: application/json' -d'
{
"_source": "opusName.suggest",
"suggest": {
"product-suggest" : {
"text" : "阿狸",
"completion" : {
"field" : "opusName.suggest",
"fuzzy" : {
"fuzziness" : 2
},
"size": 5
}
}
}
}'

{
"took" : 8,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 0,
"max_score" : 0.0,
"hits" : [ ]
},
"suggest" : {
"product-suggest" : [
{
"text" : "阿狸",
"offset" : 0,
"length" : 2,
"options" : [
{
"text" : "阿狸·梦之城堡",
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "346",
"_score" : 5.0,
"_source" : { }
},
{
"text" : "阿狸·永远站",
"_index" : "db_test",
"_type" : "tb_opus",
"_id" : "353",
"_score" : 5.0,
"_source" : { }
}
]
}
]
}
}