Skip to content

Commit

Permalink
Add get/update/reset methods for prefixSearch settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Strift committed Dec 4, 2024
1 parent aabb334 commit 3c2a2b8
Show file tree
Hide file tree
Showing 3 changed files with 227 additions and 1 deletion.
45 changes: 44 additions & 1 deletion src/indexes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import {
SearchSimilarDocumentsParams,
LocalizedAttributes,
UpdateDocumentsByFunctionOptions,
PrefixSearch,
} from "./types";
import { removeUndefinedFromObject } from "./utils";
import { HttpRequests } from "./http-requests";
Expand Down Expand Up @@ -1458,6 +1459,10 @@ class Index<T extends Record<string, any> = Record<string, any>> {
return new EnqueuedTask(task);
}

///
/// FACET SEARCH SETTINGS
///

/**
* Get the facet search settings.
*
Expand All @@ -1476,7 +1481,8 @@ class Index<T extends Record<string, any> = Record<string, any>> {
*/
async updateFacetSearch(facetSearch: boolean): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/settings/facet-search`;
return await this.httpRequest.put(url, facetSearch);
const task = await this.httpRequest.put(url, facetSearch);
return new EnqueuedTask(task);
}

/**
Expand All @@ -1487,9 +1493,46 @@ class Index<T extends Record<string, any> = Record<string, any>> {
async resetFacetSearch(): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/settings/facet-search`;
const task = await this.httpRequest.delete(url);
return new EnqueuedTask(task);
}

///
/// PREFIX SEARCH SETTINGS
///

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

/**
* Update the prefix search settings.
*
* @param prefixSearch - PrefixSearch value
* @returns Promise containing an EnqueuedTask
*/
async updatePrefixSearch(prefixSearch: PrefixSearch): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/settings/prefix-search`;
const task = await this.httpRequest.put(url, prefixSearch);
return new EnqueuedTask(task);
}

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

}

export { Index };
9 changes: 9 additions & 0 deletions tests/__snapshots__/prefix_search_settings.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`Test on prefix search settings > Admin key: Get prefixSearch settings on empty index 1`] = `"indexingTime"`;

exports[`Test on prefix search settings > Admin key: Reset prefixSearch settings on an empty index 1`] = `"indexingTime"`;

exports[`Test on prefix search settings > Master key: Get prefixSearch settings on empty index 1`] = `"indexingTime"`;

exports[`Test on prefix search settings > Master key: Reset prefixSearch settings on an empty index 1`] = `"indexingTime"`;
174 changes: 174 additions & 0 deletions tests/prefix_search_settings.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
import { afterAll, expect, test, describe, beforeEach } from "vitest";
import { ErrorStatusCode } from "../src/types";
import {
clearAllIndexes,
config,
BAD_HOST,
MeiliSearch,
getClient,
dataset,
} from "./utils/meilisearch-test-utils";

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

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

describe.each([{ permission: "Master" }, { permission: "Admin" }])(
"Test on prefix search settings",
({ 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 prefixSearch settings on empty index`, async () => {
const client = await getClient(permission);

const response = await client.index(index.uid).getPrefixSearch();
expect(response).toMatchSnapshot();
});

test(`${permission} key: Set prefixSearch settings with dedicated endpoint on empty index`, async () => {
const client = await getClient(permission);

const { taskUid } = await client.index(index.uid).updatePrefixSearch('disabled');
await client.index(index.uid).waitForTask(taskUid);

const updatedSettings = await client.index(index.uid).getPrefixSearch();
expect(updatedSettings).toBe('disabled');
});

test(`${permission} key: Reset prefixSearch settings on an empty index`, async () => {
const client = await getClient(permission);

const { taskUid } = await client.index(index.uid).resetPrefixSearch();
await client.index(index.uid).waitForTask(taskUid);

const response = await client.index(index.uid).getPrefixSearch();
expect(response).toMatchSnapshot();
});
},
);

describe.each([{ permission: "Search" }])(
"Test on prefix search settings",
({ permission }) => {
beforeEach(async () => {
await clearAllIndexes(config);
const client = await getClient("Master");
const { taskUid } = await client.createIndex(index.uid);
await client.waitForTask(taskUid);
});

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

test(`${permission} key: try to update prefix search settings and be denied`, async () => {
const client = await getClient(permission);
await expect(
client.index(index.uid).updatePrefixSearch('disabled'),
).rejects.toHaveProperty("cause.code", ErrorStatusCode.INVALID_API_KEY);
});

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

describe.each([{ permission: "No" }])(
"Test on prefix search settings",
({ permission }) => {
beforeEach(async () => {
await clearAllIndexes(config);
const client = await getClient("Master");
const { taskUid } = await client.createIndex(index.uid);
await client.waitForTask(taskUid);
});

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

test(`${permission} key: try to update prefix search settings and be denied`, async () => {
const client = await getClient(permission);
await expect(
client.index(index.uid).updatePrefixSearch('disabled'),
).rejects.toHaveProperty(
"cause.code",
ErrorStatusCode.MISSING_AUTHORIZATION_HEADER,
);
});

test(`${permission} key: try to reset prefix search settings and be denied`, async () => {
const client = await getClient(permission);
await expect(
client.index(index.uid).resetPrefixSearch(),
).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(`getPrefixSearch route`, async () => {
const route = `indexes/${index.uid}/settings/prefix-search`;
const client = new MeiliSearch({ host });
const strippedHost = trailing ? host.slice(0, -1) : host;
await expect(
client.index(index.uid).getPrefixSearch(),
).rejects.toHaveProperty(
"message",
`Request to ${strippedHost}/${route} has failed`,
);
});

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

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

0 comments on commit 3c2a2b8

Please sign in to comment.