Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add language settings & search parameter #1693

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 };
84 changes: 84 additions & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,77 @@ export type HybridSearch = {
semanticRatio?: number;
};

export type Locale =
mdubus marked this conversation as resolved.
Show resolved Hide resolved
| 'epo'
| 'eng'
| 'rus'
| 'cmn'
| 'spa'
| 'por'
| 'ita'
| 'ben'
| 'fra'
| 'deu'
| 'ukr'
| 'kat'
| 'ara'
| 'hin'
| 'jpn'
| 'heb'
| 'yid'
| 'pol'
| 'amh'
| 'jav'
| 'kor'
| 'nob'
| 'dan'
| 'swe'
| 'fin'
| 'tur'
| 'nld'
| 'hun'
| 'ces'
| 'ell'
| 'bul'
| 'bel'
| 'mar'
| 'kan'
| 'ron'
| 'slv'
| 'hrv'
| 'srp'
| 'mkd'
| 'lit'
| 'lav'
| 'est'
| 'tam'
| 'vie'
| 'urd'
| 'tha'
| 'guj'
| 'uzb'
| 'pan'
| 'aze'
| 'ind'
| 'tel'
| 'pes'
| 'mal'
| 'ori'
| 'mya'
| 'nep'
| 'sin'
| 'khm'
| 'tuk'
| 'aka'
| 'zul'
| 'sna'
| 'afr'
| 'lat'
| 'slk'
| 'cat'
| 'tgl'
| 'hye';

export type SearchParams = Query &
Pagination &
Highlight &
Expand All @@ -130,6 +201,7 @@ export type SearchParams = Query &
hybrid?: HybridSearch;
distinct?: string;
retrieveVectors?: boolean;
locales?: Locale[];
mdubus marked this conversation as resolved.
Show resolved Hide resolved
};

// Search parameters for searches made with the GET method
Expand Down Expand Up @@ -428,6 +500,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 +525,7 @@ export type Settings = {
proximityPrecision?: ProximityPrecision;
embedders?: Embedders;
searchCutoffMs?: SearchCutoffMs;
localizedAttributes?: LocalizedAttributes;
};

/*
Expand Down Expand Up @@ -992,6 +1072,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
212 changes: 212 additions & 0 deletions tests/localized_attributes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
import { ErrorStatusCode, type LocalizedAttributes } from '../src/types';
import {
clearAllIndexes,
config,
BAD_HOST,
MeiliSearch,
getClient,
dataset,
} from './utils/meilisearch-test-utils';

const index = {
uid: 'movies_test',
};

const DEFAULT_LOCALIZED_ATTRIBUTES = null;

jest.setTimeout(100 * 1000);

afterAll(() => {
return clearAllIndexes(config);
});

describe.each([{ permission: 'Master' }, { permission: 'Admin' }])(
'Test on localizedAttributes',
({ permission }) => {
beforeEach(async () => {
await clearAllIndexes(config);
const client = await getClient('Master');
const { taskUid } = await client.index(index.uid).addDocuments(dataset);
await client.waitForTask(taskUid);
});

test(`${permission} key: Get default localizedAttributes settings`, async () => {
const client = await getClient(permission);
const response = await client.index(index.uid).getLocalizedAttributes();

expect(response).toEqual(DEFAULT_LOCALIZED_ATTRIBUTES);
});

test(`${permission} key: Update localizedAttributes to valid value`, async () => {
const client = await getClient(permission);
const newLocalizedAttributes: LocalizedAttributes = [
{ attributePatterns: ['title'], locales: ['eng'] },
];
const task = await client
.index(index.uid)
.updateLocalizedAttributes(newLocalizedAttributes);
await client.waitForTask(task.taskUid);

const response = await client.index(index.uid).getLocalizedAttributes();

expect(response).toEqual(newLocalizedAttributes);
});

test(`${permission} key: Update localizedAttributes to null`, async () => {
const client = await getClient(permission);
const newLocalizedAttributes = null;
const task = await client
.index(index.uid)
.updateLocalizedAttributes(newLocalizedAttributes);
await client.index(index.uid).waitForTask(task.taskUid);

const response = await client.index(index.uid).getLocalizedAttributes();

expect(response).toEqual(DEFAULT_LOCALIZED_ATTRIBUTES);
});

test(`${permission} key: Update localizedAttributes with invalid value`, async () => {
const client = await getClient(permission);
const newLocalizedAttributes = 'hello' as any; // bad localizedAttributes value

await expect(
client
.index(index.uid)
.updateLocalizedAttributes(newLocalizedAttributes),
).rejects.toHaveProperty(
'cause.code',
ErrorStatusCode.INVALID_SETTINGS_LOCALIZED_ATTRIBUTES,
);
});

test(`${permission} key: Reset localizedAttributes`, async () => {
const client = await getClient(permission);
const newLocalizedAttributes: LocalizedAttributes = [];
const updateTask = await client
.index(index.uid)
.updateLocalizedAttributes(newLocalizedAttributes);
await client.waitForTask(updateTask.taskUid);
const task = await client.index(index.uid).resetLocalizedAttributes();
await client.waitForTask(task.taskUid);

const response = await client.index(index.uid).getLocalizedAttributes();

expect(response).toEqual(DEFAULT_LOCALIZED_ATTRIBUTES);
});
},
);

describe.each([{ permission: 'Search' }])(
'Test on localizedAttributes',
({ permission }) => {
beforeEach(async () => {
const client = await getClient('Master');
const { taskUid } = await client.createIndex(index.uid);
await client.waitForTask(taskUid);
});

test(`${permission} key: try to get localizedAttributes and be denied`, async () => {
const client = await getClient(permission);
await expect(
client.index(index.uid).getLocalizedAttributes(),
).rejects.toHaveProperty('cause.code', ErrorStatusCode.INVALID_API_KEY);
});

test(`${permission} key: try to update localizedAttributes and be denied`, async () => {
const client = await getClient(permission);
await expect(
client.index(index.uid).updateLocalizedAttributes([]),
).rejects.toHaveProperty('cause.code', ErrorStatusCode.INVALID_API_KEY);
});

test(`${permission} key: try to reset localizedAttributes and be denied`, async () => {
const client = await getClient(permission);
await expect(
client.index(index.uid).resetLocalizedAttributes(),
).rejects.toHaveProperty('cause.code', ErrorStatusCode.INVALID_API_KEY);
});
},
);

describe.each([{ permission: 'No' }])(
'Test on localizedAttributes',
({ permission }) => {
beforeAll(async () => {
const client = await getClient('Master');
const { taskUid } = await client.createIndex(index.uid);
await client.waitForTask(taskUid);
});

test(`${permission} key: try to get localizedAttributes and be denied`, async () => {
const client = await getClient(permission);
await expect(
client.index(index.uid).getLocalizedAttributes(),
).rejects.toHaveProperty(
'cause.code',
ErrorStatusCode.MISSING_AUTHORIZATION_HEADER,
);
});

test(`${permission} key: try to update localizedAttributes and be denied`, async () => {
const client = await getClient(permission);
await expect(
client.index(index.uid).updateLocalizedAttributes([]),
).rejects.toHaveProperty(
'cause.code',
ErrorStatusCode.MISSING_AUTHORIZATION_HEADER,
);
});

test(`${permission} key: try to reset localizedAttributes and be denied`, async () => {
const client = await getClient(permission);
await expect(
client.index(index.uid).resetLocalizedAttributes(),
).rejects.toHaveProperty(
'cause.code',
ErrorStatusCode.MISSING_AUTHORIZATION_HEADER,
);
});
},
);

describe.each([
{ host: BAD_HOST, trailing: false },
{ host: `${BAD_HOST}/api`, trailing: false },
{ host: `${BAD_HOST}/trailing/`, trailing: true },
])('Tests on url construction', ({ host, trailing }) => {
test(`Test getLocalizedAttributes route`, async () => {
const route = `indexes/${index.uid}/settings/localized-attributes`;
const client = new MeiliSearch({ host });
const strippedHost = trailing ? host.slice(0, -1) : host;
await expect(
client.index(index.uid).getLocalizedAttributes(),
).rejects.toHaveProperty(
'message',
`Request to ${strippedHost}/${route} has failed`,
);
});

test(`Test updateLocalizedAttributes route`, async () => {
const route = `indexes/${index.uid}/settings/localized-attributes`;
const client = new MeiliSearch({ host });
const strippedHost = trailing ? host.slice(0, -1) : host;
await expect(
client.index(index.uid).updateLocalizedAttributes(null),
).rejects.toHaveProperty(
'message',
`Request to ${strippedHost}/${route} has failed`,
);
});

test(`Test resetLocalizedAttributes route`, async () => {
const route = `indexes/${index.uid}/settings/localized-attributes`;
const client = new MeiliSearch({ host });
const strippedHost = trailing ? host.slice(0, -1) : host;
await expect(
client.index(index.uid).resetLocalizedAttributes(),
).rejects.toHaveProperty(
'message',
`Request to ${strippedHost}/${route} has failed`,
);
});
});
Loading
Loading