Skip to content

Commit

Permalink
feat: hybrid search changes (#1679)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdubus authored Jun 25, 2024
1 parent 65bc592 commit 8e56525
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ export type SearchParams = Query &
attributesToSearchOn?: string[] | null;
hybrid?: HybridSearch;
distinct?: string;
retrieveVectors?: boolean;
};

// Search parameters for searches made with the GET method
Expand All @@ -150,6 +151,7 @@ export type SearchRequestGET = Pagination &
hybridSemanticRatio?: number;
rankingScoreThreshold?: number;
distinct?: string;
retrieveVectors?: boolean;
};

export type MultiSearchQuery = SearchParams & { indexUid: string };
Expand Down Expand Up @@ -311,6 +313,7 @@ export type DocumentsQuery<T = Record<string, any>> = ResourceQuery & {
filter?: Filter;
limit?: number;
offset?: number;
retrieveVectors?: boolean;
};

export type DocumentQuery<T = Record<string, any>> = {
Expand Down
86 changes: 86 additions & 0 deletions tests/documents.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,92 @@ Hint: It might not be working because maybe you're not up to date with the Meili
expect(documents.results.length).toEqual(dataset.length);
});

test(`${permission} key: Get documents with retrieveVectors to true`, async () => {
const client = await getClient(permission);
const adminKey = await getKey('Admin');

await fetch(`${HOST}/experimental-features`, {
body: JSON.stringify({ vectorStore: true }),
headers: {
Authorization: `Bearer ${adminKey}`,
'Content-Type': 'application/json',
},
method: 'PATCH',
});

const { taskUid } = await client
.index(indexPk.uid)
.addDocuments(dataset);
await client.index(indexPk.uid).waitForTask(taskUid);

// Get documents with POST
const documentsPost = await client
.index(indexPk.uid)
.getDocuments<Book>({ retrieveVectors: true });

expect(documentsPost.results.length).toEqual(dataset.length);
expect(documentsPost.results[0]).toHaveProperty('_vectors');

// Get documents with GET
const res = await fetch(
`${HOST}/indexes/${indexPk.uid}/documents?retrieveVectors=true`,
{
headers: {
Authorization: `Bearer ${adminKey}`,
'Content-Type': 'application/json',
},
method: 'GET',
},
);
const documentsGet = await res.json();

expect(documentsGet.results.length).toEqual(dataset.length);
expect(documentsGet.results[0]).toHaveProperty('_vectors');
});

test(`${permission} key: Get documents without retrieveVectors`, async () => {
const client = await getClient(permission);
const adminKey = await getKey('Admin');

await fetch(`${HOST}/experimental-features`, {
body: JSON.stringify({ vectorStore: true }),
headers: {
Authorization: `Bearer ${adminKey}`,
'Content-Type': 'application/json',
},
method: 'PATCH',
});

const { taskUid } = await client
.index(indexPk.uid)
.addDocuments(dataset);
await client.index(indexPk.uid).waitForTask(taskUid);

// Get documents with POST
const documentsPost = await client
.index(indexPk.uid)
.getDocuments<Book>();

expect(documentsPost.results.length).toEqual(dataset.length);
expect(documentsPost.results[0]).not.toHaveProperty('_vectors');

// Get documents with GET
const res = await fetch(
`${HOST}/indexes/${indexPk.uid}/documents?retrieveVectors=false`,
{
headers: {
Authorization: `Bearer ${adminKey}`,
'Content-Type': 'application/json',
},
method: 'GET',
},
);
const documentsGet = await res.json();

expect(documentsGet.results.length).toEqual(dataset.length);
expect(documentsGet.results[0]).not.toHaveProperty('_vectors');
});

test(`${permission} key: Replace documents from index that has NO primary key`, async () => {
const client = await getClient(permission);
const { taskUid: addDocTask } = await client
Expand Down
42 changes: 42 additions & 0 deletions tests/get_search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,48 @@ describe.each([
expect(response.hits.length).toEqual(4);
});

test(`${permission} key: search with retrieveVectors to true`, async () => {
const client = await getClient(permission);
const adminKey = await getKey('Admin');

await fetch(`${HOST}/experimental-features`, {
body: JSON.stringify({ vectorStore: true }),
headers: {
Authorization: `Bearer ${adminKey}`,
'Content-Type': 'application/json',
},
method: 'PATCH',
});

const response = await client.index(index.uid).searchGet('prince', {
retrieveVectors: true,
});

expect(response).toHaveProperty('hits', expect.any(Array));
expect(response).toHaveProperty('query', 'prince');
expect(response.hits[0]).toHaveProperty('_vectors');
});

test(`${permission} key: search without retrieveVectors`, async () => {
const client = await getClient(permission);
const adminKey = await getKey('Admin');

await fetch(`${HOST}/experimental-features`, {
body: JSON.stringify({ vectorStore: true }),
headers: {
Authorization: `Bearer ${adminKey}`,
'Content-Type': 'application/json',
},
method: 'PATCH',
});

const response = await client.index(index.uid).searchGet('prince');

expect(response).toHaveProperty('hits', expect.any(Array));
expect(response).toHaveProperty('query', 'prince');
expect(response.hits[0]).not.toHaveProperty('_vectors');
});

test(`${permission} key: Try to search on deleted index and fail`, async () => {
const client = await getClient(permission);
const masterClient = await getClient('Master');
Expand Down
42 changes: 42 additions & 0 deletions tests/search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,48 @@ describe.each([
expect(response.hits.length).toEqual(4);
});

test(`${permission} key: search with retrieveVectors to true`, async () => {
const client = await getClient(permission);
const adminKey = await getKey('Admin');

await fetch(`${HOST}/experimental-features`, {
body: JSON.stringify({ vectorStore: true }),
headers: {
Authorization: `Bearer ${adminKey}`,
'Content-Type': 'application/json',
},
method: 'PATCH',
});

const response = await client.index(index.uid).search('prince', {
retrieveVectors: true,
});

expect(response).toHaveProperty('hits', expect.any(Array));
expect(response).toHaveProperty('query', 'prince');
expect(response.hits[0]).toHaveProperty('_vectors');
});

test(`${permission} key: search without retrieveVectors`, async () => {
const client = await getClient(permission);
const adminKey = await getKey('Admin');

await fetch(`${HOST}/experimental-features`, {
body: JSON.stringify({ vectorStore: true }),
headers: {
Authorization: `Bearer ${adminKey}`,
'Content-Type': 'application/json',
},
method: 'PATCH',
});

const response = await client.index(index.uid).search('prince');

expect(response).toHaveProperty('hits', expect.any(Array));
expect(response).toHaveProperty('query', 'prince');
expect(response.hits[0]).not.toHaveProperty('_vectors');
});

test(`${permission} key: Try to search on deleted index and fail`, async () => {
const client = await getClient(permission);
const masterClient = await getClient('Master');
Expand Down

0 comments on commit 8e56525

Please sign in to comment.