Query the index defined default_field when a field isn't explicitly provided (#15101)

In 7.0 ES put a limit on the number of fields that a query can expand to. Since a search term without an explicit field selection hits all fields by default, a kibana user could get an error if the index they're searching against has more than 1024 fields. With lucene, the ES admin could fix this by changing the default_field in their index settings. Kuery still had a problem though, because multi_match did not support default_field. The ES team kindly added default_field support to multi_match for us, so this PR updates Kuery to rely on default_field when a user submits a query without an explicit field.
This commit is contained in:
Matt Bargar 2017-11-28 13:51:19 -05:00 committed by GitHub
parent f343059597
commit c7ce51b5f8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 32 additions and 9 deletions

View file

@ -53,9 +53,9 @@ describe('kuery functions', function () {
);
});
it('should wrap a literal argument with an "is" function targeting all fields', function () {
it('should wrap a literal argument with an "is" function targeting the default_field', function () {
const literalFoo = nodeTypes.literal.buildNode('foo');
const expectedChild = ast.toElasticsearchQuery(nodeTypes.function.buildNode('is', '*', 'foo'), indexPattern);
const expectedChild = ast.toElasticsearchQuery(nodeTypes.function.buildNode('is', null, 'foo'), indexPattern);
const node = nodeTypes.function.buildNode('and', [literalFoo]);
const result = and.toElasticsearchQuery(node, indexPattern);
const resultChild = result.bool.filter[0];

View file

@ -57,6 +57,20 @@ describe('kuery functions', function () {
expectDeepEqual(result, expected);
});
it('should return an ES multi_match query using default_field when fieldName is null', function () {
const expected = {
multi_match: {
query: 200,
type: 'phrase',
lenient: true,
}
};
const node = nodeTypes.function.buildNode('is', null, 200);
const result = is.toElasticsearchQuery(node, indexPattern);
expectDeepEqual(result, expected);
});
it('should return an ES multi_match query when fieldName is "*"', function () {
const expected = {
multi_match: {

View file

@ -48,9 +48,9 @@ describe('kuery functions', function () {
expect(result.bool.must_not).to.eql(ast.toElasticsearchQuery(childNode, indexPattern));
});
it('should wrap a literal argument with an "is" function targeting all fields', function () {
it('should wrap a literal argument with an "is" function targeting the default_field', function () {
const literalFoo = nodeTypes.literal.buildNode('foo');
const expectedChild = ast.toElasticsearchQuery(nodeTypes.function.buildNode('is', '*', 'foo'), indexPattern);
const expectedChild = ast.toElasticsearchQuery(nodeTypes.function.buildNode('is', null, 'foo'), indexPattern);
const node = nodeTypes.function.buildNode('not', literalFoo);
const result = not.toElasticsearchQuery(node, indexPattern);
const resultChild = result.bool.must_not;

View file

@ -53,9 +53,9 @@ describe('kuery functions', function () {
);
});
it('should wrap a literal argument with an "is" function targeting all fields', function () {
it('should wrap a literal argument with an "is" function targeting the default_field', function () {
const literalFoo = nodeTypes.literal.buildNode('foo');
const expectedChild = ast.toElasticsearchQuery(nodeTypes.function.buildNode('is', '*', 'foo'), indexPattern);
const expectedChild = ast.toElasticsearchQuery(nodeTypes.function.buildNode('is', null, 'foo'), indexPattern);
const node = nodeTypes.function.buildNode('or', [literalFoo]);
const result = or.toElasticsearchQuery(node, indexPattern);
const resultChild = result.bool.should[0];

View file

@ -15,7 +15,7 @@ export function toElasticsearchQuery(node, indexPattern) {
bool: {
filter: children.map((child) => {
if (child.type === 'literal') {
child = nodeTypes.function.buildNode('is', '*', child.value);
child = nodeTypes.function.buildNode('is', null, child.value);
}
return ast.toElasticsearchQuery(child, indexPattern);

View file

@ -29,6 +29,15 @@ export function toElasticsearchQuery(node, indexPattern) {
}
};
}
else if (fieldName === null) {
return {
multi_match: {
query: value,
type: 'phrase',
lenient: true,
}
};
}
else if (fieldName === '*' && value === '*') {
return { match_all: {} };
}

View file

@ -11,7 +11,7 @@ export function buildNodeParams(child, serializeStyle = 'operator') {
export function toElasticsearchQuery(node, indexPattern) {
let [ argument ] = node.arguments;
if (argument.type === 'literal') {
argument = nodeTypes.function.buildNode('is', '*', argument.value);
argument = nodeTypes.function.buildNode('is', null, argument.value);
}
return {

View file

@ -15,7 +15,7 @@ export function toElasticsearchQuery(node, indexPattern) {
bool: {
should: children.map((child) => {
if (child.type === 'literal') {
child = nodeTypes.function.buildNode('is', '*', child.value);
child = nodeTypes.function.buildNode('is', null, child.value);
}
return ast.toElasticsearchQuery(child, indexPattern);