From 2cb273fdeec7d8beb360d322f7128e6aca40ad29 Mon Sep 17 00:00:00 2001 From: William Chu Date: Sat, 17 Aug 2024 15:09:46 +1000 Subject: [PATCH] feat(autocomplete): use query to prefix search autocompletion results --- src/components/LuceneQueryEditor.tsx | 2 +- src/datasource/base.ts | 17 +++++++++-------- src/datasource/utils.ts | 4 ++-- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/components/LuceneQueryEditor.tsx b/src/components/LuceneQueryEditor.tsx index a0469ef..9a7aa31 100644 --- a/src/components/LuceneQueryEditor.tsx +++ b/src/components/LuceneQueryEditor.tsx @@ -40,7 +40,7 @@ export function LuceneQueryEditor(props: LuceneQueryEditorProps){ if (!word){ return null } suggestions = await autocompleter(word?.text); if (suggestions && suggestions.options.length > 0 ) { - // Fixes autocompletion inserting an extra quote when the cursor is before a quote + // Fixes autocompletion inserting an extra quote when the cursor is immediately before a quote const cursorIsBeforeQuote = context.state.doc.toString().slice(context.pos, context.pos + 1) === '"'; if (cursorIsBeforeQuote) { suggestions.options = suggestions.options.map(o => ({...o, apply: `${o.label.replace(/"$/g, '')}`})); diff --git a/src/datasource/base.ts b/src/datasource/base.ts index 4a91cdf..64bbcb4 100644 --- a/src/datasource/base.ts +++ b/src/datasource/base.ts @@ -17,9 +17,9 @@ import { TimeRange, } from '@grafana/data'; import { BucketAggregation, DataLinkConfig, ElasticsearchQuery, TermsQuery, FieldCapabilitiesResponse } from '@/types'; -import { - DataSourceWithBackend, - getTemplateSrv, +import { + DataSourceWithBackend, + getTemplateSrv, TemplateSrv } from '@grafana/runtime'; import { QuickwitOptions } from 'quickwit'; import { getDataQuery } from 'QueryBuilder/elastic'; @@ -36,7 +36,7 @@ import { getQueryResponseProcessor } from 'datasource/processResponse'; import { SECOND } from 'utils/time'; import { GConstructor } from 'utils/mixins'; import { LuceneQuery } from '@/utils/lucene'; -import { uidMaker } from "@/utils/uid" +import { uidMaker } from "@/utils/uid" import { DefaultsConfigOverrides } from 'store/defaults/conf'; export type BaseQuickwitDataSourceConstructor = GConstructor @@ -199,7 +199,7 @@ export class BaseQuickwitDataSource .map(field_capability => { return { text: field_capability.field_name, - value: fieldTypeMap[field_capability.type], + value: fieldTypeMap[field_capability.type], } }); const uniquefieldCapabilities = fieldCapabilities.filter((field_capability, index, self) => @@ -223,9 +223,10 @@ export class BaseQuickwitDataSource /** * Get tag values for adhoc filters */ - getTagValues(options: any) { - const terms = this.getTerms({ field: options.key }, options.timeRange) - return lastValueFrom(terms, {defaultValue:[]}); + getTagValues(options: { key: string, fieldValue: string, timeRange: TimeRange }) { + const query = `${options.key}:${options.fieldValue}*` + const terms = this.getTerms({ field: options.key, query }, options.timeRange) + return lastValueFrom(terms, { defaultValue: [] }); } /** diff --git a/src/datasource/utils.ts b/src/datasource/utils.ts index e07ba7b..b5bac89 100644 --- a/src/datasource/utils.ts +++ b/src/datasource/utils.ts @@ -38,8 +38,8 @@ export function useDatasourceFields(datasource: BaseQuickwitDataSource, range: T const wordIsField = word.match(/([^:\s]+):"?([^"\s]*)"?/); if (wordIsField?.length) { - const [_match, fieldName, _fieldValue] = wordIsField; - const candidateValues = await datasource.getTagValues({ key: fieldName, timeRange: range }); + const [_match, fieldName, fieldValue] = wordIsField; + const candidateValues = await datasource.getTagValues({ key: fieldName, timeRange: range, fieldValue}); suggestions.from = fieldName.length + 1; // Replace only the value part suggestions.options = candidateValues.map(v => ({ type: 'text',