Skip to content

Commit

Permalink
Fixed search bar query parser (#413)
Browse files Browse the repository at this point in the history
- relaxed the support for phrases - we now accept spaces anywhere in the phrase as long as it contains at least one term - the phrases are trimmed
- it's now possible to have a single term OR clause
  • Loading branch information
uboness authored Feb 15, 2018
1 parent f36aab1 commit 3b84dd9
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/components/search_bar/query/default_syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ fieldValue "field value"
fieldValues
= "(" space? head:value tail:(
space ([oO][rR]) space value:value space { return value; }
)+ space? ")" { return [ head, ...tail ] }
space ([oO][rR]) space value:value { return value; }
)* space? ")" { return [ head, ...tail ] }
termValue "term"
= value
value
= word
/ '"' phrase:phrase '"' { return phrase; }
/ '"' space? phrase:phrase space?'"' { return phrase; }
phrase
= word (space word)* { return unescapeValue(text()); }
Expand All @@ -89,10 +89,10 @@ reservedChar
= [:\\-\\\\]
alnum "alpha numeric"
= [a-zA-Z0-9]+
= [a-zA-Z0-9]
space "whitespace"
= [ \\t\\n\\r]*
= [ \\t\\n\\r]+
`;

const printValue = (value) => {
Expand Down
39 changes: 39 additions & 0 deletions src/components/search_bar/query/default_syntax.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,4 +411,43 @@ describe('defaultSyntax', () => {
expect(printedQuery).toBe(query);
});

test('relaxed phrases with spaces', () => {

const query = `f:" this is a relaxed phrase \t"`;
const ast = defaultSyntax.parse(query);

expect(ast).toBeDefined();
expect(ast.clauses).toHaveLength(1);

const clause = ast.getSimpleFieldClause('f');
expect(clause).toBeDefined();
expect(AST.Field.isInstance(clause)).toBe(true);
expect(AST.Match.isMustClause(clause)).toBe(true);
expect(clause.field).toBe('f');
expect(clause.value).toBe('this is a relaxed phrase');

const printedQuery = defaultSyntax.print(ast);
expect(printedQuery).toBe(`f:"this is a relaxed phrase"`);
});

test('single term or expression', () => {

const query = `f:(foo)`;
const ast = defaultSyntax.parse(query);

expect(ast).toBeDefined();
expect(ast.clauses).toHaveLength(1);

const clause = ast.getOrFieldClause('f');
expect(clause).toBeDefined();
expect(AST.Field.isInstance(clause)).toBe(true);
expect(AST.Match.isMustClause(clause)).toBe(true);
expect(clause.field).toBe('f');
expect(clause.value).toHaveLength(1);
expect(clause.value).toContain('foo');

const printedQuery = defaultSyntax.print(ast);
expect(printedQuery).toBe(query);
});

});

0 comments on commit 3b84dd9

Please sign in to comment.