Skip to content

Commit 2cb273f

Browse files
committed
feat(autocomplete): use query to prefix search autocompletion results
1 parent a746b97 commit 2cb273f

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

src/components/LuceneQueryEditor.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export function LuceneQueryEditor(props: LuceneQueryEditorProps){
4040
if (!word){ return null }
4141
suggestions = await autocompleter(word?.text);
4242
if (suggestions && suggestions.options.length > 0 ) {
43-
// Fixes autocompletion inserting an extra quote when the cursor is before a quote
43+
// Fixes autocompletion inserting an extra quote when the cursor is immediately before a quote
4444
const cursorIsBeforeQuote = context.state.doc.toString().slice(context.pos, context.pos + 1) === '"';
4545
if (cursorIsBeforeQuote) {
4646
suggestions.options = suggestions.options.map(o => ({...o, apply: `${o.label.replace(/"$/g, '')}`}));

src/datasource/base.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ import {
1717
TimeRange,
1818
} from '@grafana/data';
1919
import { BucketAggregation, DataLinkConfig, ElasticsearchQuery, TermsQuery, FieldCapabilitiesResponse } from '@/types';
20-
import {
21-
DataSourceWithBackend,
22-
getTemplateSrv,
20+
import {
21+
DataSourceWithBackend,
22+
getTemplateSrv,
2323
TemplateSrv } from '@grafana/runtime';
2424
import { QuickwitOptions } from 'quickwit';
2525
import { getDataQuery } from 'QueryBuilder/elastic';
@@ -36,7 +36,7 @@ import { getQueryResponseProcessor } from 'datasource/processResponse';
3636
import { SECOND } from 'utils/time';
3737
import { GConstructor } from 'utils/mixins';
3838
import { LuceneQuery } from '@/utils/lucene';
39-
import { uidMaker } from "@/utils/uid"
39+
import { uidMaker } from "@/utils/uid"
4040
import { DefaultsConfigOverrides } from 'store/defaults/conf';
4141

4242
export type BaseQuickwitDataSourceConstructor = GConstructor<BaseQuickwitDataSource>
@@ -199,7 +199,7 @@ export class BaseQuickwitDataSource
199199
.map(field_capability => {
200200
return {
201201
text: field_capability.field_name,
202-
value: fieldTypeMap[field_capability.type],
202+
value: fieldTypeMap[field_capability.type],
203203
}
204204
});
205205
const uniquefieldCapabilities = fieldCapabilities.filter((field_capability, index, self) =>
@@ -223,9 +223,10 @@ export class BaseQuickwitDataSource
223223
/**
224224
* Get tag values for adhoc filters
225225
*/
226-
getTagValues(options: any) {
227-
const terms = this.getTerms({ field: options.key }, options.timeRange)
228-
return lastValueFrom(terms, {defaultValue:[]});
226+
getTagValues(options: { key: string, fieldValue: string, timeRange: TimeRange }) {
227+
const query = `${options.key}:${options.fieldValue}*`
228+
const terms = this.getTerms({ field: options.key, query }, options.timeRange)
229+
return lastValueFrom(terms, { defaultValue: [] });
229230
}
230231

231232
/**

src/datasource/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ export function useDatasourceFields(datasource: BaseQuickwitDataSource, range: T
3838

3939
const wordIsField = word.match(/([^:\s]+):"?([^"\s]*)"?/);
4040
if (wordIsField?.length) {
41-
const [_match, fieldName, _fieldValue] = wordIsField;
42-
const candidateValues = await datasource.getTagValues({ key: fieldName, timeRange: range });
41+
const [_match, fieldName, fieldValue] = wordIsField;
42+
const candidateValues = await datasource.getTagValues({ key: fieldName, timeRange: range, fieldValue});
4343
suggestions.from = fieldName.length + 1; // Replace only the value part
4444
suggestions.options = candidateValues.map(v => ({
4545
type: 'text',

0 commit comments

Comments
 (0)