From c0de6adaeef98523e23c716a330ce592e732c586 Mon Sep 17 00:00:00 2001 From: Matthew Bargar Date: Thu, 29 Mar 2018 14:59:45 -0400 Subject: [PATCH 1/2] Don't count lone wildcards as leading wildcards --- src/ui/public/kuery/node_types/wildcard.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/ui/public/kuery/node_types/wildcard.js b/src/ui/public/kuery/node_types/wildcard.js index 0cd52b802695d..870546e59c276 100644 --- a/src/ui/public/kuery/node_types/wildcard.js +++ b/src/ui/public/kuery/node_types/wildcard.js @@ -25,7 +25,10 @@ export function buildNode(value) { export function test(node, string) { const { value } = node; - const regex = value.split(wildcardSymbol).map(escapeRegExp).join('.*'); + const regex = value + .split(wildcardSymbol) + .map(escapeRegExp) + .join('.*'); const regexp = new RegExp(`^${regex}$`); return regexp.test(string); } @@ -37,10 +40,18 @@ export function toElasticsearchQuery(node) { export function toQueryStringQuery(node) { const { value } = node; - return value.split(wildcardSymbol).map(escapeQueryString).join('*'); + return value + .split(wildcardSymbol) + .map(escapeQueryString) + .join('*'); } export function hasLeadingWildcard(node) { const { value } = node; - return value.startsWith(wildcardSymbol); + // A lone wildcard turns into an `exists` query, so we're only concerned with + // leading wildcards followed by additional characters. + return ( + value.startsWith(wildcardSymbol) && + value.replace(wildcardSymbol, '').length > 0 + ); } From 9b4a7165ab757e50f5bf4fde9794f1aa642b082d Mon Sep 17 00:00:00 2001 From: Matthew Bargar Date: Thu, 29 Mar 2018 15:37:24 -0400 Subject: [PATCH 2/2] add tests for hasLeadingWildcard --- .../kuery/node_types/__tests__/wildcard.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/ui/public/kuery/node_types/__tests__/wildcard.js b/src/ui/public/kuery/node_types/__tests__/wildcard.js index aed0b795e67c1..66cb919488789 100644 --- a/src/ui/public/kuery/node_types/__tests__/wildcard.js +++ b/src/ui/public/kuery/node_types/__tests__/wildcard.js @@ -62,6 +62,22 @@ describe('kuery node types', function () { }); + describe('hasLeadingWildcard', function () { + it('should determine whether a wildcard node contains a leading wildcard', function () { + const node = wildcard.buildNode('foo*bar'); + expect(wildcard.hasLeadingWildcard(node)).to.be(false); + + const leadingWildcardNode = wildcard.buildNode('*foobar'); + expect(wildcard.hasLeadingWildcard(leadingWildcardNode)).to.be(true); + }); + + // Lone wildcards become exists queries, so we aren't worried about their performance + it('should not consider a lone wildcard to be a leading wildcard', function () { + const leadingWildcardNode = wildcard.buildNode('*'); + expect(wildcard.hasLeadingWildcard(leadingWildcardNode)).to.be(false); + }); + }); + }); });