-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathforceIndexRefresh.js
More file actions
177 lines (163 loc) · 4.69 KB
/
Copy pathforceIndexRefresh.js
File metadata and controls
177 lines (163 loc) · 4.69 KB
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
'use strict'
const mongoose = require('mongoose')
const config = require('./config')
const esClient = config.getClient()
const Schema = mongoose.Schema
const mongoosastic = require('../lib/mongoosastic')
const indexName = 'es-test'
const DummySchema = new Schema({
text: String
})
DummySchema.plugin(mongoosastic, {
esClient: esClient,
index: indexName
})
const Dummy = mongoose.model('Dummy', DummySchema)
describe('forceIndexRefresh connection option', function () {
before(function (done) {
// connect to mongodb
mongoose.connect(config.mongoUrl, function () {
// delete the index from elasticsearch
config.deleteIndexIfExists([indexName], function (err) {
// recreate the index
Dummy.createMapping({
'analysis': {
'analyzer': {
'content': {
'type': 'custom',
'tokenizer': 'whitespace'
}
}
}
}, function (err, mapping) {
// clean mongodb
Dummy.remove(function (err) {
setTimeout(done, config.INDEXING_TIMEOUT)
})
})
})
})
})
after(function (done) {
// disconnect mongodb
mongoose.disconnect()
// disconnect elasticsearch
config.close()
done()
})
it('should always succeed: refresh the index immediately on insert', function (done) {
DummySchema.plugin(mongoosastic, {
esClient: esClient,
index: indexName,
forceIndexRefresh: true
})
const Dummy3 = mongoose.model('Dummy', DummySchema)
const d = new Dummy3({text: 'Text1'})
doInsertOperation(Dummy3, d, indexName, done)
})
it('should fail randomly: refresh the index every 1s on insert', function (done) {
DummySchema.plugin(mongoosastic, {
esClient: esClient,
index: indexName,
forceIndexRefresh: false
})
const Dummy2 = mongoose.model('Dummy', DummySchema)
const d = new Dummy2({text: 'Text1'})
doInsertOperation(Dummy2, d, indexName, done)
})
it('should always succeed: refresh the index immediately on update', function (done) {
DummySchema.plugin(mongoosastic, {
esClient: esClient,
index: indexName,
forceIndexRefresh: true
})
const Dummy3 = mongoose.model('Dummy', DummySchema)
const d = new Dummy3({text: 'Text1'})
doUpdateOperation(Dummy3, d, 'this is the new text', indexName, done)
})
it('should fail randomly: refresh the index every 1s on update', function (done) {
DummySchema.plugin(mongoosastic, {
esClient: esClient,
index: indexName,
forceIndexRefresh: false
})
const Dummy2 = mongoose.model('Dummy', DummySchema)
const d = new Dummy2({text: 'Text1'})
doUpdateOperation(Dummy2, d, 'this is the new text', indexName, done)
})
})
function doInsertOperation (Model, object, indexName, callback) {
// save object
object.save(function (err, savedObject) {
if (err) {
return callback(err)
}
// wait for indexing
savedObject.on('es-indexed', function (err) {
if (err) {
return callback(err)
}
// look for the object just saved
Model.search({
term: {_id: savedObject._id}
},
function (err, results) {
results.hits.total.should.eql(1)
// clean the db
savedObject.remove(function (err) {
if (err) {
return callback(err)
}
savedObject.on('es-removed', function (err) {
if (err) {
return callback(err)
}
callback()
})
})
})
})
})
}
function doUpdateOperation (Model, object, newText, indexName, callback) {
// save object
object.save(function (err, savedObject) {
if (err) {
return callback(err)
}
// update object
Model
.findOneAndUpdate({_id: savedObject._id}, {text: newText}, {'new': true})
.exec(function (err, updatedObject) {
if (err) {
return callback(err)
}
// wait for indexing
updatedObject.on('es-indexed', function (err) {
if (err) {
return callback(err)
}
// look for the object just saved
Model.search({
term: {_id: savedObject._id.toString()}
},
function (err, results) {
results.hits.total.should.eql(1)
results.hits.hits[0]._source.text.should.eql(newText)
// clean the db
updatedObject.remove(function (err) {
if (err) {
return callback(err)
}
updatedObject.on('es-removed', function (err) {
if (err) {
return callback(err)
}
callback()
})
})
})
})
})
})
}