Skip to content

Commit 6740e9b

Browse files
vbabaninstIncMale
andauthored
Add 'type' field support for Search Index creation. (#1438)
JAVA-5323 --------- Co-authored-by: Valentin Kovalenko <[email protected]>
1 parent 83ff78a commit 6740e9b

File tree

17 files changed

+477
-55
lines changed

17 files changed

+477
-55
lines changed

driver-core/src/main/com/mongodb/client/model/SearchIndexModel.java

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@
2525
* A model describing the creation of a single Atlas Search index.
2626
*
2727
* @since 4.11
28-
* @mongodb.server.release 7.0
28+
* @mongodb.server.release 6.0
2929
*/
3030
public final class SearchIndexModel {
3131
@Nullable
3232
private final String name;
3333
private final Bson definition;
34+
@Nullable
35+
private final SearchIndexType type;
3436

3537
/**
3638
* Construct an instance with the given Atlas Search index mapping definition.
@@ -42,8 +44,7 @@ public final class SearchIndexModel {
4244
* @param definition the search index mapping definition.
4345
*/
4446
public SearchIndexModel(final Bson definition) {
45-
this.definition = notNull("definition", definition);
46-
this.name = null;
47+
this(null, definition, null);
4748
}
4849

4950
/**
@@ -53,8 +54,21 @@ public SearchIndexModel(final Bson definition) {
5354
* @param definition the search index mapping definition.
5455
*/
5556
public SearchIndexModel(final String name, final Bson definition) {
57+
this(name, definition, null);
58+
}
59+
60+
/**
61+
* Construct an instance with the given Atlas Search name, index definition, and type.
62+
*
63+
* @param name the search index name.
64+
* @param definition the search index mapping definition.
65+
* @param type the search index type.
66+
* @since 5.2
67+
*/
68+
public SearchIndexModel(@Nullable final String name, final Bson definition, @Nullable final SearchIndexType type) {
5669
this.definition = notNull("definition", definition);
57-
this.name = notNull("name", name);
70+
this.name = name;
71+
this.type = type;
5872
}
5973

6074
/**
@@ -76,11 +90,23 @@ public String getName() {
7690
return name;
7791
}
7892

93+
/**
94+
* Get the Atlas Search index type.
95+
*
96+
* @return the search index type.
97+
* @since 5.2
98+
*/
99+
@Nullable
100+
public SearchIndexType getType() {
101+
return type;
102+
}
103+
79104
@Override
80105
public String toString() {
81106
return "SearchIndexModel{"
82107
+ "name=" + name
83108
+ ", definition=" + definition
109+
+ ", type=" + (type == null ? "null" : type.toBsonValue())
84110
+ '}';
85111
}
86112
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.mongodb.client.model;
18+
19+
import com.mongodb.annotations.Sealed;
20+
import org.bson.BsonString;
21+
import org.bson.BsonValue;
22+
23+
import static com.mongodb.assertions.Assertions.notNull;
24+
25+
/**
26+
* This interface represents an Atlas Search Index type, which is utilized for creating specific types of indexes.
27+
* <p>
28+
* It provides methods for creating and converting Atlas Search Index types to {@link BsonValue}.
29+
* </p>
30+
*
31+
* @mongodb.server.release 6.0
32+
* @see SearchIndexModel The model class that utilizes this index type.
33+
* @since 5.2
34+
*/
35+
@Sealed
36+
public interface SearchIndexType {
37+
38+
/**
39+
* Returns a {@link SearchIndexType} instance representing the "search" index type.
40+
*
41+
* @return The requested {@link SearchIndexType}.
42+
*/
43+
static SearchIndexType search() {
44+
return new SearchIndexTypeBson(new BsonString("search"));
45+
}
46+
47+
/**
48+
* Returns a {@link SearchIndexType} instance representing the "vectorSearch" index type.
49+
*
50+
* @return The requested {@link SearchIndexType}.
51+
*/
52+
static SearchIndexType vectorSearch() {
53+
return new SearchIndexTypeBson(new BsonString("vectorSearch"));
54+
}
55+
56+
/**
57+
* Creates a {@link SearchIndexType} from a {@link BsonValue} in situations when there is no builder method
58+
* that better satisfies your needs.
59+
* This method cannot be used to validate the syntax.
60+
* <p>
61+
* <i>Example</i><br>
62+
* The following code creates two functionally equivalent {@link SearchIndexType}s,
63+
* though they may not be {@linkplain Object#equals(Object) equal}.
64+
* <pre>{@code
65+
* SearchIndexType type1 = SearchIndexType.vectorSearch();
66+
* SearchIndexType type2 = SearchIndexType.of(new BsonString("vectorSearch"));
67+
* }</pre>
68+
*
69+
* @param indexType A {@link BsonValue} representing the required {@link SearchIndexType}.
70+
* @return The requested {@link SearchIndexType}.
71+
*/
72+
static SearchIndexType of(final BsonValue indexType) {
73+
notNull("indexType", indexType);
74+
return new SearchIndexTypeBson(indexType);
75+
}
76+
77+
/**
78+
* Converts this object to {@link BsonValue}.
79+
*
80+
* @return A {@link BsonValue} representing this {@link SearchIndexType}.
81+
*/
82+
BsonValue toBsonValue();
83+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.mongodb.client.model;
2+
3+
/*
4+
* Copyright 2008-present MongoDB, Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import org.bson.BsonValue;
20+
21+
import java.util.Objects;
22+
23+
final class SearchIndexTypeBson implements SearchIndexType {
24+
private final BsonValue bsonValue;
25+
26+
SearchIndexTypeBson(final BsonValue bsonValue) {
27+
this.bsonValue = bsonValue;
28+
}
29+
30+
@Override
31+
public BsonValue toBsonValue() {
32+
return bsonValue;
33+
}
34+
35+
@Override
36+
public boolean equals(final Object o) {
37+
if (this == o) {
38+
return true;
39+
}
40+
if (o == null || getClass() != o.getClass()) {
41+
return false;
42+
}
43+
SearchIndexTypeBson that = (SearchIndexTypeBson) o;
44+
return Objects.equals(bsonValue, that.bsonValue);
45+
}
46+
47+
@Override
48+
public int hashCode() {
49+
return Objects.hash(bsonValue);
50+
}
51+
}
52+

driver-core/src/main/com/mongodb/internal/operation/CreateSearchIndexesOperation.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.mongodb.internal.operation;
1818

1919
import com.mongodb.MongoNamespace;
20+
import com.mongodb.client.model.SearchIndexType;
2021
import org.bson.BsonArray;
2122
import org.bson.BsonDocument;
2223
import org.bson.BsonString;
@@ -52,6 +53,10 @@ private static BsonDocument convert(final SearchIndexRequest request) {
5253
if (searchIndexName != null) {
5354
bsonIndexRequest.append("name", new BsonString(searchIndexName));
5455
}
56+
SearchIndexType searchIndexType = request.getSearchIndexType();
57+
if (searchIndexType != null) {
58+
bsonIndexRequest.append("type", searchIndexType.toBsonValue());
59+
}
5560
bsonIndexRequest.append("definition", request.getDefinition());
5661
return bsonIndexRequest;
5762
}

driver-core/src/main/com/mongodb/internal/operation/Operations.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import com.mongodb.client.model.ReplaceOptions;
4949
import com.mongodb.client.model.ReturnDocument;
5050
import com.mongodb.client.model.SearchIndexModel;
51+
import com.mongodb.client.model.SearchIndexType;
5152
import com.mongodb.client.model.UpdateManyModel;
5253
import com.mongodb.client.model.UpdateOneModel;
5354
import com.mongodb.client.model.UpdateOptions;
@@ -752,7 +753,8 @@ private List<BsonDocument> toBsonDocumentList(@Nullable final List<? extends Bso
752753
private SearchIndexRequest createSearchIndexRequest(final SearchIndexModel model) {
753754
BsonDocument definition = assertNotNull(toBsonDocument(model.getDefinition()));
754755
String indexName = model.getName();
756+
SearchIndexType searchIndexType = model.getType();
755757

756-
return new SearchIndexRequest(definition, indexName);
758+
return new SearchIndexRequest(definition, indexName, searchIndexType);
757759
}
758760
}

driver-core/src/main/com/mongodb/internal/operation/SearchIndexRequest.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.mongodb.internal.operation;
1818

19+
import com.mongodb.client.model.SearchIndexType;
1920
import com.mongodb.lang.Nullable;
2021
import org.bson.BsonDocument;
2122

@@ -34,11 +35,18 @@ final class SearchIndexRequest {
3435
private final BsonDocument definition;
3536
@Nullable
3637
private final String indexName;
38+
@Nullable
39+
private final SearchIndexType searchIndexType;
3740

38-
SearchIndexRequest(final BsonDocument definition, @Nullable final String indexName) {
41+
SearchIndexRequest(final BsonDocument definition, @Nullable final String indexName, @Nullable final SearchIndexType searchIndexType) {
3942
assertNotNull(definition);
4043
this.definition = definition;
4144
this.indexName = indexName;
45+
this.searchIndexType = searchIndexType;
46+
}
47+
48+
SearchIndexRequest(final BsonDocument definition, @Nullable final String indexName) {
49+
this(definition, indexName, null);
4250
}
4351

4452
public BsonDocument getDefinition() {
@@ -49,4 +57,9 @@ public BsonDocument getDefinition() {
4957
public String getIndexName() {
5058
return indexName;
5159
}
60+
@Nullable
61+
public SearchIndexType getSearchIndexType() {
62+
return searchIndexType;
63+
}
64+
5265
}

driver-core/src/test/resources/unified-test-format/index-management/createSearchIndex.json

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@
5050
"mappings": {
5151
"dynamic": true
5252
}
53-
}
53+
},
54+
"type": "search"
5455
}
5556
},
5657
"expectError": {
@@ -73,7 +74,8 @@
7374
"mappings": {
7475
"dynamic": true
7576
}
76-
}
77+
},
78+
"type": "search"
7779
}
7880
],
7981
"$db": "database0"
@@ -97,7 +99,8 @@
9799
"dynamic": true
98100
}
99101
},
100-
"name": "test index"
102+
"name": "test index",
103+
"type": "search"
101104
}
102105
},
103106
"expectError": {
@@ -121,7 +124,68 @@
121124
"dynamic": true
122125
}
123126
},
124-
"name": "test index"
127+
"name": "test index",
128+
"type": "search"
129+
}
130+
],
131+
"$db": "database0"
132+
}
133+
}
134+
}
135+
]
136+
}
137+
]
138+
},
139+
{
140+
"description": "create a vector search index",
141+
"operations": [
142+
{
143+
"name": "createSearchIndex",
144+
"object": "collection0",
145+
"arguments": {
146+
"model": {
147+
"definition": {
148+
"fields": [
149+
{
150+
"type": "vector",
151+
"path": "plot_embedding",
152+
"numDimensions": 1536,
153+
"similarity": "euclidean"
154+
}
155+
]
156+
},
157+
"name": "test index",
158+
"type": "vectorSearch"
159+
}
160+
},
161+
"expectError": {
162+
"isError": true,
163+
"errorContains": "Atlas"
164+
}
165+
}
166+
],
167+
"expectEvents": [
168+
{
169+
"client": "client0",
170+
"events": [
171+
{
172+
"commandStartedEvent": {
173+
"command": {
174+
"createSearchIndexes": "collection0",
175+
"indexes": [
176+
{
177+
"definition": {
178+
"fields": [
179+
{
180+
"type": "vector",
181+
"path": "plot_embedding",
182+
"numDimensions": 1536,
183+
"similarity": "euclidean"
184+
}
185+
]
186+
},
187+
"name": "test index",
188+
"type": "vectorSearch"
125189
}
126190
],
127191
"$db": "database0"

0 commit comments

Comments
 (0)