Skip to content

Commit

Permalink
feat(autocomplete): use query to prefix search autocompletion results
Browse files Browse the repository at this point in the history
  • Loading branch information
uptickmetachu committed Aug 18, 2024
1 parent a746b97 commit 2cb273f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/components/LuceneQueryEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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, '')}`}));
Expand Down
17 changes: 9 additions & 8 deletions src/datasource/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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<BaseQuickwitDataSource>
Expand Down Expand Up @@ -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) =>
Expand All @@ -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: [] });
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/datasource/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 2cb273f

Please sign in to comment.