1 | define([
|
---|
2 | 'dojo/_base/declare',
|
---|
3 | "dojo/_base/json",
|
---|
4 | 'dojo/_base/lang',
|
---|
5 | 'dojo/_base/xhr',
|
---|
6 | 'dojox/data/QueryReadStore'
|
---|
7 | ],function(declare, json, lang, xhr, QueryReadStore) {
|
---|
8 |
|
---|
9 | return declare("rft.elastic.ElasticReadStore", QueryReadStore, {
|
---|
10 | fetch:function(request){
|
---|
11 | var attr = Object.keys(request.query)[0];
|
---|
12 | if (request.query[attr].length == 0) {
|
---|
13 | return 0;
|
---|
14 | }
|
---|
15 | var q = request.query[attr];
|
---|
16 |
|
---|
17 | request.serverQuery = json.toJson({
|
---|
18 | query: {
|
---|
19 | query_string: {
|
---|
20 | default_field: attr,
|
---|
21 | query: q
|
---|
22 | }
|
---|
23 | }
|
---|
24 | });
|
---|
25 | return this.inherited(arguments);
|
---|
26 | },
|
---|
27 | _fetchItems: function(request, fetchHandler, errorHandler){
|
---|
28 | var serverQuery = request.serverQuery;
|
---|
29 | var xhrHandler = xhr.post({
|
---|
30 | url: this.url,
|
---|
31 | handleAs: "json",
|
---|
32 | postData: serverQuery
|
---|
33 | }).then(lang.hitch(this, function(data){
|
---|
34 | this._xhrFetchHandler(data, request, fetchHandler, errorHandler);
|
---|
35 | }),function(error){
|
---|
36 | errorHandler(error, request);
|
---|
37 | });
|
---|
38 | request.abort = function(){
|
---|
39 | xhrHandler.cancel();
|
---|
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 | }) |
---|