Commit e91cf28f by Torkel Ödegaard

elasticsearch: changed ad hoc filters from using term filters to using phrase…

elasticsearch: changed ad hoc filters from using term filters to using phrase match queries, closing #9095
parent 3b9fbd60
define([ define([
'./query_def' './query_def',
'lodash',
], ],
function (queryDef) { function (queryDef, _) {
'use strict'; 'use strict';
function ElasticQueryBuilder(options) { function ElasticQueryBuilder(options) {
...@@ -133,17 +134,20 @@ function (queryDef) { ...@@ -133,17 +134,20 @@ function (queryDef) {
return; return;
} }
var i, filter, condition; 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 = {};
condition[filter.key] = filter.value; condition[filter.key] = filter.value;
queryCondition = {};
queryCondition[filter.key] = {query: filter.value};
switch(filter.operator){ switch(filter.operator){
case "=": case "=":
query.query.bool.filter.push({"term": condition}); _.set(query, "query.bool.must.match_phrase", queryCondition);
break; break;
case "!=": case "!=":
query.query.bool.filter.push({"bool": {"must_not": {"term": condition}}}); _.set(query, "query.bool.must_not.match_phrase", queryCondition);
break; break;
case "<": case "<":
condition[filter.key] = {"lt": filter.value}; condition[filter.key] = {"lt": filter.value};
......
...@@ -289,11 +289,11 @@ describe('ElasticQueryBuilder', function() { ...@@ -289,11 +289,11 @@ describe('ElasticQueryBuilder', function() {
{key: 'key6', operator: '!~', value: 'value6'}, {key: 'key6', operator: '!~', value: 'value6'},
]); ]);
expect(query.query.bool.filter[2].term["key1"]).to.be("value1"); expect(query.query.bool.must.match_phrase["key1"].query).to.be("value1");
expect(query.query.bool.filter[3].bool.must_not.term["key2"]).to.be("value2"); expect(query.query.bool.must_not.match_phrase["key2"].query).to.be("value2");
expect(query.query.bool.filter[4].range["key3"].lt).to.be("value3"); expect(query.query.bool.filter[2].range["key3"].lt).to.be("value3");
expect(query.query.bool.filter[5].range["key4"].gt).to.be("value4"); expect(query.query.bool.filter[3].range["key4"].gt).to.be("value4");
expect(query.query.bool.filter[6].regexp["key5"]).to.be("value5"); expect(query.query.bool.filter[4].regexp["key5"]).to.be("value5");
expect(query.query.bool.filter[7].bool.must_not.regexp["key6"]).to.be("value6"); expect(query.query.bool.filter[5].bool.must_not.regexp["key6"]).to.be("value6");
}); });
}); });
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