Commit 873024b9 by bergquist

tech(elastic): use bool conditions for all es queries

parent 6440d884
script.inline: on
script.indexed: on
elasticsearch1:
image: elasticsearch:1.7.6
command: elasticsearch -Des.network.host=0.0.0.0
ports:
- "11200:9200"
- "11300:9300"
volumes:
- ./blocks/elastic/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
......@@ -4,5 +4,5 @@ elasticsearch5:
image: elasticsearch:5
command: elasticsearch
ports:
- "9200:9200"
- "9300:9300"
- "10200:9200"
- "10300:9300"
......@@ -88,7 +88,16 @@ function (angular, _, moment, kbn, ElasticQueryBuilder, IndexPattern, ElasticRes
}
var queryInterpolated = templateSrv.replace(queryString, {}, 'lucene');
var query = { "bool": { "must": [{ "range": range }, { "query_string": { "query": queryInterpolated } }] }};
var query = {
"bool": {
"must": [
{ "range": range },
{ "query_string": {
"query": queryInterpolated }
}
]
}
};
var data = {
"query" : query,
......
......@@ -76,23 +76,13 @@ function (queryDef) {
for (var i = 0; i < aggDef.settings.filters.length; i++) {
var query = aggDef.settings.filters[i].query;
if (this.esVersion >= 5) {
filterObj[query] = {
query_string: {
query: query,
analyze_wildcard: true
}
};
} else {
filterObj[query] = {
query: {
query_string: {
query: query,
analyze_wildcard: true
}
}
};
}
filterObj[query] = {
query_string: {
query: query,
analyze_wildcard: true
}
};
}
return filterObj;
......@@ -119,12 +109,7 @@ function (queryDef) {
}
var i, filter, condition, must;
if (this.esVersion >= 5) {
must = query.query.bool.must;
} else {
must = query.query.filtered.filter.bool.must;
}
must = query.query.bool.must;
for (i = 0; i < adhocFilters.length; i++) {
filter = adhocFilters[i];
......@@ -143,42 +128,21 @@ function (queryDef) {
var i, nestedAggs, metric;
var query = {};
if (this.esVersion >= 5) {
query = {
"size": 0,
"query": {
"bool": {
"must": [
{"range": this.getRangeFilter()},
{"query_string": {
"analyze_wildcard": true,
"query": '$lucene_query'
}
}
]
}
}
};
} else {
query = {
"size": 0,
"query": {
"filtered": {
"query": {
"query_string": {
"analyze_wildcard": true,
"query": '$lucene_query',
}
},
"filter": {
"bool": {
"must": [{"range": this.getRangeFilter()}]
query = {
"size": 0,
"query": {
"bool": {
"must": [
{"range": this.getRangeFilter()},
{"query_string": {
"analyze_wildcard": true,
"query": '$lucene_query'
}
}
}
]
}
};
}
}
};
this.addAdhocFilters(query, adhocFilters);
......@@ -258,47 +222,22 @@ function (queryDef) {
ElasticQueryBuilder.prototype.getTermsQuery = function(queryDef) {
var query;
if (this.esVersion >= 5) {
query = {
"size": 0,
"query": {
"bool": {
"must": [{"range": this.getRangeFilter()}]
}
query = {
"size": 0,
"query": {
"bool": {
"must": [{"range": this.getRangeFilter()}]
}
};
if (queryDef.query) {
query.query.bool.must.push({
"query_string": {
"analyze_wildcard": true,
"query": queryDef.query,
}
});
}
};
} else {
query = {
"size": 0,
"query": {
"filtered": {
"filter": {
"bool": {
"must": [{"range": this.getRangeFilter()}]
}
}
}
if (queryDef.query) {
query.query.bool.must.push({
"query_string": {
"analyze_wildcard": true,
"query": queryDef.query,
}
};
if (queryDef.query) {
query.query.filtered.query = {
"query_string": {
"analyze_wildcard": true,
"query": queryDef.query,
}
};
}
});
}
query.aggs = {
......
......@@ -77,7 +77,7 @@ describe('ElasticDatasource', function() {
it('should json escape lucene query', function() {
var body = angular.fromJson(parts[1]);
expect(body.query.filtered.query.query_string.query).to.be('escape\\:test');
expect(body.query.bool.must[1].query_string.query).to.be('escape\\:test');
});
});
......
......@@ -16,7 +16,7 @@ describe('ElasticQueryBuilder', function() {
bucketAggs: [{type: 'date_histogram', field: '@timestamp', id: '1'}],
});
expect(query.query.filtered.filter.bool.must[0].range["@timestamp"].gte).to.be("$timeFrom");
expect(query.query.bool.must[0].range["@timestamp"].gte).to.be("$timeFrom");
expect(query.aggs["1"].date_histogram.extended_bounds.min).to.be("$timeFrom");
});
......@@ -154,8 +154,8 @@ describe('ElasticQueryBuilder', function() {
],
});
expect(query.aggs["2"].filters.filters["@metric:cpu"].query.query_string.query).to.be("@metric:cpu");
expect(query.aggs["2"].filters.filters["@metric:logins.count"].query.query_string.query).to.be("@metric:logins.count");
expect(query.aggs["2"].filters.filters["@metric:cpu"].query_string.query).to.be("@metric:cpu");
expect(query.aggs["2"].filters.filters["@metric:logins.count"].query_string.query).to.be("@metric:logins.count");
expect(query.aggs["2"].aggs["4"].date_histogram.field).to.be("@timestamp");
});
......@@ -291,7 +291,6 @@ describe('ElasticQueryBuilder', function() {
{key: 'key1', operator: '=', value: 'value1'}
]);
expect(query.query.filtered.filter.bool.must[1].term["key1"]).to.be("value1");
expect(query.query.bool.must[2].term["key1"]).to.be("value1");
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment