Elasticsearch Learning (II) ---- Search

Elasticsearch
1.query String Search
. Search all 1.1
// 1.
GET HTTP: // ip: 9200 / the Test / the Test / _search
result:
{
"Took": 86, # time-consuming: MS
"TIMED_OUT": false, # whether a timeout
"_shards": {# data stored on the five main fragments
"Total":. 5,
"successful":. 5,
"Skipped": 0,
"failed": 0
},
"Hits": {# matches
" total ": 3, # query to three Document
" max_score ":. 1, # match score of correlation: the higher the score the more relevant
" Hits ": [
{
" the _index ":" Test ", index # index
" _type ": "the Test", # of the type
"_id": "2", the above mentioned id #: The only
"_score": 1,Match score # of correlation: the higher the score the more relevant
"_source": {json data stored #
"first_name": "leach", Field #
"last_name": "cuicui",
"age": 18,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}
},
{
"_index": "test",
"_type": "test",
"_id": "1",
"_score": 1,
"_source": {
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}
},
{
"_index": "test",
"_type": "test",
"_id": "AWrVpGsO0WDJvaOeQOjd",
"_score": 1,
"_source": {
"first_name": "小翠",
"last_name": "cui",
"age": 18,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}
}
]
}
}
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 is
44 is
45
46 is
47
48
49
50
51 is
52 is
53 is
54 is
55
56 is
57 is
58
59
60
61 is
62 is
63 is
64
65
66
67
1.2 In the field contains about climbing and descending characters of the query may be a plurality of sorted age field, separated by commas:
2. //
GET HTTP: // ip: 9200 / the Test / the Test / _search q = the About:? = the Sort-climbing It & Age: desc,. price: desc
GET HTTP: // ip: 9200 / the Test / the Test / _search q = the About? : & Sort = Age-climbing: desc
{
"Took": 10,
"TIMED_OUT": to false,
"_shards": {
"Total":. 5,
"successful":. 5,
"Skipped": 0,
"failed":0
},
"hits": {
"total": 3,
"max_score": null,
"hits": [
{
"_index": "test",
"_type": "test",
"_id": "1",
"_score": null,
"_source": {
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
},
"sort": [ #排序字段的值
25
]
},
{
"_index": "test",
"_type": "test",
"_id": "2",
"_score": null,
"_source": {
"first_name": "小翠",
"last_name": "cuicui",
"age": 18,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
},
"sort": [
18
]
},
{
"_index": "test",
"_type": "test",
"_id": "AWrVpGsO0WDJvaOeQOjd",
"_score": null,
"_source": {
"first_name": "小翠",
"last_name": "cui",
"age": 18,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
},
"sort": [
18
]
}
]
}
}
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
2.query DSL
2.1.搜索全部
// 1.
POST http://ip:9200/test/test/_search
语法:
{
"query":{
"match_all":{}
}
}
结果:
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 5,
"max_score": 1,
"hits": [
{
"_index": "test",
"_type": "test",
"_id": "2",
"_score": 1,
"_source": {
"first_name": "小翠",
"last_name": "cuicui",
"age": 18,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}
},
{
"_index": "test",
"_type": "test",
"_id": "4",
"_score": 1,
"_source": {
"first_name": "小翠",
"last_name": "cui",
"age": 18,
"price": 15000,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}
},
{
"_index": "test",
"_type": "test",
"_id": "1",
"_score": 1,
"_source": {
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}
},
{
"_index": "test",
"_type": "test",
"_id": "AWrVpGsO0WDJvaOeQOjd",
"_score": 1,
"_source": {
"first_name": "小翠",
"last_name": "cui",
"age": 18,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}
},
{
"_index": "test",
"_type": "test",
"_id": "3",
"_score": 1,
"_source": {
"first_name": "小雪",
"last_name": "xiaoxue",
"age": 20,
"about": "climbing",
"interests": [
"dancing",
"music"
]
}
}
]
}
}
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
2.2 In the field contains about climbing and descending characters of the query may be a plurality of sorted age field, separated by commas
// 1.
the POST HTTP: // IP: 9200 / test / test / _search
syntax:
{
"Query": {
"match": {
"About": "-climbing"
}
},
"Sort": [
{
"Age": "desc"
}
]
}

结果:
{
"took": 115,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 5,
"max_score": null,
"hits": [
{
"_index": "test",
"_type": "test",
"_id": "1",
"_score": null,
"_source": {
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
},
"sort": [
25
]
},
{
"_index": "test",
"_type": "test",
"_id": "3",
"_score": null,
"_source": {
"first_name": "小雪",
"last_name": "xiaoxue",
"age": 20,
"about": "climbing",
"interests": [
"dancing",
"music"
]
},
"sort": [
20
]
},
{
"_index": "test",
"_type": "test",
"_id": "2",
"_score": null,
"_source": {
"first_name": "小翠",
"last_name": "cuicui",
"age": 18,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
},
"sort": [
18
]
},
{
"_index": "test",
"_type": "test",
"_id": "4",
"_score": null,
"_source": {
"first_name": "小翠",
"last_name": "cui",
"age": 18,
"price": 15000,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
},
"sort": [
18
]
},
{
"_index": "test",
"_type": "test",
"_id": "AWrVpGsO0WDJvaOeQOjd",
"_score": null,
"_source": {
"first_name": "小翠",
"last_name": "cui",
"age": 18,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
},
"sort": [
18
]
}
]
}
}
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
1 16
117
1 18
119
120
121
122
123
124
125
126
127
128
129
2.3 paged data.
// 1.
the POST HTTP: // IP: 9200 / Test / test / _search
syntax:
{
"query": {
"MATCH_ALL": {} Search #
},
"from": 0, # 0 starts from the first few data: the first
"size": 1 # show several data
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 2.4 shows the specified Filed
// 1.
the POST HTTP: // IP: 9200 / Test / Test / _search
Syntax:
{
"Query": {
"MATCH_ALL": {}
},
"_Source": [
"FIRST_NAME",
"Age"
]
}

结果:
{
"took": 8,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 5,
"max_score": 1,
"hits": [
{
"_index": "test",
"_type": "test",
"_id": "3",
"_score": 1,
"_source": {
"first_name": "小雪",
"age": 20
}
}
]
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
. 17
18 is
. 19
20
21 is
22 is
23 is
24
25
26 is
27
28
29
30
31 is
32
33 is
34 is
35
36
37 [
38 is
39
40
3.query filter
. 3.1 plurality of query conditions: about field must contain "climbing"; age greater than 20 years
// 1 .
the POST HTTP: // IP: 9200 / Test / Test / _search
syntax:
{
"Query": {
"BOOL": {
"MUST": {
"match": {
"About": "-climbing"
}
},
"filter ": {
" Range ": {
" Age ": {
" gt ":20
}
}
}
}
}
}

结果
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.26742277,
"hits": [
{
"_index": "test",
"_type": "test",
"_id": "1",
"_score": 0.26742277,
"_source": {
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}
}
]
}
}
1
2
3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
23 is
24
25
26 is
27
28
29
30
31 is
32
33 is
34 is
35
36
37 [
38 is
39
40
41 is
42 is
43 is
44 is
45
46 is
47
48
49
50
51 is
52 is
53 is
54
55
3.2 multiple query conditions: must, should, must_not
Http://47.99.74.228:9200/test/test/_search the POST
{
"Query": {
"BOOL": {
"MUST": {# must match
"match": {
"FIRST_NAME": "leach"
}
} ,
"Should": {# matches, may not match the
"match": {
"last_name": "Xue"
}
},
"must_not": {# not necessarily match the
"match": {
"last_name": "CUI"
}
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Search the Test-4.full
4.1 full-text search.
// 1.
POST HTTP: // ip: 9200 / the Test / the Test / _search
syntax:
{
"Query": {
"match": {
"the About": "Go-climbing It"
}
}
}
analysis: es about this filed will be disassembled into each word (term), create an inverted index, each corresponding to a respective term document_id
result:
{
"Took":. 8,
"TIMED_OUT": to false,
"_shards": {
"Total":. 5,
"successful":. 5,
"Skipped": 0,
"failed": 0
},
"Hits": {
"Total":. 7,
"max_score": 0.7447149,
"Hits": [
{
" _index ":"test",
"_type": "test",
"_id": "2",
"_score": 0.7447149,
"_source": {
"first_name": "小翠",
"last_name": "cuicui",
"age": 18,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}
},
{
"_index": "test",
"_type": "test",
"_id": "4",
"_score": 0.7447149,
"_source": {
"first_name": "小翠",
"last_name": "cui",
"age": 18,
"price": 15000,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}
},
{
"_index": "test",
"_type": "test",
"_id": "1",
"_score": 0.61562645,
"_source": {
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}
},
{
"_index": "test",
"_type": "test",
"_id": "AWrVpGsO0WDJvaOeQOjd",
"_score": 0.61562645,
"_source": {
"first_name": "小翠",
"last_name": "cui",
"age": 18,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}
},
{
"_index": "test",
"_type": "test",
"_id": "5",
"_score": 0.2876821,
"_source": {
"first_name": "花花",
"last_name": "huahau",
"age": 16,
"price": 20000,
"about": "climbing",
"interests": [
"sports",
"music"
]
}
},
{
"_index": "test",
"_type": "test",
"_id": "7",
"_score": 0.25759193,
"_source": {
"about": "go"
}
},
{
"_index": "test",
"_type": "test",
"_id": "3",
"_score": 0.25759193,
"_source": {
"first_name": "小雪",
"last_name": "xiaoxue",
"age": 20,
"about": "climbing",
"interests": [
"dancing",
"music"
]
}
}
]
}
}
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
1 16
117
1 18
119
120
121
122
123
124
125
126
127
128
129
130.
131 is
132
133
134
135
4.2 phrase search: matching phrase
// 1.
the POST HTTP: // IP: 9200 / test / test / _search
syntax:
{
"Query": {
"match_phrase": {
"About": "Rock-climbing"
}
}
}

结果:
{
"took": 20,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 0.9748371,
"hits": [
{
"_index": "test",
"_type": "test",
"_id": "1",
"_score": 0.9748371,
"_source": {
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}
},
{
"_index": "test",
"_type": "test",
"_id": "2",
"_score": 0.7447149,
"_source": {
"first_name": "小翠",
"last_name": "cuicui",
"age": 18,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}
},
{
"_index": "test",
"_type": "test",
"_id": "4",
"_score": 0.7447149,
"_source": {
"first_name": "小翠",
"last_name": "cui",
"age": 18,
"price": 15000,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}
},
{
"_index": "test",
"_type": "test",
"_id": "AWrVpGsO0WDJvaOeQOjd",
"_score": 0.6156264,
"_source": {
"first_name": "小翠",
"last_name": "cui",
"age": 18,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}
}
]
}
}
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 is
92
93
5.highlight Search
5.1 highlighted keywords.
@ 1
Syntax:
{
"Query": {
"match": {
"About": "-climbing" # keywords
}
},
"highlight ": {
" fields ": {
" About ": {} # http://www.my516.com field
}
}
}

结果:
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 6,
"max_score": 0.48741856,
"hits": [
{
"_index": "test",
"_type": "test",
"_id": "1",
"_score": 0.48741856,
"_source": {
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
},
"highlight": {
"about": [
"I love to go rock <em>climbing</em>" # <em> 标签html中高亮显示
]
}
},
{
"_index": "test",
"_type": "test",
"_id": "2",
"_score": 0.37235746,
"_source": {
"first_name": "小翠",
"last_name": "cuicui",
"age": 18,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
},
"highlight": {
"about": [
"I love to go rock <em>climbing</em>"
]
}
},
{
"_index": "test",
"_type": "test",
"_id": "4",
"_score": 0.37235746,
"_source": {
"first_name": "小翠",
"last_name": "cui",
"age": 18,
"price": 15000,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
},
"highlight": {
"about": [
"I love to go rock <em>climbing</em>"
]
}
},
{
"_index": "test",
"_type": "test",
"_id": "5",
"_score": 0.2876821,
"_source": {
"first_name": "花花",
"last_name": "huahau",
"age": 16,
"price": 20000,
"about": "climbing",
"interests": [
"sports",
"music"
]
},
"highlight": {
"about": [
"<em>climbing</em>"
]
}
},
{
"_index": "test",
"_type": "test",
"_id": "3",
"_score": 0.25759193,
"_source": {
"first_name": "小雪",
"last_name": "xiaoxue",
"age": 20,
"about": "climbing",
"interests": [
"dancing",
"music"
]
},
"highlight": {
"about": [
"<em>climbing</em>"
]
}
},
{
"_index": "test",
"_type": "test",
"_id": "AWrVpGsO0WDJvaOeQOjd",
"_score": 0.12820786,
"_source": {
"first_name": "小翠",
"last_name": "cui",
"age": 18,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
},
"highlight": {
"about": [
"I love to go rock <em>climbing</em>"
]
}
}
]
}
}
--------------------- 

Guess you like

Origin www.cnblogs.com/hyhy904/p/10992425.html