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

[KQL] Add case-insensitive config for term/wildcard queries #148916

Merged
merged 4 commits into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
1 change: 1 addition & 0 deletions packages/kbn-es-query/src/es_query/build_es_query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export function buildEsQuery(
dateFormatTZ: config.dateFormatTZ,
filtersInMustClause: config.filtersInMustClause,
nestedIgnoreUnmapped: config.nestedIgnoreUnmapped,
caseInsensitive: config.caseInsensitive,
}
);
const luceneQuery = buildQueryFromLucene(
Expand Down
8 changes: 7 additions & 1 deletion packages/kbn-es-query/src/es_query/from_kuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ export function buildQueryFromKuery(
{ allowLeadingWildcards = false }: { allowLeadingWildcards?: boolean } = {
allowLeadingWildcards: false,
},
{ filtersInMustClause = false, dateFormatTZ, nestedIgnoreUnmapped }: KueryQueryOptions = {
{
filtersInMustClause = false,
dateFormatTZ,
nestedIgnoreUnmapped,
caseInsensitive,
}: KueryQueryOptions = {
filtersInMustClause: false,
}
): BoolQuery {
Expand All @@ -35,6 +40,7 @@ export function buildQueryFromKuery(
filtersInMustClause,
dateFormatTZ,
nestedIgnoreUnmapped,
caseInsensitive,
});
}

Expand Down
40 changes: 38 additions & 2 deletions packages/kbn-es-query/src/kuery/functions/is.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ describe('kuery functions', () => {
should: [
{
wildcard: {
'machine.os.keyword': 'win*',
'machine.os.keyword': { value: 'win*' },
},
},
],
Expand All @@ -225,6 +225,25 @@ describe('kuery functions', () => {
expect(result).toEqual(expected);
});

test('should create a case-insensitive wildcard query for keyword fields', () => {
const expected = {
bool: {
should: [
{
wildcard: {
'machine.os.keyword': { value: 'win*', case_insensitive: true },
},
},
],
minimum_should_match: 1,
},
};
const node = nodeTypes.function.buildNode('is', 'machine.os.keyword', 'win*');
const result = is.toElasticsearchQuery(node, indexPattern, { caseInsensitive: true });

expect(result).toEqual(expected);
});

test('should support scripted fields', () => {
const node = nodeTypes.function.buildNode('is', 'script string', 'foo');
const result = is.toElasticsearchQuery(node, indexPattern);
Expand Down Expand Up @@ -370,7 +389,24 @@ describe('kuery functions', () => {
should: [
{
term: {
'machine.os.keyword': 'Win 7',
'machine.os.keyword': { value: 'Win 7' },
},
},
],
minimum_should_match: 1,
},
});
});

test('should use a case-insensitive term query for keyword fields', () => {
const node = nodeTypes.function.buildNode('is', 'machine.os.keyword', 'Win 7');
const result = is.toElasticsearchQuery(node, indexPattern, { caseInsensitive: true });
expect(result).toEqual({
bool: {
should: [
{
term: {
'machine.os.keyword': { value: 'Win 7', case_insensitive: true },
},
},
],
Expand Down
23 changes: 21 additions & 2 deletions packages/kbn-es-query/src/kuery/functions/is.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,12 @@ export function toElasticsearchQuery(
const query = isKeywordField
? {
wildcard: {
[field.name]: value,
[field.name]: {
value,
...(typeof config.caseInsensitive === 'boolean' && {
case_insensitive: config.caseInsensitive,
}),
},
},
}
: {
Expand Down Expand Up @@ -177,8 +182,22 @@ export function toElasticsearchQuery(
},
}),
];
} else if (isKeywordField) {
return [
...accumulator,
wrapWithNestedQuery({
term: {
[field.name]: {
value,
...(typeof config.caseInsensitive === 'boolean' && {
case_insensitive: config.caseInsensitive,
}),
},
},
}),
];
} else {
const queryType = isKeywordField ? 'term' : type === 'phrase' ? 'match_phrase' : 'match';
const queryType = type === 'phrase' ? 'match_phrase' : 'match';
return [
...accumulator,
wrapWithNestedQuery({
Expand Down
6 changes: 6 additions & 0 deletions packages/kbn-es-query/src/kuery/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ export interface KueryQueryOptions {
* The `nestedIgnoreUnmapped` param allows creating queries with "ignore_unmapped": true
*/
nestedIgnoreUnmapped?: boolean;
/**
* Whether term-level queries should be treated as case-insensitive or not. For example, `agent.keyword: foobar` won't
* match a value of "FooBar" unless this parameter is `true`.
* (See https://www.elastic.co/guide/en/elasticsearch/reference/8.6/query-dsl-term-query.html#term-field-params)
*/
caseInsensitive?: boolean;
}

/** @public */
Expand Down