forked from shaggyone/es-experiments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexperiments.rb
327 lines (300 loc) · 10.8 KB
/
experiments.rb
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
require 'rubygems'
require 'bundler/setup'
require 'elasticsearch'
require 'pry'
$client = Elasticsearch::Client.new(
url: "http://elastic:[email protected]:9200",
log: true,
)
# $client.transport.reload_connections!
puts $client.cluster.health
$client.indices.delete(index: "myindex") rescue puts("No index yet")
$client.indices.create(
index: 'myindex',
body: {
settings: {
index: {
analysis: {
analyzer: {
trigram_english: {
type: "custom",
tokenizer: "standard",
filter: [
# "stemmer",
"lowercase",
"trigrams_filter",
]
}
},
filter: {
stemmer: {
type: "stemmer",
name: "english"
},
trigrams_filter: {
type: "ngram",
min_gram: 3,
max_gram: 3,
}
}
}
}
},
mappings: {
saved_searches: {
properties: {
keywords: {
type: "text",
analyzer: "english",
fields: {
trigram: {
type: "text",
analyzer: "trigram_english",
}
}
}
}
},
bars: {
properties: {
title: {
type: "text",
analyzer: "english",
# index_options: "docs"
fields: {
trigram: {
type: "text",
analyzer: "trigram_english"
}
}
}
}
},
foos: {
properties: {
title: {
type: "text",
analyzer: "english",
# index_options: "docs"
fields: {
trigram: {
type: "text",
analyzer: "trigram_english"
}
}
},
title2: {
type: "text",
analyzer: "english",
# index_options: "docs"
fields: {
trigram: {
type: "text",
analyzer: "trigram_english"
}
}
},
body: {
type: "text",
analyzer: "english",
# index_options: "docs"
fields: {
trigram: {
type: "text",
analyzer: "trigram_english"
}
}
}
}
}
}
}
)
$client.index index: 'myindex', type: 'foos', id: 1, body: { title: 'Test', body: "Foo bar", val: 10 }
$client.index index: 'myindex', type: 'foos', id: 2, body: { title: 'Test 2', body: "Foo bar", val: 11 }
$client.index index: 'myindex', type: 'foos', id: 3, body: { title: 'foobar', body: "Test", val: 12 }
$client.index index: 'myindex', type: 'foos', id: 4, body: { title: 'foobar', title2: %w'test test test', body: "Test", val: 1 }
$client.index index: 'myindex', type: 'foos', id: 5, body: { title: 'foobar', title2: 'test test', body: "Test", val: 5 }
$client.index index: 'myindex', type: 'foos', id: 6, body: { title: 'test', title2: 'test test', body: "foo bar", val: 8 }
$client.index index: 'myindex', type: 'foos', id: 7, body: { title: 'tests', title2: 'test test', body: "test foo bar" }
$client.index index: 'myindex', type: 'foos', id: 8, body: { title: 'footestbar' }
$client.index index: 'myindex', type: 'foos', id: 9, body: { title: 'foo bar', body: 'footestbar' }
$client.index index: 'myindex', type: 'foos', id: 10, body: { title: 'foobar', body: "Test", val: 15 }
$client.index index: 'myindex', type: 'foos', id: 11, body: { title: 'foobar', body: "Test tests", val: 2 }
$client.index index: 'myindex', type: 'foos', id: 12, body: { title: 'foobar', body: "foo Tests foo test", val: 21 }
$client.index index: 'myindex', type: 'foos', id: 13, body: { title: 'afoobcbard', body: "Tests foo test", val: 21 }
$client.index index: 'myindex', type: 'foos', id: 14, body: { title: 'foobar', body: "Tests afoob cbard test", val: 21 }
$client.index index: 'myindex', type: 'foos', id: 15, body: { title: 'xxx', body: "afoo abar", val: 21 }
$client.index index: 'myindex', type: 'bars', id: 1, body: { title: 'foo bar' }
$client.index index: 'myindex', type: 'bars', id: 2, body: { title: 'foo a bar' }
$client.index index: 'myindex', type: 'bars', id: 3, body: { title: 'foo a b bar' }
$client.index index: 'myindex', type: 'bars', id: 4, body: { title: 'foo a b c bar' }
$client.index index: 'myindex', type: 'bars', id: 5, body: { title: 'foo a b c d bar' }
$client.index index: 'myindex', type: 'bars', id: 6, body: { title: 'foo a b c d e bar' }
$client.index index: 'myindex', type: 'bars', id: 7, body: { title: 'foo a b c d e f bar' }
$client.index index: 'myindex', type: 'bars', id: 8, body: { title: 'foo a b c d e f g bar' }
$client.index index: 'myindex', type: 'bars', id: 9, body: { title: 'foo a b c d e f g h bar' }
$client.index index: 'myindex', type: 'bars', id: 10, body: { title: 'foo a b c d e f g h i bar' }
$client.index index: 'myindex', type: 'bars', id: 11, body: { title: 'foo a b c d e f g h i j bar' }
$client.index index: 'myindex', type: 'bars', id: 12, body: { title: 'foo a b c d e f g h i j' }
$client.index index: 'myindex', type: 'bars', id: 13, body: { title: 'a b c d e f g h i j bar' }
$client.index index: 'myindex', type: 'saved_searches', id: 1, body: { keywords: "catherine" }
$client.index index: 'myindex', type: 'saved_searches', id: 2, body: { keywords: "'Super Duper'" }
$client.index index: 'myindex', type: 'saved_searches', id: 3, body: { keywords: "Other Accessories" }
$client.index index: 'myindex', type: 'saved_searches', id: 4, body: { keywords: "696 Metallic Detailed Leather Trainer" }
$client.index index: 'myindex', type: 'saved_searches', id: 5, body: { keywords: "rings" }
$client.index index: 'myindex', type: 'saved_searches', id: 6, body: { keywords: "Everlane" }
$client.index index: 'myindex', type: 'saved_searches', id: 7, body: { keywords: "Christmas" }
$client.index index: 'myindex', type: 'saved_searches', id: 8, body: { keywords: "Sweaters" }
$client.index index: 'myindex', type: 'saved_searches', id: 9, body: { keywords: "Bergdorfgoodman" }
$client.index index: 'myindex', type: 'saved_searches', id: 10, body: { keywords: "Editorialist" }
$client.index index: 'myindex', type: 'saved_searches', id: 11, body: { keywords: "query" }
$client.index index: 'myindex', type: 'saved_searches', id: 12, body: { keywords: "testfoobar" }
binding.pry
value = "query"
$client.search(
index: ['myindex'],
from: 0,
size: 100,
body: {
query: {
multi_match: {
query: value,
fields: ["keywords*"],
minimum_should_match: "100%", # If only 30% of query string matches, that's ok
}
}
},
pretty: true
)
$client.termvectors(index: 'myindex', type: 'foos', id: 14,
fields: ["*"],
)["term_vectors"].map { |k,v| [k, v["terms"].keys] }.to_h
# term_statistics: true,
# field_statistics: true,
# offsets: true,
# positions: true,
# payloads: true,
# pretty: true,
# :fields (List) — A comma-separated list of fields to return
# :body (Hash) — The request definition
# :preference (String) — Specify the node or shard the operation should be performed on (default: random)
# :realtime (String) — Specifies if requests are real-time as opposed to near-real-time (default: true)
# :routing (String) — Specific routing value
# :parent (String) — Parent ID of the documents
# )
value = "query"
$client.explain(
index: 'myindex',
type: 'saved_searches',
id: 11,
body: {
query: {
multi_match: {
query:"query",
fields: ["keywords.trigram"],
minimum_should_match: "100%", # If only 30% of query string matches, that's ok
}
}
}
)
value = "foo bar"
$client.search(
index: ['myindex'],
body: {
from: 0,
size: 100,
query: {
function_score: {
query: { # This is basic query
bool: {
must: [
{ multi_match: { query: value, fields: [ 'title*', 'body', 'title*.trigram', 'body*.trigram'] } },
{ type: { value: "foos" } },
]
}
},
score_mode: "sum", # Use + operation when calculating score
boost_mode: "sum", # Use + operation when boosting score
functions: [
{
filter: { # Add 100 millions to score of docs, that have val between 10 and 20.
# Can be used to raise score of outfits registered within last 4 months
range: { val: { gte: 10, lte: 20 } }
},
weight: 100_000_000,
},
{ # Add 1 million to score for matches using full words
filter: {
multi_match: { query: value, fields: ['title*', 'body'] }
},
weight: 1000_000,
},
{
filter: { # Add 10 000 to score for matches within title attributes
multi_match: { query: value, fields: [ 'title*', 'title*.trigram'] }
},
weight: 10_000,
},
{ # Add 100 to score for matches within body attributes
filter: {
multi_match: { query: value, fields: [ 'body', 'body.trigram'] }
},
weight: 100,
},
{ # Add 100 to score for matches within body attributes
filter: {
multi_match: { query: value, fields: [ 'body', 'body.trigram'] }
},
weight: -100,
},
],
}
}
}
)
value = "foo bar"
$client.search(
index: ['myindex'],
type: "bars",
body: {
from: 0,
size: 100,
query: {
function_score: {
query: { # This is basic query
multi_match: {
query: value,
fields: ['title', 'title.trigram'],
minimum_should_match: "30%", # If only 30% of query string matches, that's ok
},
},
score_mode: "sum", # Use + operation when calculating score
boost_mode: "sum", # Use + operation when boosting score
functions: [
{ # Add 1 million to score for matches using full words
filter: {
multi_match: {
query: value,
fields: ['title'],
type: "phrase",
slop: 5 # Docs, with matchig words within 5 other words will appear higher
},
},
weight: 10,
},
{ # Add 1 million to score for matches using full words
filter: {
multi_match: {
query: value,
fields: ['title', 'title.trigram'],
minimum_should_match: "100%", # Full matches are put to top
},
},
weight: 100,
},
],
}
}
}
)