Skip to content

Commit

Permalink
Finish #30
Browse files Browse the repository at this point in the history
  • Loading branch information
itayw committed Oct 8, 2015
2 parents bbb0061 + 4340f04 commit 0233110
Showing 1 changed file with 70 additions and 15 deletions.
85 changes: 70 additions & 15 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,7 @@ ElasticSearch.prototype.buildQueryPlan = function(query, callback) {
else
colQuery.type = 'plain';

var _$match = self.common.extend({}, $match);

var _$match = ce.cloneextend({}, $match);
var _$unwind;
if (metric.dependsOn.indexOf('.') > 0 && self.common.checkNestedArray(metric.collection, metric.dependsOn))
_$unwind = '$' + metric.dependsOn.substring(0, metric.dependsOn.indexOf('.')) || metric._key;
Expand All @@ -316,15 +315,74 @@ ElasticSearch.prototype.buildQueryPlan = function(query, callback) {
var _$sort = self.common.extend({}, $sort);

if (metric.filter) {
metric.filter.forEach(function(f) {
if (f[1] == 'eq')
_$match[f[0]] = f[2];
else {
_$match[f[0]] = {};
_$match[f[0]]['$' + f[1]] = f[2];
}
});
try {
metric.filter.forEach(function(f) {
if (f[1] === 'eq') {
var matchPhrase = {};
matchPhrase[f[0]] = f[2];
_$match.query.bool = _$match.query.bool || {
"must": [{
"match_all": {}
}]
};
_$match.query.bool['must'].push({
term: matchPhrase
});
} else if (f[1] === 'in') {
var matchPhrase = {};
matchPhrase[f[0]] = JSON.parse(f[2]);
matchPhrase.execution = 'or';
_$match.filter.bool.must.push({
terms: matchPhrase
});
} else if (f[1] === '_in') {
var matchPhrase = {};
matchPhrase[f[0]] = JSON.parse(f[2]);
matchPhrase.execution = 'and';
_$match.filter.bool.must.push({
terms: matchPhrase
});
} else if (f[1] === 'regex') {
var matchPhrase = {};
matchPhrase[f[0]] = f[2];
_$match.query.bool = _$match.query.bool || {
"must": [{
"match_all": {}
}]
};
_$match.query.bool['must'].push({
regexp: matchPhrase
});
} else if (['gt', 'gte', 'lt', 'lte'].indexOf(f[1]) > -1) {
var matchPhrase = {};
matchPhrase[f[0]] = {};
matchPhrase[f[0]][f[1]] = f[2];
_$match.query.bool = _$match.query.bool || {
"must": [{
"match_all": {}
}]
};
_$match.query.bool['must'].push({
range: matchPhrase
});
} else if (f[1] === 'geo_distance') {
var geo_distance = {
distance: Math.round(f[2][0]) + 'm'
};
geo_distance[f[0]] = f[2][1];
_$match.filter.bool.must.push({
geo_distance: geo_distance
});
} else {
_$match[f[0]] = {};
_$match[f[0]]['$' + f[1]] = f[2];
}
});
} catch (ex) {
return callback(new Error('Failed to parse filter: ' + ex.toString()));
}
}

colQuery.key = self.common.hash(colQuery.type + '_' + metric.collection.key + '_' + JSON.stringify(_$match) + '_' + JSON.stringify(_$unwind) + '_' + metric.key);
if (plan.colQueries[colQuery.key]) {
if (_$unwind)
Expand All @@ -336,14 +394,12 @@ ElasticSearch.prototype.buildQueryPlan = function(query, callback) {
_$group.aggs = {};
if (metric.key !== 'fake') {
_$group[metric.key] = {};
if (metric.aggregation === 'count')
{
if (metric.aggregation === 'count') {
_$group.aggs[metric.key] = {};
_$group.aggs[metric.key]['value_count'] = {
field: metric._key
};
}
else if (metric.aggregation === 'ucount') {
} else if (metric.aggregation === 'ucount') {
_$group.aggs[metric.key] = {};
_$group.aggs[metric.key].cardinality = {};
_$group.aggs[metric.key].cardinality[metric.key] = {
Expand All @@ -363,7 +419,6 @@ ElasticSearch.prototype.buildQueryPlan = function(query, callback) {
$group: _$group,
$sort: _$sort
};

if ($limit) {
colQuery.query.$limit = {
from: 0,
Expand Down

0 comments on commit 0233110

Please sign in to comment.