Skip to content

Commit

Permalink
Improve query modifications, partial fix #56 #96
Browse files Browse the repository at this point in the history
  • Loading branch information
ddelemeny committed Mar 5, 2024
1 parent 2843709 commit 788fd75
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
15 changes: 5 additions & 10 deletions src/datasource/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<BaseQuickwitDataSource>

Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 6 additions & 2 deletions src/utils/lucene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 788fd75

Please sign in to comment.