1 | define(["dojo", "dojox", "dojo/json"], function(dojo, dojox, JSON) {
|
---|
2 | dojo.provide("rft.elastic.ElasticReadStore");
|
---|
3 |
|
---|
4 | dojo.require("dojox.data.QueryReadStore");
|
---|
5 |
|
---|
6 | dojo.declare("rft.elastic.ElasticReadStore", dojox.data.QueryReadStore, {
|
---|
7 | fetch:function(request){
|
---|
8 | var attr = Object.keys(request.query)[0];
|
---|
9 | if (request.query[attr].length == 0) {
|
---|
10 | return 0;
|
---|
11 | }
|
---|
12 | var q = request.query[attr];
|
---|
13 |
|
---|
14 | request.serverQuery = dojo.toJson({
|
---|
15 | query:
|
---|
16 | {
|
---|
17 | query_string:
|
---|
18 | {
|
---|
19 | default_field: attr,
|
---|
20 | query: q
|
---|
21 | }
|
---|
22 | }
|
---|
23 | });
|
---|
24 | // Call superclasses' fetch
|
---|
25 | return this.inherited("fetch", arguments);
|
---|
26 | },
|
---|
27 | _fetchItems: function(request, fetchHandler, errorHandler){
|
---|
28 | var serverQuery = request.serverQuery;
|
---|
29 | var xhrHandler = dojo.xhrPost( { url:this.url,
|
---|
30 | handleAs: "json",
|
---|
31 | postData: serverQuery });
|
---|
32 | request.abort = function(){
|
---|
33 | xhrHandler.cancel();
|
---|
34 | };
|
---|
35 | xhrHandler.addCallback(dojo.hitch(this, function(data){
|
---|
36 | this._xhrFetchHandler(data, request, fetchHandler, errorHandler);
|
---|
37 | }));
|
---|
38 | xhrHandler.addErrback(function(error){
|
---|
39 | errorHandler(error, request);
|
---|
40 | });
|
---|
41 | },
|
---|
42 | _xhrFetchHandler: function(data, request, fetchHandler, errorHandler) {
|
---|
43 | data = this._filterResponse(data);
|
---|
44 | this._items = [];
|
---|
45 | var numHits = data.hits.total || -1;
|
---|
46 | if(numHits > 0) {
|
---|
47 | this._items.push({i:data.hits.hits[0]._source, r:this, n:0});
|
---|
48 | }
|
---|
49 | fetchHandler(this._items, request, data.hits.total);
|
---|
50 | this._numRows = data.hits.total;
|
---|
51 | }
|
---|
52 | });
|
---|
53 | }) |
---|