Skip to content

Commit 5ac68c1

Browse files
authored
Add indices to matches position (#1773)
* make 'genre' an array of genres * Fix tests after making genre an array * Fix search (post) tests after making genre an array * Fix typed search tests after updating genre to an array * Add indices in match positions * prettier * Move test before test deleting the index
1 parent 7f4fbd6 commit 5ac68c1

File tree

6 files changed

+197
-77
lines changed

6 files changed

+197
-77
lines changed

src/types/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ export type CategoriesDistribution = {
185185
export type Facet = string;
186186
export type FacetDistribution = Record<Facet, CategoriesDistribution>;
187187
export type MatchesPosition<T> = Partial<
188-
Record<keyof T, Array<{ start: number; length: number }>>
188+
Record<keyof T, Array<{ start: number; length: number; indices?: number[] }>>
189189
>;
190190

191191
export type RankingScoreDetails = {

tests/__snapshots__/get_search.test.ts.snap

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,21 @@ exports[`Test on GET search > Admin key: search on attributesToSearchOn set to n
55
"estimatedTotalHits": 2,
66
"hits": [
77
{
8+
"author": "Antoine de Saint-Exupéry",
89
"comment": "A french book about a prince that walks on little cute planets",
9-
"genre": "adventure",
10+
"genre": [
11+
"adventure",
12+
],
1013
"id": 456,
1114
"title": "Le Petit Prince",
1215
},
1316
{
17+
"author": "J.K. Rowling",
1418
"comment": "The best book",
15-
"genre": "fantasy",
19+
"genre": [
20+
"fantasy",
21+
"adventure",
22+
],
1623
"id": 4,
1724
"title": "Harry Potter and the Half-Blood Prince",
1825
},
@@ -29,14 +36,21 @@ exports[`Test on GET search > Master key: search on attributesToSearchOn set to
2936
"estimatedTotalHits": 2,
3037
"hits": [
3138
{
39+
"author": "Antoine de Saint-Exupéry",
3240
"comment": "A french book about a prince that walks on little cute planets",
33-
"genre": "adventure",
41+
"genre": [
42+
"adventure",
43+
],
3444
"id": 456,
3545
"title": "Le Petit Prince",
3646
},
3747
{
48+
"author": "J.K. Rowling",
3849
"comment": "The best book",
39-
"genre": "fantasy",
50+
"genre": [
51+
"fantasy",
52+
"adventure",
53+
],
4054
"id": 4,
4155
"title": "Harry Potter and the Half-Blood Prince",
4256
},
@@ -53,14 +67,21 @@ exports[`Test on GET search > Search key: search on attributesToSearchOn set to
5367
"estimatedTotalHits": 2,
5468
"hits": [
5569
{
70+
"author": "Antoine de Saint-Exupéry",
5671
"comment": "A french book about a prince that walks on little cute planets",
57-
"genre": "adventure",
72+
"genre": [
73+
"adventure",
74+
],
5875
"id": 456,
5976
"title": "Le Petit Prince",
6077
},
6178
{
79+
"author": "J.K. Rowling",
6280
"comment": "The best book",
63-
"genre": "fantasy",
81+
"genre": [
82+
"fantasy",
83+
"adventure",
84+
],
6485
"id": 4,
6586
"title": "Harry Potter and the Half-Blood Prince",
6687
},

tests/__snapshots__/search.test.ts.snap

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,23 @@ exports[`Test on POST search > Admin key: search on attributesToSearchOn set to
55
"estimatedTotalHits": 2,
66
"hits": [
77
{
8+
"author": "Antoine de Saint-Exupéry",
89
"comment": "A french book about a prince that walks on little cute planets",
9-
"genre": "adventure",
10+
"genre": [
11+
"adventure",
12+
],
1013
"id": 456,
1114
"isNull": null,
1215
"isTrue": true,
1316
"title": "Le Petit Prince",
1417
},
1518
{
19+
"author": "J.K. Rowling",
1620
"comment": "The best book",
17-
"genre": "fantasy",
21+
"genre": [
22+
"fantasy",
23+
"adventure",
24+
],
1825
"id": 4,
1926
"title": "Harry Potter and the Half-Blood Prince",
2027
},
@@ -31,16 +38,23 @@ exports[`Test on POST search > Master key: search on attributesToSearchOn set to
3138
"estimatedTotalHits": 2,
3239
"hits": [
3340
{
41+
"author": "Antoine de Saint-Exupéry",
3442
"comment": "A french book about a prince that walks on little cute planets",
35-
"genre": "adventure",
43+
"genre": [
44+
"adventure",
45+
],
3646
"id": 456,
3747
"isNull": null,
3848
"isTrue": true,
3949
"title": "Le Petit Prince",
4050
},
4151
{
52+
"author": "J.K. Rowling",
4253
"comment": "The best book",
43-
"genre": "fantasy",
54+
"genre": [
55+
"fantasy",
56+
"adventure",
57+
],
4458
"id": 4,
4559
"title": "Harry Potter and the Half-Blood Prince",
4660
},
@@ -57,16 +71,23 @@ exports[`Test on POST search > Search key: search on attributesToSearchOn set to
5771
"estimatedTotalHits": 2,
5872
"hits": [
5973
{
74+
"author": "Antoine de Saint-Exupéry",
6075
"comment": "A french book about a prince that walks on little cute planets",
61-
"genre": "adventure",
76+
"genre": [
77+
"adventure",
78+
],
6279
"id": 456,
6380
"isNull": null,
6481
"isTrue": true,
6582
"title": "Le Petit Prince",
6683
},
6784
{
85+
"author": "J.K. Rowling",
6886
"comment": "The best book",
69-
"genre": "fantasy",
87+
"genre": [
88+
"fantasy",
89+
"adventure",
90+
],
7091
"id": 4,
7192
"title": "Harry Potter and the Half-Blood Prince",
7293
},

tests/get_search.test.ts

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,40 +22,57 @@ const dataset = [
2222
{
2323
id: 123,
2424
title: "Pride and Prejudice",
25+
author: "Jane Austen",
2526
comment: "A great book",
26-
genre: "romance",
27+
genre: ["romance"],
2728
},
2829
{
2930
id: 456,
3031
title: "Le Petit Prince",
32+
author: "Antoine de Saint-Exupéry",
3133
comment: "A french book about a prince that walks on little cute planets",
32-
genre: "adventure",
34+
genre: ["adventure"],
3335
},
3436
{
3537
id: 2,
3638
title: "Le Rouge et le Noir",
39+
author: "Stendhal",
3740
comment: "Another french book",
38-
genre: "romance",
41+
genre: ["romance"],
3942
},
4043
{
4144
id: 1,
4245
title: "Alice In Wonderland",
46+
author: "Lewis Carroll",
4347
comment: "A weird book",
44-
genre: "adventure",
48+
genre: ["adventure"],
4549
},
4650
{
4751
id: 1344,
4852
title: "The Hobbit",
53+
author: "J.R.R. Tolkien",
4954
comment: "An awesome book",
50-
genre: "sci fi",
55+
genre: ["fantasy", "adventure"],
5156
},
5257
{
5358
id: 4,
5459
title: "Harry Potter and the Half-Blood Prince",
60+
author: "J.K. Rowling",
5561
comment: "The best book",
56-
genre: "fantasy",
62+
genre: ["fantasy", "adventure"],
63+
},
64+
{
65+
id: 5,
66+
title: "Harry Potter and the Deathly Hallows",
67+
author: "J.K. Rowling",
68+
genre: ["fantasy", "adventure"],
69+
},
70+
{
71+
id: 42,
72+
title: "The Hitchhiker's Guide to the Galaxy",
73+
author: "Douglas Adams",
74+
genre: ["sci fi", "comedy"],
5775
},
58-
{ id: 42, title: "The Hitchhiker's Guide to the Galaxy", genre: "fantasy" },
5976
];
6077

6178
afterAll(() => {
@@ -75,7 +92,7 @@ describe.each([
7592
const { taskUid: task2 } = await client.createIndex(emptyIndex.uid);
7693
await client.waitForTask(task2);
7794

78-
const newFilterableAttributes = ["genre", "title", "id"];
95+
const newFilterableAttributes = ["genre", "title", "id", "author"];
7996
const { taskUid: task3 }: EnqueuedTask = await client
8097
.index(index.uid)
8198
.updateSettings({
@@ -188,8 +205,9 @@ describe.each([
188205
{
189206
id: 4,
190207
title: "Harry Potter and the Half-Blood Prince",
208+
author: "J.K. Rowling",
191209
comment: "The best book",
192-
genre: "fantasy",
210+
genre: ["fantasy", "adventure"],
193211
},
194212
]);
195213
expect(response).toHaveProperty("offset", 1);
@@ -415,9 +433,9 @@ describe.each([
415433
facets: ["genre"],
416434
});
417435
expect(response).toHaveProperty("facetDistribution", {
418-
genre: { fantasy: 2 },
436+
genre: { adventure: 3, fantasy: 3 },
419437
});
420-
expect(response.hits.length).toEqual(2);
438+
expect(response.hits.length).toEqual(3);
421439
});
422440

423441
test(`${permission} key: search with multiple filter and null query (placeholder)`, async () => {
@@ -427,10 +445,13 @@ describe.each([
427445
facets: ["genre"],
428446
});
429447
expect(response).toHaveProperty("facetDistribution", {
430-
genre: { fantasy: 2 },
448+
genre: {
449+
adventure: 3,
450+
fantasy: 3,
451+
},
431452
});
432-
expect(response.hits.length).toEqual(2);
433-
expect(response.estimatedTotalHits).toEqual(2);
453+
expect(response.hits.length).toEqual(3);
454+
expect(response.estimatedTotalHits).toEqual(3);
434455
});
435456

436457
test(`${permission} key: search with multiple filter and empty string query (placeholder)`, async () => {
@@ -441,9 +462,9 @@ describe.each([
441462
});
442463

443464
expect(response).toHaveProperty("facetDistribution", {
444-
genre: { fantasy: 2 },
465+
genre: { adventure: 3, fantasy: 3 },
445466
});
446-
expect(response.hits.length).toEqual(2);
467+
expect(response.hits.length).toEqual(3);
447468
});
448469

449470
test(`${permission} key: Try to search with wrong format filter`, async () => {
@@ -492,9 +513,9 @@ describe.each([
492513
const client = await getClient(permission);
493514
const response = await client
494515
.index(index.uid)
495-
.search("", { distinct: "genre" });
516+
.search("", { distinct: "author" });
496517

497-
expect(response.hits.length).toEqual(4);
518+
expect(response.hits.length).toEqual(7);
498519
});
499520

500521
test(`${permission} key: search with retrieveVectors to true`, async () => {
@@ -539,6 +560,17 @@ describe.each([
539560
expect(response.hits[0]).not.toHaveProperty("_vectors");
540561
});
541562

563+
test(`${permission} key: matches position contain indices`, async () => {
564+
const client = await getClient(permission);
565+
const response = await client.index(index.uid).searchGet("fantasy", {
566+
showMatchesPosition: true,
567+
});
568+
expect(response.hits[0]._matchesPosition).toEqual({
569+
genre: [{ start: 0, length: 7, indices: [0] }],
570+
});
571+
});
572+
573+
// This test deletes the index, so following tests may fail if they need an existing index
542574
test(`${permission} key: Try to search on deleted index and fail`, async () => {
543575
const client = await getClient(permission);
544576
const masterClient = await getClient("Master");

0 commit comments

Comments
 (0)