Commit 4e387ed4 by Torkel Ödegaard

fix: Elasticsearch and adhoc filters fix, fixes #9165

parent 52676161
...@@ -135,6 +135,7 @@ function (queryDef, _) { ...@@ -135,6 +135,7 @@ function (queryDef, _) {
} }
var i, filter, condition, queryCondition; var i, filter, condition, queryCondition;
for (i = 0; i < adhocFilters.length; i++) { for (i = 0; i < adhocFilters.length; i++) {
filter = adhocFilters[i]; filter = adhocFilters[i];
condition = {}; condition = {};
...@@ -144,10 +145,12 @@ function (queryDef, _) { ...@@ -144,10 +145,12 @@ function (queryDef, _) {
switch(filter.operator){ switch(filter.operator){
case "=": case "=":
_.set(query, "query.bool.must.match_phrase", queryCondition); if (!query.query.bool.must) { query.query.bool.must = []; }
query.query.bool.must.push({match_phrase: queryCondition});
break; break;
case "!=": case "!=":
_.set(query, "query.bool.must_not.match_phrase", queryCondition); if (!query.query.bool.must_not) { query.query.bool.must_not = []; }
query.query.bool.must_not.push({match_phrase: queryCondition});
break; break;
case "<": case "<":
condition[filter.key] = {"lt": filter.value}; condition[filter.key] = {"lt": filter.value};
......
...@@ -40,8 +40,7 @@ describe('ElasticQueryBuilder', function() { ...@@ -40,8 +40,7 @@ describe('ElasticQueryBuilder', function() {
var query = builder.build({ var query = builder.build({
metrics: [{type: 'count', id: '1'}], metrics: [{type: 'count', id: '1'}],
timeField: '@timestamp', timeField: '@timestamp',
bucketAggs: [ bucketAggs: [ {type: 'terms', field: '@host', id: '2'},
{type: 'terms', field: '@host', id: '2'},
{type: 'date_histogram', field: '@timestamp', id: '3'} {type: 'date_histogram', field: '@timestamp', id: '3'}
], ],
}); });
...@@ -282,6 +281,7 @@ describe('ElasticQueryBuilder', function() { ...@@ -282,6 +281,7 @@ describe('ElasticQueryBuilder', function() {
bucketAggs: [{type: 'date_histogram', field: '@timestamp', id: '3'}], bucketAggs: [{type: 'date_histogram', field: '@timestamp', id: '3'}],
}, [ }, [
{key: 'key1', operator: '=', value: 'value1'}, {key: 'key1', operator: '=', value: 'value1'},
{key: 'key2', operator: '=', value: 'value2'},
{key: 'key2', operator: '!=', value: 'value2'}, {key: 'key2', operator: '!=', value: 'value2'},
{key: 'key3', operator: '<', value: 'value3'}, {key: 'key3', operator: '<', value: 'value3'},
{key: 'key4', operator: '>', value: 'value4'}, {key: 'key4', operator: '>', value: 'value4'},
...@@ -289,8 +289,9 @@ describe('ElasticQueryBuilder', function() { ...@@ -289,8 +289,9 @@ describe('ElasticQueryBuilder', function() {
{key: 'key6', operator: '!~', value: 'value6'}, {key: 'key6', operator: '!~', value: 'value6'},
]); ]);
expect(query.query.bool.must.match_phrase["key1"].query).to.be("value1"); expect(query.query.bool.must[0].match_phrase["key1"].query).to.be("value1");
expect(query.query.bool.must_not.match_phrase["key2"].query).to.be("value2"); expect(query.query.bool.must[1].match_phrase["key2"].query).to.be("value2");
expect(query.query.bool.must_not[0].match_phrase["key2"].query).to.be("value2");
expect(query.query.bool.filter[2].range["key3"].lt).to.be("value3"); expect(query.query.bool.filter[2].range["key3"].lt).to.be("value3");
expect(query.query.bool.filter[3].range["key4"].gt).to.be("value4"); expect(query.query.bool.filter[3].range["key4"].gt).to.be("value4");
expect(query.query.bool.filter[4].regexp["key5"]).to.be("value5"); expect(query.query.bool.filter[4].regexp["key5"]).to.be("value5");
......
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