-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathElasticsearchIndex.test.js
155 lines (141 loc) · 4.12 KB
/
ElasticsearchIndex.test.js
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
require('rubico/global')
const x = require('rubico/x')
const assert = require('assert')
const Test = require('thunk-test')
const ElasticsearchIndex = require('./ElasticsearchIndex')
const { callProp, includes } = x
const test = Test('ElasticsearchIndex', async function () {
const testIndex = new ElasticsearchIndex({
node: 'http://localhost:9200/',
index: 'test-index',
mappings: {
slug: { type: 'keyword' },
text: { type: 'text' },
rating: { type: 'integer' },
},
})
await testIndex.ready
await testIndex.indexDocument({
slug: 'hello-there',
text: 'hello there',
rating: 9,
}, {
refresh: 'true',
})
await testIndex.indexDocument({
slug: 'hello-world',
text: 'hello world',
rating: 8,
}, {
refresh: 'wait_for',
})
this.goodbyeWorldElasticsearchId = await testIndex.indexDocument({
slug: 'goodbye-world',
text: 'goodbye world',
rating: 7,
}, {
refresh: 'false',
}).then(pipe([
callProp('json'),
get('_id'),
]))
await testIndex.refresh()
{
const response = await testIndex.term({
slug: 'hello-world',
})
assert.strictEqual(response.status, 200)
const data = await response.json()
assert.strictEqual(data.hits.total.value, 1)
assert.strictEqual(data.hits.hits.length, 1)
const doc = data.hits.hits[0]._source
assert.strictEqual(doc.slug, 'hello-world')
}
{
const response = await testIndex.match({
text: 'hello',
})
assert.strictEqual(response.status, 200)
const data = await response.json()
assert.strictEqual(data.hits.total.value, 2)
assert.strictEqual(data.hits.hits.length, 2)
const texts = data.hits.hits.map(get('_source.text'))
assert(texts.every(includes('hello')))
}
{
const response = await testIndex.multiMatch({
query: 'hello',
fields: ['text', 'slug'],
})
assert.strictEqual(response.status, 200)
const data = await response.json()
assert.strictEqual(data.hits.total.value, 2)
assert.strictEqual(data.hits.hits.length, 2)
const texts = data.hits.hits.map(get('_source.text'))
assert(texts.every(includes('hello')))
}
{
const response = await testIndex.bool({
must_not: [
{ term: { slug: 'goodbye-world' } },
],
}, { size: 100 })
assert.strictEqual(response.status, 200)
const data = await response.json()
assert.strictEqual(data.hits.total.value, 2)
assert.strictEqual(data.hits.hits.length, 2)
const texts = data.hits.hits.map(get('_source.text'))
assert(texts.every(includes('hello')))
}
{
const response = await testIndex.disMax({
queries: [
{ term: { slug: 'hello-there' } },
{ term: { slug: 'hello-world' } },
],
tie_breaker: 0,
}, { size: 100 })
assert.strictEqual(response.status, 200)
const data = await response.json()
assert.strictEqual(data.hits.total.value, 2)
assert.strictEqual(data.hits.hits.length, 2)
const texts = data.hits.hits.map(get('_source.text'))
assert(texts.every(includes('hello')))
}
{
const response = await testIndex.functionScore({
field_value_factor: {
field: 'rating',
factor: 1.2,
modifier: 'sqrt',
missing: 1,
},
})
assert.strictEqual(response.status, 200)
const data = await response.json()
assert.strictEqual(data.hits.total.value, 3)
assert.strictEqual(data.hits.hits.length, 3)
const ratings = data.hits.hits.map(get('_source.rating'))
assert.deepEqual(ratings, [...ratings].sort((a, b) => b - a)) // desc
}
{
const response = await testIndex.updateDocument(this.goodbyeWorldElasticsearchId, {
text: 'goodbye world?',
}, {
refresh: 'true',
})
assert.strictEqual(response.status, 200)
const data = await response.json()
}
{
const response = await testIndex.getDocument(this.goodbyeWorldElasticsearchId)
assert.strictEqual(response.status, 200)
const data = await response.json()
assert.strictEqual(data._source.text, 'goodbye world?')
}
await testIndex.delete()
}).case()
if (process.argv[1] == __filename) {
test()
}
module.exports = test