Skip to content

Commit f7d2388

Browse files
committed
Add typed builder API for vector search index definitions
* Introduced SearchIndexDefinition sealed interface with factory methods, * VectorSearchIndexFields (vectorField, filterField, autoEmbedField builders), * HnswSearchIndexOptions for HNSW-specific parameters, and * VectorSearchIndexDefinition as the concrete implementation. Updates * Updated SearchIndexModel javadoc to reference the new builders. JAVA-6112
1 parent c0f9627 commit f7d2388

7 files changed

Lines changed: 853 additions & 0 deletions

File tree

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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.lang.Nullable;
20+
import org.bson.BsonDocument;
21+
import org.bson.BsonInt32;
22+
import org.bson.codecs.configuration.CodecRegistry;
23+
import org.bson.conversions.Bson;
24+
25+
/**
26+
* Options for the HNSW (Hierarchical Navigable Small World) indexing method in a vector search index.
27+
*
28+
* <p>This class provides a fluent builder for specifying HNSW-specific parameters when creating
29+
* a vector search index with {@code indexingMethod("hnsw")}.</p>
30+
*
31+
* <p>Since {@link VectorSearchIndexFields.VectorField#hnswOptions(Bson)} accepts any {@link Bson},
32+
* a raw {@link org.bson.Document} may also be passed directly for forward compatibility.</p>
33+
*
34+
* <pre>{@code
35+
* vectorField("embedding")
36+
* .indexingMethod("hnsw")
37+
* .hnswOptions(new HnswSearchIndexOptions().maxEdges(16).numEdgeCandidates(200))
38+
* }</pre>
39+
*
40+
* @see VectorSearchIndexFields.VectorField#hnswOptions(Bson)
41+
* @since 5.8
42+
*/
43+
public final class HnswSearchIndexOptions implements Bson {
44+
@Nullable
45+
private Integer maxEdges;
46+
@Nullable
47+
private Integer numEdgeCandidates;
48+
49+
/**
50+
* Creates a new instance with default settings.
51+
*
52+
* @since 5.8
53+
*/
54+
public HnswSearchIndexOptions() {
55+
}
56+
57+
/**
58+
* Sets the maximum number of connected neighbors for each node in the HNSW graph.
59+
*
60+
* @param maxEdges the maximum number of edges (connected neighbors)
61+
* @return this
62+
* @since 5.8
63+
*/
64+
public HnswSearchIndexOptions maxEdges(final int maxEdges) {
65+
this.maxEdges = maxEdges;
66+
return this;
67+
}
68+
69+
/**
70+
* Sets the number of nearest neighbor candidates to consider when building the HNSW graph.
71+
*
72+
* @param numEdgeCandidates the number of nearest neighbor candidates
73+
* @return this
74+
* @since 5.8
75+
*/
76+
public HnswSearchIndexOptions numEdgeCandidates(final int numEdgeCandidates) {
77+
this.numEdgeCandidates = numEdgeCandidates;
78+
return this;
79+
}
80+
81+
@Override
82+
public <TDocument> BsonDocument toBsonDocument(final Class<TDocument> documentClass, final CodecRegistry codecRegistry) {
83+
BsonDocument doc = new BsonDocument();
84+
if (maxEdges != null) {
85+
doc.append("maxEdges", new BsonInt32(maxEdges));
86+
}
87+
if (numEdgeCandidates != null) {
88+
doc.append("numEdgeCandidates", new BsonInt32(numEdgeCandidates));
89+
}
90+
return doc;
91+
}
92+
93+
@Override
94+
public String toString() {
95+
return "HnswSearchIndexOptions{"
96+
+ "maxEdges=" + maxEdges
97+
+ ", numEdgeCandidates=" + numEdgeCandidates
98+
+ '}';
99+
}
100+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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.conversions.Bson;
21+
22+
import java.util.List;
23+
24+
import static com.mongodb.assertions.Assertions.notNull;
25+
import static java.util.Arrays.asList;
26+
27+
/**
28+
* A definition for an Atlas Search index.
29+
*
30+
* <p>This interface provides factory methods for creating search index definitions
31+
* that can be passed to {@link SearchIndexModel}.</p>
32+
*
33+
* @see SearchIndexModel
34+
* @see VectorSearchIndexDefinition
35+
* @since 5.8
36+
*/
37+
@Sealed
38+
public interface SearchIndexDefinition extends Bson {
39+
40+
/**
41+
* Creates a vector search index definition with the specified fields.
42+
*
43+
* <p>The resulting definition produces a document of the form {@code {"fields": [...]}},
44+
* suitable for use with {@link SearchIndexType#vectorSearch()}.</p>
45+
*
46+
* @param fields the fields for the vector search index. Each field should be created using
47+
* {@link VectorSearchIndexFields} factory methods, or may be a raw {@link Bson} document.
48+
* @return a new {@link VectorSearchIndexDefinition}
49+
* @see VectorSearchIndexFields#vectorField(String)
50+
* @see VectorSearchIndexFields#filterField(String)
51+
* @see VectorSearchIndexFields#autoEmbedField(String)
52+
* @since 5.8
53+
*/
54+
static VectorSearchIndexDefinition vectorSearch(final Bson... fields) {
55+
notNull("fields", fields);
56+
return vectorSearch(asList(fields));
57+
}
58+
59+
/**
60+
* Creates a vector search index definition with the specified fields.
61+
*
62+
* <p>The resulting definition produces a document of the form {@code {"fields": [...]}},
63+
* suitable for use with {@link SearchIndexType#vectorSearch()}.</p>
64+
*
65+
* @param fields the fields for the vector search index. Each field should be created using
66+
* {@link VectorSearchIndexFields} factory methods, or may be a raw {@link Bson} document.
67+
* @return a new {@link VectorSearchIndexDefinition}
68+
* @see VectorSearchIndexFields#vectorField(String)
69+
* @see VectorSearchIndexFields#filterField(String)
70+
* @see VectorSearchIndexFields#autoEmbedField(String)
71+
* @since 5.8
72+
*/
73+
static VectorSearchIndexDefinition vectorSearch(final List<? extends Bson> fields) {
74+
notNull("fields", fields);
75+
return new VectorSearchIndexDefinition(fields);
76+
}
77+
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424
/**
2525
* A model describing the creation of a single Atlas Search index.
2626
*
27+
* <p>The {@code definition} parameter accepts any {@link org.bson.conversions.Bson} instance.
28+
* For vector search indexes, use the builders provided by {@link SearchIndexDefinition#vectorSearch(Bson...)}
29+
* and {@link VectorSearchIndexFields} to construct the definition.</p>
30+
*
31+
* @see SearchIndexDefinition
32+
* @see VectorSearchIndexFields
2733
* @since 4.11
2834
* @mongodb.server.release 6.0
2935
*/
@@ -42,6 +48,7 @@ public final class SearchIndexModel {
4248
* will be used to create the search index.</p>
4349
*
4450
* @param definition the search index mapping definition.
51+
* @see SearchIndexDefinition#vectorSearch(Bson...)
4552
*/
4653
public SearchIndexModel(final Bson definition) {
4754
this(null, definition, null);
@@ -52,6 +59,7 @@ public SearchIndexModel(final Bson definition) {
5259
*
5360
* @param name the search index name.
5461
* @param definition the search index mapping definition.
62+
* @see SearchIndexDefinition#vectorSearch(Bson...)
5563
*/
5664
public SearchIndexModel(final String name, final Bson definition) {
5765
this(name, definition, null);
@@ -63,6 +71,7 @@ public SearchIndexModel(final String name, final Bson definition) {
6371
* @param name the search index name.
6472
* @param definition the search index mapping definition.
6573
* @param type the search index type.
74+
* @see SearchIndexDefinition#vectorSearch(Bson...)
6675
* @since 5.2
6776
*/
6877
public SearchIndexModel(@Nullable final String name, final Bson definition, @Nullable final SearchIndexType type) {
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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 org.bson.BsonArray;
20+
import org.bson.BsonDocument;
21+
import org.bson.codecs.configuration.CodecRegistry;
22+
import org.bson.conversions.Bson;
23+
24+
import java.util.ArrayList;
25+
import java.util.List;
26+
import java.util.Objects;
27+
28+
/**
29+
* A vector search index definition, producing a document of the form {@code {"fields": [...]}}.
30+
*
31+
* <p>Instances are created via {@link SearchIndexDefinition#vectorSearch(Bson...)}.</p>
32+
*
33+
* @see SearchIndexDefinition
34+
* @see SearchIndexType#vectorSearch()
35+
* @since 5.8
36+
*/
37+
public final class VectorSearchIndexDefinition implements SearchIndexDefinition {
38+
private final List<? extends Bson> fields;
39+
40+
VectorSearchIndexDefinition(final List<? extends Bson> fields) {
41+
this.fields = new ArrayList<>(fields);
42+
}
43+
44+
@Override
45+
public <TDocument> BsonDocument toBsonDocument(final Class<TDocument> documentClass, final CodecRegistry codecRegistry) {
46+
BsonArray fieldArray = new BsonArray();
47+
for (Bson field : fields) {
48+
fieldArray.add(field.toBsonDocument(documentClass, codecRegistry));
49+
}
50+
return new BsonDocument("fields", fieldArray);
51+
}
52+
53+
@Override
54+
public boolean equals(final Object o) {
55+
if (this == o) {
56+
return true;
57+
}
58+
if (o == null || getClass() != o.getClass()) {
59+
return false;
60+
}
61+
VectorSearchIndexDefinition that = (VectorSearchIndexDefinition) o;
62+
return Objects.equals(fields, that.fields);
63+
}
64+
65+
@Override
66+
public int hashCode() {
67+
return Objects.hash(fields);
68+
}
69+
70+
@Override
71+
public String toString() {
72+
return "VectorSearchIndexDefinition{"
73+
+ "fields=" + fields
74+
+ '}';
75+
}
76+
}

0 commit comments

Comments
 (0)