Skip to content

Commit

Permalink
Merge pull request #1693 from flevi29/feature-lang-settings-search-pa…
Browse files Browse the repository at this point in the history
…rams

Add language settings & search parameter
  • Loading branch information
mdubus authored Aug 21, 2024
2 parents a0a27cc + 74b48ab commit a2d46d5
Show file tree
Hide file tree
Showing 6 changed files with 313 additions and 2 deletions.
42 changes: 42 additions & 0 deletions src/indexes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import {
Embedders,
SearchCutoffMs,
SearchSimilarDocumentsParams,
LocalizedAttributes,
} from './types';
import { removeUndefinedFromObject } from './utils';
import { HttpRequests } from './http-requests';
Expand Down Expand Up @@ -1393,6 +1394,47 @@ class Index<T extends Record<string, any> = Record<string, any>> {

return new EnqueuedTask(task);
}

///
/// LOCALIZED ATTRIBUTES SETTINGS
///

/**
* Get the localized attributes settings.
*
* @returns Promise containing object of localized attributes settings
*/
async getLocalizedAttributes(): Promise<LocalizedAttributes> {
const url = `indexes/${this.uid}/settings/localized-attributes`;
return await this.httpRequest.get<LocalizedAttributes>(url);
}

/**
* Update the localized attributes settings.
*
* @param localizedAttributes - Localized attributes object
* @returns Promise containing an EnqueuedTask
*/
async updateLocalizedAttributes(
localizedAttributes: LocalizedAttributes,
): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/settings/localized-attributes`;
const task = await this.httpRequest.put(url, localizedAttributes);

return new EnqueuedTask(task);
}

/**
* Reset the localized attributes settings.
*
* @returns Promise containing an EnqueuedTask
*/
async resetLocalizedAttributes(): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/settings/localized-attributes`;
const task = await this.httpRequest.delete(url);

return new EnqueuedTask(task);
}
}

export { Index };
21 changes: 19 additions & 2 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ export type HybridSearch = {
semanticRatio?: number;
};

// https://www.meilisearch.com/docs/reference/api/settings#localized-attributes
export type Locale = string;

export type SearchParams = Query &
Pagination &
Highlight &
Expand All @@ -130,6 +133,7 @@ export type SearchParams = Query &
hybrid?: HybridSearch;
distinct?: string;
retrieveVectors?: boolean;
locales?: Locale[];
};

// Search parameters for searches made with the GET method
Expand All @@ -152,6 +156,7 @@ export type SearchRequestGET = Pagination &
rankingScoreThreshold?: number;
distinct?: string;
retrieveVectors?: boolean;
locales?: Locale[];
};

export type MultiSearchQuery = SearchParams & { indexUid: string };
Expand Down Expand Up @@ -428,6 +433,13 @@ export type PaginationSettings = {

export type SearchCutoffMs = number | null;

export type LocalizedAttribute = {
attributePatterns: string[];
locales: Locale[];
};

export type LocalizedAttributes = LocalizedAttribute[] | null;

export type Settings = {
filterableAttributes?: FilterableAttributes;
distinctAttribute?: DistinctAttribute;
Expand All @@ -446,6 +458,7 @@ export type Settings = {
proximityPrecision?: ProximityPrecision;
embedders?: Embedders;
searchCutoffMs?: SearchCutoffMs;
localizedAttributes?: LocalizedAttributes;
};

/*
Expand Down Expand Up @@ -677,9 +690,9 @@ export interface FetchError extends Error {

export type MeiliSearchErrorResponse = {
message: string;
// @TODO: Could be typed, but will it be kept updated? https://www.meilisearch.com/docs/reference/errors/error_codes
// https://www.meilisearch.com/docs/reference/errors/error_codes
code: string;
// @TODO: Could be typed https://www.meilisearch.com/docs/reference/errors/overview#errors
// https://www.meilisearch.com/docs/reference/errors/overview#errors
type: string;
link: string;
};
Expand Down Expand Up @@ -992,6 +1005,10 @@ export const ErrorStatusCode = {
/** @see https://www.meilisearch.com/docs/reference/errors/error_codes#invalid_settings_search_cutoff_ms */
INVALID_SETTINGS_SEARCH_CUTOFF_MS: 'invalid_settings_search_cutoff_ms',

/** @see https://www.meilisearch.com/docs/reference/errors/error_codes#invalid_settings_search_cutoff_ms */
INVALID_SETTINGS_LOCALIZED_ATTRIBUTES:
'invalid_settings_localized_attributes',

/** @see https://www.meilisearch.com/docs/reference/errors/error_codes#invalid_task_before_enqueued_at */
INVALID_TASK_BEFORE_ENQUEUED_AT: 'invalid_task_before_enqueued_at',

Expand Down
22 changes: 22 additions & 0 deletions tests/__snapshots__/settings.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ exports[`Test on settings Admin key: Get default settings of an index 1`] = `
},
},
"filterableAttributes": [],
"localizedAttributes": null,
"nonSeparatorTokens": [],
"pagination": {
"maxTotalHits": 1000,
Expand Down Expand Up @@ -61,6 +62,7 @@ exports[`Test on settings Admin key: Get default settings of empty index with pr
},
},
"filterableAttributes": [],
"localizedAttributes": null,
"nonSeparatorTokens": [],
"pagination": {
"maxTotalHits": 1000,
Expand Down Expand Up @@ -108,6 +110,7 @@ exports[`Test on settings Admin key: Reset embedders settings 1`] = `
},
},
"filterableAttributes": [],
"localizedAttributes": null,
"nonSeparatorTokens": [],
"pagination": {
"maxTotalHits": 1000,
Expand Down Expand Up @@ -155,6 +158,7 @@ exports[`Test on settings Admin key: Reset settings 1`] = `
},
},
"filterableAttributes": [],
"localizedAttributes": null,
"nonSeparatorTokens": [],
"pagination": {
"maxTotalHits": 1000,
Expand Down Expand Up @@ -202,6 +206,7 @@ exports[`Test on settings Admin key: Reset settings of empty index 1`] = `
},
},
"filterableAttributes": [],
"localizedAttributes": null,
"nonSeparatorTokens": [],
"pagination": {
"maxTotalHits": 1000,
Expand Down Expand Up @@ -257,6 +262,7 @@ exports[`Test on settings Admin key: Update embedders settings 1`] = `
},
},
"filterableAttributes": [],
"localizedAttributes": null,
"nonSeparatorTokens": [],
"pagination": {
"maxTotalHits": 1000,
Expand Down Expand Up @@ -304,6 +310,7 @@ exports[`Test on settings Admin key: Update searchableAttributes settings on emp
},
},
"filterableAttributes": [],
"localizedAttributes": null,
"nonSeparatorTokens": [],
"pagination": {
"maxTotalHits": 1000,
Expand Down Expand Up @@ -351,6 +358,7 @@ exports[`Test on settings Admin key: Update searchableAttributes settings on emp
},
},
"filterableAttributes": [],
"localizedAttributes": null,
"nonSeparatorTokens": [],
"pagination": {
"maxTotalHits": 1000,
Expand Down Expand Up @@ -403,6 +411,7 @@ exports[`Test on settings Admin key: Update settings 1`] = `
"filterableAttributes": [
"title",
],
"localizedAttributes": null,
"nonSeparatorTokens": [
"&sep",
"/",
Expand Down Expand Up @@ -466,6 +475,7 @@ exports[`Test on settings Admin key: Update settings on empty index with primary
},
},
"filterableAttributes": [],
"localizedAttributes": null,
"nonSeparatorTokens": [],
"pagination": {
"maxTotalHits": 1000,
Expand Down Expand Up @@ -511,6 +521,7 @@ exports[`Test on settings Admin key: Update settings with all null values 1`] =
},
},
"filterableAttributes": [],
"localizedAttributes": null,
"nonSeparatorTokens": [],
"pagination": {
"maxTotalHits": 1000,
Expand Down Expand Up @@ -558,6 +569,7 @@ exports[`Test on settings Master key: Get default settings of an index 1`] = `
},
},
"filterableAttributes": [],
"localizedAttributes": null,
"nonSeparatorTokens": [],
"pagination": {
"maxTotalHits": 1000,
Expand Down Expand Up @@ -605,6 +617,7 @@ exports[`Test on settings Master key: Get default settings of empty index with p
},
},
"filterableAttributes": [],
"localizedAttributes": null,
"nonSeparatorTokens": [],
"pagination": {
"maxTotalHits": 1000,
Expand Down Expand Up @@ -652,6 +665,7 @@ exports[`Test on settings Master key: Reset embedders settings 1`] = `
},
},
"filterableAttributes": [],
"localizedAttributes": null,
"nonSeparatorTokens": [],
"pagination": {
"maxTotalHits": 1000,
Expand Down Expand Up @@ -699,6 +713,7 @@ exports[`Test on settings Master key: Reset settings 1`] = `
},
},
"filterableAttributes": [],
"localizedAttributes": null,
"nonSeparatorTokens": [],
"pagination": {
"maxTotalHits": 1000,
Expand Down Expand Up @@ -746,6 +761,7 @@ exports[`Test on settings Master key: Reset settings of empty index 1`] = `
},
},
"filterableAttributes": [],
"localizedAttributes": null,
"nonSeparatorTokens": [],
"pagination": {
"maxTotalHits": 1000,
Expand Down Expand Up @@ -801,6 +817,7 @@ exports[`Test on settings Master key: Update embedders settings 1`] = `
},
},
"filterableAttributes": [],
"localizedAttributes": null,
"nonSeparatorTokens": [],
"pagination": {
"maxTotalHits": 1000,
Expand Down Expand Up @@ -848,6 +865,7 @@ exports[`Test on settings Master key: Update searchableAttributes settings on em
},
},
"filterableAttributes": [],
"localizedAttributes": null,
"nonSeparatorTokens": [],
"pagination": {
"maxTotalHits": 1000,
Expand Down Expand Up @@ -895,6 +913,7 @@ exports[`Test on settings Master key: Update searchableAttributes settings on em
},
},
"filterableAttributes": [],
"localizedAttributes": null,
"nonSeparatorTokens": [],
"pagination": {
"maxTotalHits": 1000,
Expand Down Expand Up @@ -947,6 +966,7 @@ exports[`Test on settings Master key: Update settings 1`] = `
"filterableAttributes": [
"title",
],
"localizedAttributes": null,
"nonSeparatorTokens": [
"&sep",
"/",
Expand Down Expand Up @@ -1010,6 +1030,7 @@ exports[`Test on settings Master key: Update settings on empty index with primar
},
},
"filterableAttributes": [],
"localizedAttributes": null,
"nonSeparatorTokens": [],
"pagination": {
"maxTotalHits": 1000,
Expand Down Expand Up @@ -1055,6 +1076,7 @@ exports[`Test on settings Master key: Update settings with all null values 1`] =
},
},
"filterableAttributes": [],
"localizedAttributes": null,
"nonSeparatorTokens": [],
"pagination": {
"maxTotalHits": 1000,
Expand Down
Loading

0 comments on commit a2d46d5

Please sign in to comment.