Elasticsearch 使い方

実行環境

  • Elasticsearch 7.10.1
  • Kibana 7.10.1

    検索

完全一致検索

データ項目のタイプがキーワードとして登録されている必要があります。
検索対象のtypekeywordで、querytermを指定して行う必要があります。

request

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#対象index作成 都道府県(pref)のtypeを完全一致の対象にする為にkeywordで登録する
PUT search_job
{
"mappings" : {
"properties": {
"pref": {
"type": "keyword"
}
}
}
}

#type確認
GET search_job/_mapping

response

1
2
3
4
5
6
7
8
9
10
11
{
"search_job" : {
"mappings" : {
"properties" : {
"pref" : {
"type" : "keyword"
}
}
}
}
}

request

1
2
3
4
5
6
7
データ登録
POST /search_job/_doc
{
"pref" : "東京"
}
# データ確認
GET /search_job/_search

response

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
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "search_job",
"_type" : "_doc",
"_id" : "jS17e3YB0KvFjllCp3-y",
"_score" : 1.0,
"_source" : {
"pref" : "東京"
}
}
]
}
}

request

1
2
3
4
5
6
7
8
9
# queryの種類をtermにすることで完全一致検索
GET /search_job/_search
{
"query" : {
"term": {
"pref":"東京"
}
}
}

response

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
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "search_job",
"_type" : "_doc",
"_id" : "jS17e3YB0KvFjllCp3-y",
"_score" : 0.2876821,
"_source" : {
"pref" : "東京"
}
}
]
}
}

request

1
2
3
4
5
6
7
8
9
# queryの種類をtermにすることで完全一致検索
GET /search_job/_search
{
"query" : {
"term": {
"pref":"東"
}
}
}

response

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 該当するものは無い
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
}
}

indexを定義せずにデータ登録した場合

request

1
2
3
4
POST /search_job2/_doc
{
"pref" : "東京"
}

response

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# typeでtextで登録されて、fieldsにtype:keywordでも登録されます
{
"search_job2" : {
"mappings" : {
"properties" : {
"pref" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}

request

1
2
3
4
5
6
7
8
9
# termの対象をpref.keywordとすることで、fieldsのtype:keywordを対象とすることができます
GET /search_job2/_search
{
"query" : {
"term" : {
"pref.keyword": "東京"
}
}
}

response

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
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "search_job2",
"_type" : "_doc",
"_id" : "ki2Ne3YB0KvFjllCJX_C",
"_score" : 0.2876821,
"_source" : {
"pref" : "東京"
}
}
]
}
}

複数検索条件の完全一致

queryのboolmustで複数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
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
#request
PUT search_job3
{
"mappings" : {
"properties": {
"pref": {
"type": "keyword"
},
"employment": {
"type": "keyword"
}
}
}
}
#response
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "search_job3"
}
#request
GET search_job3/_mapping
#response
{
"search_job3" : {
"mappings" : {
"properties" : {
"employment" : {
"type" : "keyword"
},
"pref" : {
"type" : "keyword"
}
}
}
}
}
#request
POST /search_job3/_doc
{
"pref" : "東京",
"employment" : "full-time"
}
#response
{
"_index" : "search_job3",
"_type" : "_doc",
"_id" : "lS1OfXYB0KvFjllCZ3-D",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
#request
GET /search_job3/_search
#response
{
"took" : 484,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "search_job3",
"_type" : "_doc",
"_id" : "lS1OfXYB0KvFjllCZ3-D",
"_score" : 1.0,
"_source" : {
"pref" : "東京",
"employment" : "full-time"
}
}
]
}
}

#request
#複数検索条件の完全一致
GET /search_job3/_search
{
"query" : {
"bool": {
"must" : [
{
"term": {
"pref": "東京"
}
},
{
"term": {
"employment": "full-time"
}
}
]
}
}
}
#response
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.5753642,
"hits" : [
{
"_index" : "search_job3",
"_type" : "_doc",
"_id" : "lS1OfXYB0KvFjllCZ3-D",
"_score" : 0.5753642,
"_source" : {
"pref" : "東京",
"employment" : "full-time"
}
}
]
}
}

ネストした検索対象への完全一致検索

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
#  路線が駅を持つようなネストしたデータを作成します
# request
PUT search_job4
{
"mappings" : {
"properties": {
"pref": {
"type": "keyword"
},
"employment": {
"type": "keyword"
},
"line": {
"properties" : {
"name": {
"type": "keyword"
},
"stations" : {
"properties" : {
"name" : {
"type" : "keyword"
}
}
}
}
}
}
}
}

POST /search_job4/_doc
{
"pref" : "東京",
"employment" : "full-time",
"line" : {
"name" : "山手線",
"stations" : [
{
"name" : "渋谷"
},
{
"name" : "恵比寿"
},
{
"name" : "品川"
}
]
}
}

GET /search_job4/_search

# response
# 路線が駅を持つようなネストしたデータが登録できました
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "search_job4",
"_type" : "_doc",
"_id" : "li1_fXYB0KvFjllCmn_A",
"_score" : 1.0,
"_source" : {
"pref" : "東京",
"employment" : "full-time",
"line" : {
"name" : "山手線",
"stations" : [
{
"name" : "渋谷"
},
{
"name" : "恵比寿"
},
{
"name" : "品川"
}
]
}
}
}
]
}
}

# resquest
# ネストされている駅名に対して完全一致検索をします
GET /search_job4/_search
{
"query" : {
"term": {
"line.stations.name": "渋谷"
}
}
}

ネストした検索対象にネスト構造の経路の関係(Nested datatype)をもたせる

ネストした経路の関係(Nested datatype)を持たせたい場合にtypeにnested指定してmappingをします。

先程のmappingだと、何線の何駅かというのは検索できません。
下記のようなデータを追加した場合。
山手線の渋谷駅というデータを絞りこめません。

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
# request
POST /search_job4/_doc
{
"pref" : "東京",
"employment" : "full-time",
"line" : [
{
"name" : "東横線",
"stations" : [
{
"name" : "渋谷"
},
{
"name" : "中目黒"
},
{
"name" : "横浜"
}
]
},
{
"name" : "山手線",
"stations" : [
{
"name" : "東京"
},
{
"name" : "神田"
},
{
"name" : "浅草"
}
]
}
]
}

GET /search_job4/_search

# response
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "search_job4",
"_type" : "_doc",
"_id" : "li1_fXYB0KvFjllCmn_A",
"_score" : 1.0,
"_source" : {
"pref" : "東京",
"employment" : "full-time",
"line" : {
"name" : "山手線",
"stations" : [
{
"name" : "渋谷"
},
{
"name" : "恵比寿"
},
{
"name" : "品川"
}
]
}
}
},
{
"_index" : "search_job4",
"_type" : "_doc",
"_id" : "mC2sfXYB0KvFjllCHn8N",
"_score" : 1.0,
"_source" : {
"pref" : "東京",
"employment" : "full-time",
"line" : [
{
"name" : "東横線",
"stations" : [
{
"name" : "渋谷"
},
{
"name" : "中目黒"
},
{
"name" : "横浜"
}
]
},
{
"name" : "山手線",
"stations" : [
{
"name" : "東京"
},
{
"name" : "神田"
},
{
"name" : "浅草"
}
]
}
]
}
}
]
}
}

# request
# 山手線の渋谷駅で検索
GET /search_job4/_search
{
"query" : {
"bool": {
"must" : [
{
"term": {
"line.name": "山手線"
}
},
{
"term": {
"line.stations.name": "渋谷"
}
}
]
}
}
}

# response
# 山手線の渋谷駅でないものも含まれてしまう。
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.47851416,
"hits" : [
{
"_index" : "search_job4",
"_type" : "_doc",
"_id" : "li1_fXYB0KvFjllCmn_A",
"_score" : 0.47851416,
"_source" : {
"pref" : "東京",
"employment" : "full-time",
"line" : {
"name" : "山手線",
"stations" : [
{
"name" : "渋谷"
},
{
"name" : "恵比寿"
},
{
"name" : "品川"
}
]
}
}
},
{
"_index" : "search_job4",
"_type" : "_doc",
"_id" : "mC2sfXYB0KvFjllCHn8N",
"_score" : 0.47851416,
"_source" : {
"pref" : "東京",
"employment" : "full-time",
"line" : [
{
"name" : "東横線",
"stations" : [
{
"name" : "渋谷"
},
{
"name" : "中目黒"
},
{
"name" : "横浜"
}
]
},
{
"name" : "山手線",
"stations" : [
{
"name" : "東京"
},
{
"name" : "神田"
},
{
"name" : "浅草"
}
]
}
]
}
}
]
}
}

これは、追加したデータがElasticsearchの内部ではネスト構造を保たず、項目ごとにグルーピングされたような構造(Object datatype)で解釈される為になります。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
"line" : {
"name" : [
"東横線",
"山手線"
],
"stations" : {
"name" : [
"渋谷",
"中目黒",
"横浜",
"東京",
"神田",
"浅草"
]
}
}
}

路線のtypenestedにしてネスト構造を保ったままmappingし、ネスト構造の経路を指定して検索してみる。

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
275
276
277
# request
PUT search_job5
{
"mappings" : {
"properties": {
"pref": {
"type": "keyword"
},
"employment": {
"type": "keyword"
},
"line": {
"type" : "nested",
"properties" : {
"name": {
"type": "keyword"
},
"stations" : {
"properties" : {
"name" : {
"type" : "keyword"
}
}
}
}
}
}
}
}

GET search_job5/_mapping
# response
# line(路線)のtypeがnestedになっていることがわかる
{
"search_job5" : {
"mappings" : {
"properties" : {
"employment" : {
"type" : "keyword"
},
"line" : {
"type" : "nested",
"properties" : {
"name" : {
"type" : "keyword"
},
"stations" : {
"properties" : {
"name" : {
"type" : "keyword"
}
}
}
}
},
"pref" : {
"type" : "keyword"
}
}
}
}
}

# request
POST /search_job5/_doc
{
"pref" : "東京",
"employment" : "full-time",
"line" : {
"name" : "山手線",
"stations" : [
{
"name" : "渋谷"
},
{
"name" : "恵比寿"
},
{
"name" : "品川"
}
]
}
}

POST /search_job5/_doc
{
"pref" : "東京",
"employment" : "full-time",
"line" : [
{
"name" : "東横線",
"stations" : [
{
"name" : "渋谷"
},
{
"name" : "中目黒"
},
{
"name" : "横浜"
}
]
},
{
"name" : "山手線",
"stations" : [
{
"name" : "東京"
},
{
"name" : "神田"
},
{
"name" : "浅草"
}
]
}
]
}

GET /search_job5/_search
# response
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "search_job5",
"_type" : "_doc",
"_id" : "mS3wfnYB0KvFjllCcn9x",
"_score" : 1.0,
"_source" : {
"pref" : "東京",
"employment" : "full-time",
"line" : {
"name" : "山手線",
"stations" : [
{
"name" : "渋谷"
},
{
"name" : "恵比寿"
},
{
"name" : "品川"
}
]
}
}
},
{
"_index" : "search_job5",
"_type" : "_doc",
"_id" : "mi3wfnYB0KvFjllCk3_f",
"_score" : 1.0,
"_source" : {
"pref" : "東京",
"employment" : "full-time",
"line" : [
{
"name" : "東横線",
"stations" : [
{
"name" : "渋谷"
},
{
"name" : "中目黒"
},
{
"name" : "横浜"
}
]
},
{
"name" : "山手線",
"stations" : [
{
"name" : "東京"
},
{
"name" : "神田"
},
{
"name" : "浅草"
}
]
}
]
}
}
]
}
}

# request
# nestedのpathにlineを指定し、line(路線)のネスト構造の経路内で山手線の渋谷を検索する
GET /search_job5/_search
{
"query": {
"nested": {
"path": "line",
"query" : {
"bool": {
"must" : [
{
"term": {
"line.name": "山手線"
}
},
{
"term": {
"line.stations.name": "渋谷"
}
}
]
}
}
}
}
}

# response
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.1162586,
"hits" : [
{
"_index" : "search_job5",
"_type" : "_doc",
"_id" : "mS3wfnYB0KvFjllCcn9x",
"_score" : 1.1162586,
"_source" : {
"pref" : "東京",
"employment" : "full-time",
"line" : {
"name" : "山手線",
"stations" : [
{
"name" : "渋谷"
},
{
"name" : "恵比寿"
},
{
"name" : "品川"
}
]
}
}
}
]
}
}

参考url