-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathelasticsearch_adapter.js
More file actions
289 lines (220 loc) · 8.14 KB
/
elasticsearch_adapter.js
File metadata and controls
289 lines (220 loc) · 8.14 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
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
/**
This class provides an adapter for the Ember Data [https://github.com/emberjs/data]
persistence library for Ember.js, which stores data as JSON documents
in elasticsearch [http://elasticsearch.org].
### Usage
Pass the adapter to a store, optionally providing elasticsearch URL:
var store = DS.Store.create({
revision: 4,
adapter: DS.ElasticSearchAdapter.create({url: "http://localhost:9200"})
});
Let's assume a standard Ember Data model, such as:
var Person = DS.Model.extend({
name: DS.attr('string')
});
You have to define an index and type for your model as the `url` attribute:
Person.reopenClass({
url: 'people/person'
});
Now you can use the standard Ember Data adapter interface
for creating, updating, destroying and finding records:
Person.createRecord({ id: 1, name: "John" });
store.commit();
var person = Person.find( 1);
person.get("name");
person.set("name", "Jonathan");
store.commit();
person.deleteRecord();
store.commit();
Don't forget, that in Ember Data, you have to call the `commit` method to persist the changes!
You can use the full elasticsearch's _Query DSL_ to find records:
var person = Person.find({query: { query_string: { query: 'john' } }});
person.get("name");
### Todo
* Resolve the content-type issues when talking to elasticsearch from Ajax.
Can't just add `contentType: 'application/json; charset=UTF-8'` to $.ajax,
because of: `Request header field Content-Type is not allowed by Access-Control-Allow-Headers.` error.
* Refactor the HTTP "client".
* Implement the "bulk" variant of API.
@extends DS.Adapter
*/
DS.ElasticSearchAdapter = DS.Adapter.extend({
/**
@field Default URL for elasticsearch
*/
url: "http://localhost:9200",
/**
HTTP client
TODO: Refactor, simplify.
*/
http: {
get: function(url, callback) {
return jQuery.ajax({
url: url,
type: 'GET',
dataType: 'json',
cache: true,
success: callback
});
},
post: function(url, payload, callback) {
return jQuery.ajax({
url: url,
type: 'POST',
data: JSON.stringify(payload),
dataType: 'json',
cache: true,
success: callback
});
},
put: function(url, payload, callback) {
return jQuery.ajax({
url: url,
type: 'PUT',
data: JSON.stringify(payload),
dataType: 'json',
cache: true,
success: callback
});
},
delete: function(url, payload, callback) {
return jQuery.ajax({
url: url,
type: 'DELETE',
data: JSON.stringify(payload),
dataType: 'json',
cache: true,
success: callback
});
}
},
/**
Loads a single record by ID:
store.find(Person, 1);
*/
find: function(store, type, id) {
if (Ember.ENV.DEBUG) console.debug('find', store, type, id);
var url = [this.url, type.url, id].join('/');
this.http.get(url, function(data, textStatus, xhr) {
if (Ember.ENV.DEBUG) console.debug('elasticsearch (' + xhr.status + ') :', Ember.ENV.CI ? JSON.stringify(data) : data);
store.load(type, id, data['_source']);
});
},
/**
Loads all records (up to one million :):
store.findAll(Person);
*/
findAll: function(store, type) {
var url = [this.url, type.url, '_search'].join('/');
var payload = {size: 1000000};
this.http.post(url, payload, function(data, textStatus, xhr) {
if (Ember.ENV.DEBUG) console.debug('elasticsearch (' + xhr.status + '):', Ember.ENV.CI ? JSON.stringify(data) : data);
store.loadMany(type, data['hits']['hits'].map( function(i) {
return Ember.Object.create(i['_source']).reopen({id: i._id, version: i._version})
} ));
});
},
/**
Loads a collection of records by IDs:
store.find(Person, [2, 3, 1]);
*/
findMany: function(store, type, ids) {
if (Ember.ENV.DEBUG) console.debug('findMany', ids);
var url = [this.url, type.url, '_mget'].join('/');
var payload = {ids: ids};
this.http.post(url, payload, function(data, textStatus, xhr) {
if (Ember.ENV.DEBUG) console.debug('elasticsearch (' + xhr.status + '):', Ember.ENV.CI ? JSON.stringify(data) : data);
store.loadMany(type, data['docs'].map( function(i) { return i['_source'] } ));
});
},
/**
Loads a collection of records by a fulltext query:
store.findQuery(Person, {query: { query_string: { query: 'john' } }});
See the elasticsearch [documentation](http://elasticsearch.org/guide/reference/query-dsl) for more info.
*/
findQuery: function(store, type, query, recordArray) {
if (Ember.ENV.DEBUG) console.debug('findQuery', query);
var url = [this.url, type.url, '_search'].join('/');
var payload = query;
// var payload = { query: { query_string: { query: 'John' } } };
this.http.post(url, payload, function(data, textStatus, xhr) {
if (Ember.ENV.DEBUG) console.debug('elasticsearch (' + xhr.status + '):', Ember.ENV.CI ? JSON.stringify(data) : data);
recordArray.load(data['hits']['hits'].map( function(i) { return i['_source'] } ));
});
},
/**
Creates a new record, persisted in elasticsearch:
store.createRecord(Person, { id: 1, name: "Alice" });
store.commit();
If you don't provide an ID for the object, elasticsearch will generate one:
var person = store.createRecord(Person, { name: "Anne" });
store.commit();
result.get('id')
// => 'abcDEF-abc123XYZ'
*/
createRecord: function(store, type, record) {
if (Ember.ENV.DEBUG) console.debug('createRecord', type, record.toJSON(), 'id: ', record.get("id"));
var id = record.get("id") || null;
var url = [this.url, type.url, id].join('/');
var payload = record.toJSON();
var self = this;
this.http.post(url, payload, function(data, textStatus, xhr) {
if (Ember.ENV.DEBUG) console.debug('elasticsearch (' + xhr.status + '):', Ember.ENV.CI ? JSON.stringify(data) : data)
self.didCreateRecord(store, type, record, data);
});
},
didCreateRecord: function(store, type, record, json) {
var recordData = record.get('data');
if (record.get('id')) {
recordData.commit();
} else {
record.beginPropertyChanges();
record.set('id', json._id)
record.endPropertyChanges();
}
record.send('didCommit');
},
/**
Persists object changes to elasticsearch:
var person = store.find(Person, 1);
person.set("name", "Caroline");
store.commit();
*/
updateRecord: function(store, type, record) {
if (Ember.ENV.DEBUG) console.debug('updateRecord', type, record.toJSON(), 'id:', record.get("id"));
var id = record.get("id");
var url = [this.url, type.url, id].join('/');
var payload = record.toJSON();
var self = this;
this.http.put(url, payload, function(data, textStatus, xhr) {
if (Ember.ENV.DEBUG) console.debug('elasticsearch (' + xhr.status + '):', Ember.ENV.CI ? JSON.stringify(data) : data)
self.didUpdateRecord(store, type, record, data);
});
},
didUpdateRecord: function(store, type, record, json) {
var recordData = record.get('data');
recordData.commit();
record.send('didChangeData');
record.send('didSaveData');
record.send('didCommit');
},
/**
Deletes the record from elasticsearch:
var person = store.find(Person, 1);
person.deleteRecord();
store.commit();
*/
deleteRecord: function(store, type, record) {
if (Ember.ENV.DEBUG) console.debug('deleteRecord', type, record.toJSON(), 'id: ', record.get("id"));
var id = record.get("id");
var url = [this.url, type.url, id].join('/');
var self = this;
this.http.delete(url, {}, function(data, textStatus, xhr) {
if (Ember.ENV.DEBUG) console.debug('elasticsearch (' + xhr.status + '):', Ember.ENV.CI ? JSON.stringify(data) : data)
self.didDeleteRecord(store, type, record, data);
});
},
didDeleteRecord: function(store, type, record, json) {
store.didDeleteRecord(record);
}
});