From 788fd7560b01848c54924616bf2def5d183994d8 Mon Sep 17 00:00:00 2001 From: Damien de Lemeny Date: Fri, 16 Feb 2024 18:12:27 -0500 Subject: [PATCH] Improve query modifications, partial fix #56 #96 --- src/datasource/base.ts | 15 +++++---------- src/utils/lucene.ts | 8 ++++++-- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/datasource/base.ts b/src/datasource/base.ts index 719b66c..066d927 100644 --- a/src/datasource/base.ts +++ b/src/datasource/base.ts @@ -35,6 +35,7 @@ import { getQueryResponseProcessor } from 'datasource/processResponse'; import { SECOND } from 'utils/time'; import { GConstructor } from 'utils/mixins'; +import { LuceneQuery } from '@/utils/lucene'; export type BaseQuickwitDataSourceConstructor = GConstructor @@ -113,24 +114,18 @@ export class BaseQuickwitDataSource return query; } - let expression = query.query ?? ''; + let lquery = LuceneQuery.parse(query.query ?? '') switch (action.type) { case 'ADD_FILTER': { - if (expression.length > 0) { - expression += ' AND '; - } - expression += `${action.options.key}:"${action.options.value}"`; + lquery = lquery.addFilter(action.options.key, action.options.value) break; } case 'ADD_FILTER_OUT': { - if (expression.length > 0) { - expression += ' AND '; - } - expression += `-${action.options.key}:"${action.options.value}"`; + lquery = lquery.addFilter(action.options.key, action.options.value, '-') break; } } - return { ...query, query: expression }; + return { ...query, query: lquery.toString() }; } getDataQueryRequest(queryDef: TermsQuery, range: TimeRange) { diff --git a/src/utils/lucene.ts b/src/utils/lucene.ts index 8b3580a..99e0275 100644 --- a/src/utils/lucene.ts +++ b/src/utils/lucene.ts @@ -142,11 +142,15 @@ function removeNodeFromTree(ast: AST, node: NodeTerm): AST { /** * Merge a query with a filter. */ -export function concatenate(query: string, filter: string, condition = 'AND'): string { +export function concatenate(query: string, filter: string, operator?: 'AND'|'OR'): string { if (!filter) { return query; } - return query.trim() === '' ? filter : `${query} ${condition} ${filter}`; + if (query.trim() === '' ) { + return filter; + } + + return operator ? `${query} ${operator} ${filter}` : `${query} ${filter}` } export class LuceneQuery {