Skip to content

Commit b746fcc

Browse files
authored
DRIVERS-2768 [Vector Search GA] Add support for types in search index creation (#1541)
* DRIVERS-2768 [Vector Search GA] Add support for the new type field when creating search indexes
1 parent b1e0274 commit b746fcc

7 files changed

+310
-17
lines changed

source/index-management/index-management.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ interface IndexOptions {
664664
* @example For an index of name: 1, age: -1, the generated name would be "name_1_age_-1".
665665
*/
666666
name: String;
667-
667+
668668
/**
669669
* Optionally tells the index to only reference documents with the specified field in
670670
* the index.
@@ -903,6 +903,9 @@ interface SearchIndexModel {
903903

904904
// The name for this index, if present.
905905
name: Optional<string>;
906+
907+
// The type for this index, if present. Can be either "search" or "vectorSearch".
908+
type: Optional<string>;
906909
}
907910

908911
interface SearchIndexOptions {

source/index-management/index-management.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,16 @@ Common API Components
693693
*/
694694
name: String;
695695
696+
/**
697+
* Optionally specify a type for the index. Defaults to "search" if not provided.
698+
* Either "search" for regular search indexes or "vectorSearch" for vector search indexes.
699+
*
700+
* Note that to create a vector search index using a helper method, the type "vectorSearch" must be provided.
701+
*
702+
*/
703+
type: String;
704+
705+
696706
/**
697707
* Optionally tells the index to only reference documents with the specified field in
698708
* the index.
@@ -1149,3 +1159,4 @@ Changelog
11491159
:2023-07-27: Add search index management clarifications.
11501160
:2023-11-08: Clarify that ``readConcern`` and ``writeConcern`` must not be
11511161
applied to search index management commands.
1162+
:2024-03-06: Update tests to include search index typing.

source/index-management/tests/README.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,108 @@ This test fails if it times out waiting for the deletion to succeed.
231231
of `true`.
232232

233233
6. Assert that `index` has a property `latestDefinition` whose value is `{ 'mappings': { 'dynamic': false } }`
234+
235+
#### Case 7: Driver can successfully handle search index types when creating indexes
236+
237+
01. Create a collection with the "create" command using a randomly generated name (referred to as `coll0`).
238+
239+
02. Create a new search index on `coll0` with the `createSearchIndex` helper. Use the following definition:
240+
241+
```typescript
242+
243+
{
244+
name: 'test-search-index-case7-implicit',
245+
definition: {
246+
mappings: { dynamic: false }
247+
}
248+
}
249+
```
250+
251+
03. Assert that the command returns the name of the index: `"test-search-index-case7-implicit"`.
252+
253+
04. Run `coll0.listSearchIndexes('test-search-index-case7-implicit')` repeatedly every 5 seconds until the following
254+
condition is satisfied and store the value in a variable `index1`:
255+
256+
- An index with the `name` of `test-search-index-case7-implicit` is present and the index has a field `queryable`
257+
with a value of `true`.
258+
259+
05. Assert that `index1` has a property `type` whose value is `search`.
260+
261+
06. Create a new search index on `coll0` with the `createSearchIndex` helper. Use the following definition:
262+
263+
```typescript
264+
265+
{
266+
name: 'test-search-index-case7-explicit',
267+
type: 'search',
268+
definition: {
269+
mappings: { dynamic: false }
270+
}
271+
}
272+
```
273+
274+
07. Assert that the command returns the name of the index: `"test-search-index-case7-explicit"`.
275+
276+
08. Run `coll0.listSearchIndexes('test-search-index-case7-explicit')` repeatedly every 5 seconds until the following
277+
condition is satisfied and store the value in a variable `index2`:
278+
279+
- An index with the `name` of `test-search-index-case7-explicit` is present and the index has a field `queryable`
280+
with a value of `true`.
281+
282+
09. Assert that `index2` has a property `type` whose value is `search`.
283+
284+
10. Create a new vector search index on `coll0` with the `createSearchIndex` helper. Use the following definition:
285+
286+
```typescript
287+
288+
{
289+
name: 'test-search-index-case7-vector',
290+
type: 'vectorSearch',
291+
definition: {
292+
fields: [
293+
{
294+
type: 'vector',
295+
path: 'plot_embedding',
296+
numDimensions: 1536,
297+
similarity: 'euclidean',
298+
},
299+
]
300+
}
301+
}
302+
```
303+
304+
11. Assert that the command returns the name of the index: `"test-search-index-case7-vector"`.
305+
306+
12. Run `coll0.listSearchIndexes('test-search-index-case7-vector')` repeatedly every 5 seconds until the following
307+
condition is satisfied and store the value in a variable `index3`:
308+
309+
- An index with the `name` of `test-search-index-case7-vector` is present and the index has a field `queryable` with
310+
a value of `true`.
311+
312+
13. Assert that `index3` has a property `type` whose value is `vectorSearch`.
313+
314+
#### Case 8: Driver requires explicit type to create a vector search index
315+
316+
1. Create a collection with the "create" command using a randomly generated name (referred to as `coll0`).
317+
318+
2. Create a new vector search index on `coll0` with the `createSearchIndex` helper. Use the following definition:
319+
320+
```typescript
321+
322+
{
323+
name: 'test-search-index-case8-error',
324+
definition: {
325+
fields: [
326+
{
327+
type: 'vector',
328+
path: 'plot_embedding',
329+
numDimensions: 1536,
330+
similarity: 'euclidean',
331+
},
332+
]
333+
}
334+
}
335+
```
336+
337+
3. Assert that the command throws an exception containing the string "Attribute mappings missing" due to the `mappings`
338+
field missing.

source/index-management/tests/createSearchIndex.json

Lines changed: 68 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

source/index-management/tests/createSearchIndex.yml

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ tests:
2626
- name: createSearchIndex
2727
object: *collection0
2828
arguments:
29-
model: { definition: &definition { mappings: { dynamic: true } } }
29+
model: { definition: &definition { mappings: { dynamic: true } } , type: 'search' }
3030
expectError:
3131
# This test always errors in a non-Atlas environment. The test functions as a unit test by asserting
3232
# that the driver constructs and sends the correct command.
@@ -39,15 +39,15 @@ tests:
3939
- commandStartedEvent:
4040
command:
4141
createSearchIndexes: *collection0
42-
indexes: [ { definition: *definition } ]
42+
indexes: [ { definition: *definition, type: 'search'} ]
4343
$db: *database0
4444

4545
- description: "name provided for an index definition"
4646
operations:
4747
- name: createSearchIndex
4848
object: *collection0
4949
arguments:
50-
model: { definition: &definition { mappings: { dynamic: true } } , name: 'test index' }
50+
model: { definition: &definition { mappings: { dynamic: true } } , name: 'test index', type: 'search' }
5151
expectError:
5252
# This test always errors in a non-Atlas environment. The test functions as a unit test by asserting
5353
# that the driver constructs and sends the correct command.
@@ -60,5 +60,27 @@ tests:
6060
- commandStartedEvent:
6161
command:
6262
createSearchIndexes: *collection0
63-
indexes: [ { definition: *definition, name: 'test index' } ]
63+
indexes: [ { definition: *definition, name: 'test index', type: 'search' } ]
64+
$db: *database0
65+
66+
- description: "create a vector search index"
67+
operations:
68+
- name: createSearchIndex
69+
object: *collection0
70+
arguments:
71+
model: { definition: &definition { fields: [ {"type": "vector", "path": "plot_embedding", "numDimensions": 1536, "similarity": "euclidean"} ] }
72+
, name: 'test index', type: 'vectorSearch' }
73+
expectError:
74+
# This test always errors in a non-Atlas environment. The test functions as a unit test by asserting
75+
# that the driver constructs and sends the correct command.
76+
# The expected error message was changed in SERVER-83003. Check for the substring "Atlas" shared by both error messages.
77+
isError: true
78+
errorContains: Atlas
79+
expectEvents:
80+
- client: *client0
81+
events:
82+
- commandStartedEvent:
83+
command:
84+
createSearchIndexes: *collection0
85+
indexes: [ { definition: *definition, name: 'test index', type: 'vectorSearch' } ]
6486
$db: *database0

0 commit comments

Comments
 (0)