Skip to content
Open
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
41 changes: 39 additions & 2 deletions packages/common-utils/src/queryParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ function decodeSpecialTokens(query: string): string {
.replace('http_COLON_//', 'http://')
.replace('https_COLON_//', 'https://')
.replace(/localhost_COLON_(\d{1,5})/, 'localhost:$1')
.replace(/HDX_COLON/g, ':');
.replace(/HDX_COLON/g, ':')
.replace(/\\ /g, ' ');
}

export function parse(query: string): lucene.AST {
return lucene.parse(encodeSpecialTokens(query));
return lucene.parse(mergeImplicitTerms(encodeSpecialTokens(query)));
}

const IMPLICIT_FIELD = '<implicit>';
Expand Down Expand Up @@ -801,3 +802,39 @@ export async function genEnglishExplanation(query: string): Promise<string> {

return `Message containing ${query}`;
}

const isNodeTerm = (node: any): boolean => {
if (!node) return false;
return typeof (node as any).term === 'string';
};

// Spaces are recognized without the \ character.
// Querying a b is equivalent to querying a\ b.
const mergeImplicitTerms = (node: any): any => {
if (!node) return node;

const left = mergeImplicitTerms(node.left);
const right = mergeImplicitTerms(node.right);

if (
node.operator === '<implicit>' &&
isNodeTerm(left) &&
isNodeTerm(right) &&
left.field === right.field &&
left.field === IMPLICIT_FIELD &&
!left.quoted &&
!right.quoted
) {
return {
...left,
term: `${left.term} ${right.term}`,
quoted: false,
};
}

return {
...node,
left,
right,
};
};