mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 09:48:58 -04:00
Simplify wildcard handling
This commit is contained in:
parent
f6027981b2
commit
915861beab
4 changed files with 34 additions and 39 deletions
|
@ -6,7 +6,7 @@ import { nodeTypes } from '../node_types/index';
|
|||
|
||||
const legacyKueryParser = PEG.buildParser(legacyKueryGrammar);
|
||||
const kueryParser = PEG.buildParser(kueryGrammar, {
|
||||
allowedStartRules: ['start', 'Literal'],
|
||||
allowedStartRules: ['start', 'Literal', 'WildcardString'],
|
||||
});
|
||||
|
||||
export function fromLiteralExpression(expression, parseOptions) {
|
||||
|
@ -18,6 +18,15 @@ export function fromLiteralExpression(expression, parseOptions) {
|
|||
return fromExpression(expression, parseOptions, kueryParser);
|
||||
}
|
||||
|
||||
export function fromWildcardExpression(expression, parseOptions) {
|
||||
parseOptions = {
|
||||
...parseOptions,
|
||||
startRule: 'WildcardString',
|
||||
};
|
||||
|
||||
return fromExpression(expression, parseOptions, kueryParser);
|
||||
}
|
||||
|
||||
export function fromLegacyKueryExpression(expression, parseOptions) {
|
||||
return fromExpression(expression, parseOptions, legacyKueryParser);
|
||||
}
|
||||
|
|
|
@ -1 +1 @@
|
|||
export { fromLegacyKueryExpression, fromKueryExpression, fromLiteralExpression, toElasticsearchQuery } from './ast';
|
||||
export * from './ast';
|
||||
|
|
|
@ -5,14 +5,6 @@
|
|||
const buildLiteralNode = nodeTypes.literal.buildNode;
|
||||
const buildWildcardNode = nodeTypes.wildcard.buildNode;
|
||||
const buildNamedArgNode = nodeTypes.namedArg.buildNode;
|
||||
|
||||
function trimLeft(string) {
|
||||
return string.replace(/^[\s\uFEFF\xA0]+/g, '');
|
||||
}
|
||||
|
||||
function trimRight(string) {
|
||||
return string.replace(/[\s\uFEFF\xA0]+$/g, '');
|
||||
}
|
||||
}
|
||||
|
||||
start
|
||||
|
@ -161,10 +153,6 @@ Value
|
|||
const isPhrase = buildLiteralNode(true);
|
||||
return (field) => buildFunctionNode('is', [field, value, isPhrase]);
|
||||
}
|
||||
/ value:WildcardString {
|
||||
const isPhrase = buildLiteralNode(false);
|
||||
return (field) => buildFunctionNode('is', [field, value, isPhrase]);
|
||||
}
|
||||
/ value:UnquotedLiteral {
|
||||
if (value.type === 'cursor') return value;
|
||||
const isPhrase = buildLiteralNode(false);
|
||||
|
@ -181,7 +169,7 @@ Not
|
|||
= 'not'i Space+
|
||||
|
||||
Literal
|
||||
= QuotedString / WildcardString / UnquotedLiteral
|
||||
= QuotedString / UnquotedLiteral
|
||||
|
||||
QuotedString
|
||||
= '"' prefix:QuotedCharacter* cursor:Cursor suffix:QuotedCharacter* '"' {
|
||||
|
@ -203,25 +191,19 @@ QuotedCharacter
|
|||
|
||||
WildcardString
|
||||
= sequences:WildcardSequence+ {
|
||||
const compactedSequences = sequences.reduce((acc, arr, i) => {
|
||||
const compacted = arr.filter(value => value !== '');
|
||||
return [...acc, ...compacted];
|
||||
}, []);
|
||||
if (typeof compactedSequences[0] === 'string') {
|
||||
compactedSequences[0] = trimLeft(compactedSequences[0]);
|
||||
}
|
||||
const lastIndex = compactedSequences.length - 1;
|
||||
if (typeof compactedSequences[lastIndex] === 'string') {
|
||||
compactedSequences[lastIndex] = trimRight(compactedSequences[lastIndex]);
|
||||
}
|
||||
return buildWildcardNode(compactedSequences);
|
||||
return sequences.reduce((acc, arr) => [...acc, ...arr]);
|
||||
}
|
||||
|
||||
WildcardSequence
|
||||
= left:UnquotedCharacter* '*' right:UnquotedCharacter* {
|
||||
return [left.join(''), nodeTypes.wildcard.wildcardSymbol, right.join('')];
|
||||
= left:WildcardCharacter* '*' right:WildcardCharacter* {
|
||||
const sequences = [left.join(''), nodeTypes.wildcard.wildcardSymbol, right.join('')];
|
||||
return sequences.filter(value => value !== '');
|
||||
}
|
||||
|
||||
WildcardCharacter
|
||||
= EscapedWildcard
|
||||
/ !'*' char:UnquotedCharacter { return char; }
|
||||
|
||||
UnquotedLiteral
|
||||
= prefix:UnquotedCharacter* cursor:Cursor suffix:UnquotedCharacter* {
|
||||
const { start, end } = location();
|
||||
|
@ -238,13 +220,18 @@ UnquotedLiteral
|
|||
if (sequence === 'null') return buildLiteralNode(null);
|
||||
if (sequence === 'true') return buildLiteralNode(true);
|
||||
if (sequence === 'false') return buildLiteralNode(false);
|
||||
const number = Number(sequence);
|
||||
const value = isNaN(number) ? sequence : number;
|
||||
return buildLiteralNode(value);
|
||||
try {
|
||||
return buildWildcardNode(sequence);
|
||||
} catch (e) {
|
||||
const number = Number(sequence);
|
||||
const value = isNaN(number) ? sequence : number;
|
||||
return buildLiteralNode(value);
|
||||
}
|
||||
}
|
||||
|
||||
UnquotedCharacter
|
||||
= EscapedSpecialCharacter
|
||||
/ EscapedWildcard
|
||||
/ !Cursor !Separator char:. { return char; }
|
||||
|
||||
OptionalSpace
|
||||
|
@ -263,6 +250,9 @@ OptionalSpace
|
|||
EscapedSpecialCharacter
|
||||
= '\\' char:SpecialCharacter { return char; }
|
||||
|
||||
EscapedWildcard
|
||||
= '\\' char:'*' { return char; }
|
||||
|
||||
EscapedDoubleQuote
|
||||
= '\\' char:'"' { return char; }
|
||||
|
||||
|
@ -273,7 +263,7 @@ Keyword
|
|||
= Or / And / Not
|
||||
|
||||
SpecialCharacter
|
||||
= [\\():<>"*]
|
||||
= [\\():<>"]
|
||||
|
||||
RangeOperator
|
||||
= '<=' { return 'lte'; }
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { fromLiteralExpression } from '../ast/ast';
|
||||
import { fromWildcardExpression } from '../ast/ast';
|
||||
|
||||
export const wildcardSymbol = Symbol('*');
|
||||
|
||||
|
@ -13,13 +13,9 @@ function escapeQueryString(string) {
|
|||
}
|
||||
|
||||
export function buildNode(value) {
|
||||
if (typeof value === 'string') {
|
||||
return fromLiteralExpression(value);
|
||||
}
|
||||
|
||||
return {
|
||||
type: 'wildcard',
|
||||
value,
|
||||
value: fromWildcardExpression(value)
|
||||
};
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue