Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support nested fields in existing filter types #49537

Merged
merged 16 commits into from
Dec 9, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,67 +17,75 @@
* under the License.
*/

import { handleNestedFilter } from './handle_nested_filter';
import { fields } from '../../index_patterns/mocks';
import { buildPhraseFilter, buildQueryFilter } from '../filters';
import { IFieldType, IIndexPattern } from '../../index_patterns';

import { handleNestedFilter } from '../handle_nested_filter';
import { indexPatternResponse as indexPattern } from '../../__fixtures__/index_pattern_response';
import { buildPhraseFilter, buildQueryFilter } from '../../filters';
import expect from '@kbn/expect';
describe('handleNestedFilter', function() {
const indexPattern: IIndexPattern = ({
id: 'logstash-*',
fields,
} as unknown) as IIndexPattern;

describe('handleNestedFilter', function () {

it('should return the filter\'s query wrapped in nested query if the target field is nested', () => {
it("should return the filter's query wrapped in nested query if the target field is nested", () => {
const field = getField('nestedField.child');
const filter = buildPhraseFilter(field, 'foo', indexPattern);
const filter = buildPhraseFilter(field!, 'foo', indexPattern);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I recall from somewhere that we're trying not to use the ! syntax anymore in favor of explicitly checking for the presence of the item but can't remember from where. I personally prefer being more explicit, using an if condition that's not as easy to accidentally delete or omit.

const result = handleNestedFilter(filter, indexPattern);
expect(result).to.eql({
expect(result).toEqual({
meta: {
index: 'logstash-*',
},
nested: {
path: 'nestedField',
query: {
match_phrase: {
'nestedField.child': 'foo'
}
}
}
}
);
'nestedField.child': 'foo',
},
},
},
});
});

it('should return filter untouched if it does not target a nested field', () => {
const field = getField('extension');
const filter = buildPhraseFilter(field, 'jpg', indexPattern);
const filter = buildPhraseFilter(field!, 'jpg', indexPattern);
const result = handleNestedFilter(filter, indexPattern);
expect(result).to.be(filter);
expect(result).toBe(filter);
});

it('should return filter untouched if it does not target a field from the given index pattern', () => {
const field = { ...getField('extension'), name: 'notarealfield' };
const filter = buildPhraseFilter(field, 'jpg', indexPattern);
const filter = buildPhraseFilter(field as IFieldType, 'jpg', indexPattern);
const result = handleNestedFilter(filter, indexPattern);
expect(result).to.be(filter);
expect(result).toBe(filter);
});

it('should return filter untouched if no index pattern is provided', () => {
const field = getField('extension');
const filter = buildPhraseFilter(field, 'jpg', indexPattern);
const filter = buildPhraseFilter(field!, 'jpg', indexPattern);
const result = handleNestedFilter(filter);
expect(result).to.be(filter);
expect(result).toBe(filter);
});

it('should return the filter untouched if a target field cannot be determined', () => {
// for example, we don't support query_string queries
const filter = buildQueryFilter({
query: {
query_string: {
query: 'response:200'
}
}
}, indexPattern.id);
const filter = buildQueryFilter(
{
query: {
query_string: {
query: 'response:200',
},
},
},
'logstash-*',
'foo'
);
const result = handleNestedFilter(filter);
expect(result).to.be(filter);
expect(result).toBe(filter);
});
});


function getField(name) {
return indexPattern.fields.find(field => field.name === name);
}
function getField(name: string) {
return indexPattern.fields.find(field => field.name === name);
}
});