-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathkendo-elasticsearch.js
171 lines (138 loc) · 6.37 KB
/
kendo-elasticsearch.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
* A Kendo DataSource that gets its data from ElasticSearch.
*
* Read-only, supports paging, filtering and sorting.
*/
(function(kendo) {
'use strict';
var data = kendo.data;
if (!window.moment) throw new Error('Moment.js has to be loaded for ElasticSearchDataSource to work');
// Helper functions for conversion of query parameters from Kendo to ElasticSearch format
function arrayify(myArg) {
var _argArray = [];
if(myArg && myArg.constructor == Array) {
_argArray = myArg;
}
else {
if (!(myArg === void 0))
_argArray.push(myArg);
}
return _argArray;
}
data.ElasticSearchDataSource = data.DataSource.extend({
init: function(initOptions) {
var self = this;
if (!initOptions.transport || !initOptions.transport.read || !initOptions.transport.read.url)
throw new Error('transport.read.url must be set to use ElasticSearchDataSource');
initOptions.transport.read.dataType = initOptions.transport.read.dataType || 'json';
// Create a map mapping Kendo field names to ElasticSearch field names. We have to allow ElasticSearch field
// names to be different because ES likes an "@" in field names while Kendo fails on that.
var fields = initOptions.schema.model.fields;
this._esFieldMap = [];
for (var k in fields) {
if (fields.hasOwnProperty(k)) {
this._esFieldMap[k] = fields[k].hasOwnProperty('esName')
? fields[k].esName
: k;
}
}
initOptions.transport.parameterMap = function(data, type) {
var sortParams = arrayify(data.sort);
var esParams = {};
if (data.skip) esParams.from = data.skip;
if (data.take) esParams.size = data.take;
if (sortParams.length > 0)
esParams.sort = self._esFieldMap[sortParams[0].field] + ":" + sortParams[0].dir;
if (data.filter)
esParams.q = self._kendoFilterToESParam(data.filter);
return esParams;
};
var schema = initOptions.schema;
schema.parse = function(response) {
var hits = response.hits.hits;
var dataItems = [];
for (var i = 0; i < hits.length; i++) {
var hitSource = hits[i]._source;
var dataItem = {};
dataItem.id = hits[i]._id;
for (var k in self._esFieldMap) {
dataItem[k] = hitSource[self._esFieldMap[k]];
}
dataItems.push(dataItem);
}
var total_f = response.hits.total
if ("value" in response.hits.total) {
total_f = response.hits.total.value
}
return {
total: total_f,
data: dataItems
};
};
schema.data = schema.data || 'data';
schema.total = schema.total || 'total';
schema.model.id = schema.model.id || '_id';
initOptions.serverFiltering = initOptions.serverFiltering || true;
initOptions.serverSorting = initOptions.serverSorting || true;
initOptions.serverPaging = initOptions.serverPaging || true;
data.DataSource.fn.init.call(this, initOptions);
},
_kendoFilterToESParam: function(kendoFilterObj) {
// there are three possible structures for the kendoFilterObj:
// * {value: "...", operator: "...", field: " ..."}
// * {logic: "...", filters: ...}
// * [ ... ]
if (kendoFilterObj.operator) {
return this._kendoOperatorFilterToESParam(kendoFilterObj);
} else if (kendoFilterObj.logic) {
return this._kendoFilterListToESParam(kendoFilterObj.logic, kendoFilterObj.filters);
} else if (kendoFilterObj.constructor == Array) {
return this._kendoFilterListToESParam("and", kendoFilterObj.filters);
} else {
throw new Error("Don't know how to turn this Kendo filter object into ElasticSearch search parameter: "
+ kendoFilterObj);
}
},
_kendoOperatorFilterToESParam: function(kendoFilterObj) {
var fieldEscaped = this._asESParameter(this._esFieldMap[kendoFilterObj.field]);
var valueEscaped = this._asESParameter(kendoFilterObj.value);
var simpleBinaryOperators = {
eq: "",
lt: "<",
lte: "<=",
gt: ">",
gte: ">="
};
if (simpleBinaryOperators[kendoFilterObj.operator] !== void 0) {
var esOperator = simpleBinaryOperators[kendoFilterObj.operator];
return fieldEscaped + ":" + esOperator + valueEscaped;
} else {
switch (kendoFilterObj.operator) {
case "neq":
return "NOT (" + fieldEscaped + ":" + valueEscaped + ")";
case "contains":
return fieldEscaped + ":*" + valueEscaped + "*";
case "startswith":
return fieldEscaped + ":" + valueEscaped + "*";
case "endswith":
return fieldEscaped + ":*" + valueEscaped;
default:
throw new Error("Kendo search operator '" + kendoFilterObj.operator + "' is not yet supported");
}
}
},
// logicalConnective can be "and" or "or"
_kendoFilterListToESParam: function(logicalConnective, filters) {
var esParams = [];
for (var i = 0; i < filters.length; i++) {
esParams.push(" (" + this._kendoFilterToESParam(filters[i]) + ") ");
}
return esParams.join(logicalConnective.toUpperCase())
},
_asESParameter: function(value) {
if (value.constructor == Date)
value = value.toISOString();
return value.replace("\\", "\\\\").replace(/[+\-&|!()\{}\[\]^:"~*?:\/ ]/g, "\\$&");
}
})
})(window.kendo);