Skip to content

Add 'type' field support for Search Index creation. #1438

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Aug 1, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@
* A model describing the creation of a single Atlas Search index.
*
* @since 4.11
* @mongodb.server.release 7.0
* @mongodb.server.release 6.0
*/
public final class SearchIndexModel {
@Nullable
private final String name;
private final Bson definition;
@Nullable
private final SearchIndexType type;

/**
* Construct an instance with the given Atlas Search index mapping definition.
Expand All @@ -42,8 +44,7 @@ public final class SearchIndexModel {
* @param definition the search index mapping definition.
*/
public SearchIndexModel(final Bson definition) {
this.definition = notNull("definition", definition);
this.name = null;
this(null, definition, null);
}

/**
Expand All @@ -53,8 +54,21 @@ public SearchIndexModel(final Bson definition) {
* @param definition the search index mapping definition.
*/
public SearchIndexModel(final String name, final Bson definition) {
this(name, definition, null);
}

/**
* Construct an instance with the given Atlas Search name, index definition, and type.
*
* @param name the search index name.
* @param definition the search index mapping definition.
* @param type the search index type.
* @since 5.2
*/
public SearchIndexModel(@Nullable final String name, final Bson definition, @Nullable final SearchIndexType type) {
this.definition = notNull("definition", definition);
this.name = notNull("name", name);
this.name = name;
this.type = type;
}

/**
Expand All @@ -76,11 +90,23 @@ public String getName() {
return name;
}

/**
* Get the Atlas Search index type.
*
* @return the search index type.
* @since 5.2
*/
@Nullable
public SearchIndexType getType() {
return type;
}

@Override
public String toString() {
return "SearchIndexModel{"
+ "name=" + name
+ ", definition=" + definition
+ ", type=" + (type == null ? "null" : type.toBsonValue())
+ '}';
}
}
83 changes: 83 additions & 0 deletions driver-core/src/main/com/mongodb/client/model/SearchIndexType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2008-present MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.mongodb.client.model;

import com.mongodb.annotations.Sealed;
import org.bson.BsonString;
import org.bson.BsonValue;

import static com.mongodb.assertions.Assertions.notNull;

/**
* This interface represents an Atlas Search Index type, which is utilized for creating specific types of indexes.
* <p>
* It provides methods for creating and converting Atlas Search Index types to {@link BsonValue}.
* </p>
*
* @mongodb.server.release 6.0
* @see SearchIndexModel The model class that utilizes this index type.
* @since 5.2
*/
@Sealed
public interface SearchIndexType {

/**
* Returns a {@link SearchIndexType} instance representing the "search" index type.
*
* @return The requested {@link SearchIndexType}.
*/
static SearchIndexType search() {
return new SearchIndexTypeBson(new BsonString("search"));
}

/**
* Returns a {@link SearchIndexType} instance representing the "vectorSearch" index type.
*
* @return The requested {@link SearchIndexType}.
*/
static SearchIndexType vectorSearch() {
return new SearchIndexTypeBson(new BsonString("vectorSearch"));
}

/**
* Creates a {@link SearchIndexType} from a {@link BsonValue} in situations when there is no builder method
* that better satisfies your needs.
* This method cannot be used to validate the syntax.
* <p>
* <i>Example</i><br>
* The following code creates two functionally equivalent {@link SearchIndexType}s,
* though they may not be {@linkplain Object#equals(Object) equal}.
* <pre>{@code
* SearchIndexType type1 = SearchIndexType.vectorSearch();
* SearchIndexType type2 = SearchIndexType.of(new BsonString("vectorSearch"));
* }</pre>
*
* @param indexType A {@link BsonValue} representing the required {@link SearchIndexType}.
* @return The requested {@link SearchIndexType}.
*/
static SearchIndexType of(final BsonValue indexType) {
notNull("indexType", indexType);
return new SearchIndexTypeBson(indexType);
}

/**
* Converts this object to {@link BsonValue}.
*
* @return A {@link BsonValue} representing this {@link SearchIndexType}.
*/
BsonValue toBsonValue();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.mongodb.client.model;

/*
* Copyright 2008-present MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import org.bson.BsonValue;

import java.util.Objects;

final class SearchIndexTypeBson implements SearchIndexType {
private final BsonValue bsonValue;

SearchIndexTypeBson(final BsonValue bsonValue) {
this.bsonValue = bsonValue;
}

@Override
public BsonValue toBsonValue() {
return bsonValue;
}

@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
SearchIndexTypeBson that = (SearchIndexTypeBson) o;
return Objects.equals(bsonValue, that.bsonValue);
}

@Override
public int hashCode() {
return Objects.hash(bsonValue);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.mongodb.internal.operation;

import com.mongodb.MongoNamespace;
import com.mongodb.client.model.SearchIndexType;
import org.bson.BsonArray;
import org.bson.BsonDocument;
import org.bson.BsonString;
Expand Down Expand Up @@ -52,6 +53,10 @@ private static BsonDocument convert(final SearchIndexRequest request) {
if (searchIndexName != null) {
bsonIndexRequest.append("name", new BsonString(searchIndexName));
}
SearchIndexType searchIndexType = request.getSearchIndexType();
if (searchIndexType != null) {
bsonIndexRequest.append("type", searchIndexType.toBsonValue());
}
bsonIndexRequest.append("definition", request.getDefinition());
return bsonIndexRequest;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import com.mongodb.client.model.ReplaceOptions;
import com.mongodb.client.model.ReturnDocument;
import com.mongodb.client.model.SearchIndexModel;
import com.mongodb.client.model.SearchIndexType;
import com.mongodb.client.model.UpdateManyModel;
import com.mongodb.client.model.UpdateOneModel;
import com.mongodb.client.model.UpdateOptions;
Expand Down Expand Up @@ -752,7 +753,8 @@ private List<BsonDocument> toBsonDocumentList(@Nullable final List<? extends Bso
private SearchIndexRequest createSearchIndexRequest(final SearchIndexModel model) {
BsonDocument definition = assertNotNull(toBsonDocument(model.getDefinition()));
String indexName = model.getName();
SearchIndexType searchIndexType = model.getType();

return new SearchIndexRequest(definition, indexName);
return new SearchIndexRequest(definition, indexName, searchIndexType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.mongodb.internal.operation;

import com.mongodb.client.model.SearchIndexType;
import com.mongodb.lang.Nullable;
import org.bson.BsonDocument;

Expand All @@ -34,11 +35,18 @@ final class SearchIndexRequest {
private final BsonDocument definition;
@Nullable
private final String indexName;
@Nullable
private final SearchIndexType searchIndexType;

SearchIndexRequest(final BsonDocument definition, @Nullable final String indexName) {
SearchIndexRequest(final BsonDocument definition, @Nullable final String indexName, @Nullable final SearchIndexType searchIndexType) {
assertNotNull(definition);
this.definition = definition;
this.indexName = indexName;
this.searchIndexType = searchIndexType;
}

SearchIndexRequest(final BsonDocument definition, @Nullable final String indexName) {
this(definition, indexName, null);
}

public BsonDocument getDefinition() {
Expand All @@ -49,4 +57,9 @@ public BsonDocument getDefinition() {
public String getIndexName() {
return indexName;
}
@Nullable
public SearchIndexType getSearchIndexType() {
return searchIndexType;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
"mappings": {
"dynamic": true
}
}
},
"type": "search"
}
},
"expectError": {
Expand All @@ -73,7 +74,8 @@
"mappings": {
"dynamic": true
}
}
},
"type": "search"
}
],
"$db": "database0"
Expand All @@ -97,7 +99,8 @@
"dynamic": true
}
},
"name": "test index"
"name": "test index",
"type": "search"
}
},
"expectError": {
Expand All @@ -121,7 +124,68 @@
"dynamic": true
}
},
"name": "test index"
"name": "test index",
"type": "search"
}
],
"$db": "database0"
}
}
}
]
}
]
},
{
"description": "create a vector search index",
"operations": [
{
"name": "createSearchIndex",
"object": "collection0",
"arguments": {
"model": {
"definition": {
"fields": [
{
"type": "vector",
"path": "plot_embedding",
"numDimensions": 1536,
"similarity": "euclidean"
}
]
},
"name": "test index",
"type": "vectorSearch"
}
},
"expectError": {
"isError": true,
"errorContains": "Atlas"
}
}
],
"expectEvents": [
{
"client": "client0",
"events": [
{
"commandStartedEvent": {
"command": {
"createSearchIndexes": "collection0",
"indexes": [
{
"definition": {
"fields": [
{
"type": "vector",
"path": "plot_embedding",
"numDimensions": 1536,
"similarity": "euclidean"
}
]
},
"name": "test index",
"type": "vectorSearch"
}
],
"$db": "database0"
Expand Down
Loading