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

Don't count lone wildcards as leading wildcards #17462

Merged
merged 2 commits into from
Mar 30, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 16 additions & 0 deletions src/ui/public/kuery/node_types/__tests__/wildcard.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

});

});
17 changes: 14 additions & 3 deletions src/ui/public/kuery/node_types/wildcard.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('.*');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just reformatting via Prettier

const regexp = new RegExp(`^${regex}$`);
return regexp.test(string);
}
Expand All @@ -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('*');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just reformatting via Prettier

}

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
);
}