|
1
|
import { SearchTypes } from "@medusajs/framework/types" |
|
2
|
import { normalizeSearchQuery, validateFieldUsage } from ".." |
|
3
|
|
|
4
|
const index: SearchTypes.ResolvedSearchIndexDefinition = { |
|
5
|
name: "product", |
|
6
|
entity: "product", |
|
7
|
provider: "test", |
|
8
|
primary_key: "id", |
|
9
|
physical_name: "product", |
|
10
|
definition_hash: "hash", |
|
11
|
settings: {}, |
|
12
|
fields: { |
|
13
|
id: { type: "keyword", filterable: true }, |
|
14
|
title: { type: "text", searchable: true }, |
|
15
|
embedding: { type: "vector", dimensions: 3 }, |
|
16
|
}, |
|
17
|
seed: async function* () { |
|
18
|
yield [] |
|
19
|
}, |
|
20
|
} |
|
21
|
|
|
22
|
describe("query.search vector options", () => { |
|
23
|
it("infers vector.field when the index has a single vector field", () => { |
|
24
|
const normalized = normalizeSearchQuery({ |
|
25
|
query: { |
|
26
|
entity: "product", |
|
27
|
search_options: { vector: { value: [0.1, 0.2, 0.3] } }, |
|
28
|
}, |
|
29
|
index, |
|
30
|
}) |
|
31
|
|
|
32
|
expect(normalized.search_options?.vector?.field).toBe("embedding") |
|
33
|
expect(() => validateFieldUsage({ index, query: normalized })).not.toThrow() |
|
34
|
}) |
|
35
|
|
|
36
|
it("rejects a vector search without value or query", () => { |
|
37
|
const normalized = normalizeSearchQuery({ |
|
38
|
query: { |
|
39
|
entity: "product", |
|
40
|
search_options: { vector: { field: "embedding" } }, |
|
41
|
}, |
|
42
|
index, |
|
43
|
}) |
|
44
|
|
|
45
|
expect(() => validateFieldUsage({ index, query: normalized })).toThrow( |
|
46
|
/value.*query/ |
|
47
|
) |
|
48
|
}) |
|
49
|
|
|
50
|
it("rejects a vector value with the wrong dimensions", () => { |
|
51
|
const normalized = normalizeSearchQuery({ |
|
52
|
query: { |
|
53
|
entity: "product", |
|
54
|
search_options: { vector: { value: [0.1, 0.2] } }, |
|
55
|
}, |
|
56
|
index, |
|
57
|
}) |
|
58
|
|
|
59
|
expect(() => validateFieldUsage({ index, query: normalized })).toThrow( |
|
60
|
/expected 3 dimensions/ |
|
61
|
) |
|
62
|
}) |
|
63
|
|
|
64
|
it("allows a client embedding against an engine-embedded field", () => { |
|
65
|
const embedded: SearchTypes.ResolvedSearchIndexDefinition = { |
|
66
|
...index, |
|
67
|
fields: { |
|
68
|
...index.fields, |
|
69
|
embedding: { |
|
70
|
type: "vector", |
|
71
|
dimensions: 3, |
|
72
|
embed: true, |
|
73
|
}, |
|
74
|
}, |
|
75
|
} |
|
76
|
|
|
77
|
const normalized = normalizeSearchQuery({ |
|
78
|
query: { |
|
79
|
entity: "product", |
|
80
|
search_options: { vector: { value: [0.1, 0.2, 0.3] } }, |
|
81
|
}, |
|
82
|
index: embedded, |
|
83
|
}) |
|
84
|
|
|
85
|
expect(() => |
|
86
|
validateFieldUsage({ index: embedded, query: normalized }) |
|
87
|
).not.toThrow() |
|
88
|
}) |
|
89
|
|
|
90
|
it("allows vector.query when the field declares embed", () => { |
|
91
|
const embedded: SearchTypes.ResolvedSearchIndexDefinition = { |
|
92
|
...index, |
|
93
|
fields: { |
|
94
|
...index.fields, |
|
95
|
embedding: { |
|
96
|
type: "vector", |
|
97
|
dimensions: 3, |
|
98
|
embed: true, |
|
99
|
}, |
|
100
|
}, |
|
101
|
} |
|
102
|
|
|
103
|
const normalized = normalizeSearchQuery({ |
|
104
|
query: { |
|
105
|
entity: "product", |
|
106
|
search_options: { vector: { query: "red shoes" } }, |
|
107
|
}, |
|
108
|
index: embedded, |
|
109
|
}) |
|
110
|
|
|
111
|
expect(() => |
|
112
|
validateFieldUsage({ index: embedded, query: normalized }) |
|
113
|
).not.toThrow() |
|
114
|
}) |
|
115
|
|
|
116
|
it("rejects vector.query when the field does not declare embed", () => { |
|
117
|
const normalized = normalizeSearchQuery({ |
|
118
|
query: { |
|
119
|
entity: "product", |
|
120
|
search_options: { vector: { query: "red shoes" } }, |
|
121
|
}, |
|
122
|
index, |
|
123
|
}) |
|
124
|
|
|
125
|
expect(() => validateFieldUsage({ index, query: normalized })).toThrow( |
|
126
|
/declare "embed"/ |
|
127
|
) |
|
128
|
}) |
|
129
|
|
|
130
|
it("does not retrieve vector fields unless retrievable is true", () => { |
|
131
|
const normalized = normalizeSearchQuery({ |
|
132
|
query: { entity: "product" }, |
|
133
|
index, |
|
134
|
}) |
|
135
|
|
|
136
|
expect(normalized.attributes_to_retrieve).not.toContain("embedding") |
|
137
|
expect(normalized.attributes_to_retrieve).toEqual(["id", "title"]) |
|
138
|
|
|
139
|
const optedIn: SearchTypes.ResolvedSearchIndexDefinition = { |
|
140
|
...index, |
|
141
|
fields: { |
|
142
|
...index.fields, |
|
143
|
embedding: { |
|
144
|
type: "vector", |
|
145
|
dimensions: 3, |
|
146
|
retrievable: true, |
|
147
|
}, |
|
148
|
}, |
|
149
|
} |
|
150
|
|
|
151
|
const retrieved = normalizeSearchQuery({ |
|
152
|
query: { entity: "product" }, |
|
153
|
index: optedIn, |
|
154
|
}) |
|
155
|
|
|
156
|
expect(retrieved.attributes_to_retrieve).toContain("embedding") |
|
157
|
}) |
|
158
|
|
|
159
|
it("does not retrieve engine-embedded vector fields", () => { |
|
160
|
const embedded: SearchTypes.ResolvedSearchIndexDefinition = { |
|
161
|
...index, |
|
162
|
fields: { |
|
163
|
...index.fields, |
|
164
|
embedding: { |
|
165
|
type: "vector", |
|
166
|
dimensions: 3, |
|
167
|
embed: true, |
|
168
|
}, |
|
169
|
}, |
|
170
|
} |
|
171
|
|
|
172
|
const normalized = normalizeSearchQuery({ |
|
173
|
query: { entity: "product" }, |
|
174
|
index: embedded, |
|
175
|
}) |
|
176
|
|
|
177
|
expect(normalized.attributes_to_retrieve).not.toContain("embedding") |
|
178
|
expect(normalized.attributes_to_retrieve).toContain("title") |
|
179
|
}) |
|
180
|
}) |