Skip to content

Commit

Permalink
Put filters back in the filter context where they belong (elastic#42095)
Browse files Browse the repository at this point in the history
Filters were never really meant to take part in scoring in the first place. This PR puts filters back in the filter context where they were in 4.x so they can benefit from the filter cache.
  • Loading branch information
Bargs committed Aug 1, 2019
1 parent 42597aa commit 346a891
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ describe('build query', function () {
bool: {
must: [
decorateQuery(luceneStringToDsl('bar:baz'), config.queryStringOptions),
{ match_all: {} },
],
filter: [
toElasticsearchQuery(fromKueryExpression('extension:jpg'), indexPattern),
{ match_all: {} },
],
should: [],
must_not: [],
Expand All @@ -90,9 +90,8 @@ describe('build query', function () {
bool: {
must: [
decorateQuery(luceneStringToDsl('extension:jpg'), config.queryStringOptions),
{ match_all: {} },
],
filter: [],
filter: [{ match_all: {} }],
should: [],
must_not: [],
}
Expand Down Expand Up @@ -122,9 +121,11 @@ describe('build query', function () {
bool: {
must: [
decorateQuery(luceneStringToDsl('@timestamp:"2019-03-23T13:18:00"'), config.queryStringOptions, config.dateFormatTZ),
],
filter: [
toElasticsearchQuery(fromKueryExpression('@timestamp:"2019-03-23T13:18:00"'), indexPattern, config),
{ match_all: {} }
],
filter: [toElasticsearchQuery(fromKueryExpression('@timestamp:"2019-03-23T13:18:00"'), indexPattern, config)],
should: [],
must_not: [],
}
Expand Down
8 changes: 4 additions & 4 deletions packages/kbn-es-query/src/es_query/__tests__/from_filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('build query', function () {

const result = buildQueryFromFilters(filters);

expect(result.must).to.eql(expectedESQueries);
expect(result.filter).to.eql(expectedESQueries);
});

it('should place negated filters in the must_not clause', function () {
Expand Down Expand Up @@ -86,7 +86,7 @@ describe('build query', function () {

const result = buildQueryFromFilters(filters);

expect(result.must).to.eql(expectedESQueries);
expect(result.filter).to.eql(expectedESQueries);
});

it('should migrate deprecated match syntax', function () {
Expand All @@ -105,7 +105,7 @@ describe('build query', function () {

const result = buildQueryFromFilters(filters);

expect(result.must).to.eql(expectedESQueries);
expect(result.filter).to.eql(expectedESQueries);
});

it('should not add query:queryString:options to query_string filters', function () {
Expand All @@ -119,7 +119,7 @@ describe('build query', function () {

const result = buildQueryFromFilters(filters);

expect(result.must).to.eql(expectedESQueries);
expect(result.filter).to.eql(expectedESQueries);
});
});
});
4 changes: 2 additions & 2 deletions packages/kbn-es-query/src/es_query/from_filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ const cleanFilter = function (filter) {

export function buildQueryFromFilters(filters = [], indexPattern, ignoreFilterIfFieldNotInIndex) {
return {
must: filters
must: [],
filter: filters
.filter(filterNegate(false))
.filter(filter => !ignoreFilterIfFieldNotInIndex || filterMatchesIndex(filter, indexPattern))
.map(translateToQuery)
.map(cleanFilter)
.map(filter => {
return migrateFilter(filter, indexPattern);
}),
filter: [],
should: [],
must_not: filters
.filter(filterNegate(true))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ describe('query(req, panel, series)', () => {
size: 0,
query: {
bool: {
filter: [],
must: [
filter: [
{
bool: {
must: [
Expand All @@ -139,6 +138,8 @@ describe('query(req, panel, series)', () => {
],
},
},
],
must: [
{
range: {
timestamp: {
Expand Down Expand Up @@ -218,8 +219,7 @@ describe('query(req, panel, series)', () => {
size: 0,
query: {
bool: {
filter: [],
must: [
filter: [
{
bool: {
must: [
Expand All @@ -231,6 +231,8 @@ describe('query(req, panel, series)', () => {
],
},
},
],
must: [
{
range: {
timestamp: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ describe('buildRequestBody(req)', () => {
size: 0,
query: {
bool: {
filter: [],
must: [
filter: [
{
bool: {
must: [
Expand All @@ -121,6 +120,8 @@ describe('buildRequestBody(req)', () => {
must_not: [],
},
},
],
must: [
{
range: {
'@timestamp': {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ describe('Terms Agg Other bucket helper', () => {
filters: {
'': {
bool: {
must: [{ exists: { field: 'machine.os.raw' } }],
filter: [],
must: [],
filter: [{ exists: { field: 'machine.os.raw' } }],
should: [],
must_not: [
{ match_phrase: { 'machine.os.raw': { query: 'ios' } } },
Expand All @@ -208,11 +208,11 @@ describe('Terms Agg Other bucket helper', () => {
filters: {
'-IN': {
bool: {
must: [
must: [],
filter: [
{ match_phrase: { 'geo.src': { query: 'IN' } } },
{ exists: { field: 'machine.os.raw' } }
],
filter: [],
should: [],
must_not: [
{ match_phrase: { 'machine.os.raw': { query: 'ios' } } },
Expand All @@ -222,11 +222,11 @@ describe('Terms Agg Other bucket helper', () => {
},
'-US': {
bool: {
must: [
must: [],
filter: [
{ match_phrase: { 'geo.src': { query: 'US' } } },
{ exists: { field: 'machine.os.raw' } }
],
filter: [],
should: [],
must_not: [
{ match_phrase: { 'machine.os.raw': { query: 'ios' } } },
Expand Down

0 comments on commit 346a891

Please sign in to comment.