Skip to content

Commit

Permalink
Reduce number of terms for autocomplete and use single quote when bui…
Browse files Browse the repository at this point in the history
…lding query.
  • Loading branch information
fmassot committed Mar 10, 2024
1 parent ecca730 commit 5f71648
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/QueryBuilder/elastic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type OrderByType = '_key' | '_term' | '_count'
function getTermsAgg(
fieldName: string,
size: number,
segment_size: number,
orderBy: OrderByType = "_key",
order: 'asc'|'desc' = 'asc'
): BucketAggregation {
Expand All @@ -20,6 +21,7 @@ function getTermsAgg(
field: fieldName,
settings:{
size: size.toString(),
segment_size: segment_size.toString(),
order: order,
orderBy: orderBy,
}
Expand All @@ -31,7 +33,8 @@ export function getDataQuery(queryDef: TermsQuery, refId: string): Elasticsearch
{id:"count1", type:'count'}
];

// Default behaviour is to order results by { _key: asc }
// Default behaviour is to order results by { _key: asc } and get 100 terms,
// segment_size is set to 100 to limit terms fetched by segment (by default it's 100 * 10).
// queryDef.order allows selection of asc/desc
// queryDef.orderBy allows selection of doc_count ordering (defaults desc)

Expand All @@ -55,7 +58,7 @@ export function getDataQuery(queryDef: TermsQuery, refId: string): Elasticsearch

const bucketAggs: BucketAggregation[] = [];
if (queryDef.field) {
bucketAggs.push(getTermsAgg(queryDef.field, 500, orderBy, order))
bucketAggs.push(getTermsAgg(queryDef.field, 100, 100, orderBy, order))
}

return {
Expand Down
1 change: 1 addition & 0 deletions src/dataquery.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export interface Terms extends BucketAggregationWithField {
settings?: {
order?: TermsOrder;
size?: string;
segment_size?: string;
min_doc_count?: string;
orderBy?: string;
missing?: string;
Expand Down
4 changes: 2 additions & 2 deletions src/datasource/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ export function useDatasourceFields(datasource: BaseQuickwitDataSource) {
const getSuggestions = useCallback(async (word: string): Promise<Suggestion> => {
let suggestions: Suggestion = { from: 0, options: [] };

const wordIsField = word.match(/([^:\s]+):"?([^"\s]*)"?/);
const wordIsField = word.match(/([^:\s]+):'?([^'\s]*)'?/);
if (wordIsField?.length) {
const [_match, fieldName, _fieldValue] = wordIsField;
const candidateValues = await datasource.getTagValues({ key: fieldName });
suggestions.from = fieldName.length + 1; // Replace only the value part
suggestions.options = candidateValues.map(v => ({
type: 'text',
label: typeof v.text === 'number' ? `${v.text}` : `"${v.text}"`
label: typeof v.text === 'number' ? `${v.text}` : `'${v.text}'`
}));
} else {
const candidateFields = fields;
Expand Down
2 changes: 1 addition & 1 deletion src/utils/lucene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ export class LuceneQuery {

key = escapeFilter(key);
value = escapeFilterValue(value);
const filter = `${modifier}${key}:"${value}"`;
const filter = `${modifier}${key}:'${value}'`;

return LuceneQuery.parse(concatenate(this.toString(), filter));
}
Expand Down

0 comments on commit 5f71648

Please sign in to comment.