Skip to content

Commit 40c223c

Browse files
split tests for native array and BinaryVector
introduce internal search field abstraction remove methods on DefaultSearchIndexOperations
1 parent a510fd6 commit 40c223c

File tree

6 files changed

+150
-150
lines changed

6 files changed

+150
-150
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/DefaultSearchIndexOperations.java

Lines changed: 21 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public DefaultSearchIndexOperations(MongoOperations mongoOperations, Class<?> ty
4747
}
4848

4949
public DefaultSearchIndexOperations(MongoOperations mongoOperations, String collectionName, @Nullable Class<?> type) {
50+
5051
this.collectionName = collectionName;
5152

5253
if (type != null) {
@@ -68,89 +69,52 @@ public String ensureIndex(SearchIndexDefinition indexDefinition) {
6869
mongoOperations.getConverter().getMappingContext());
6970

7071
mongoOperations.getCollection(collectionName)
71-
.createSearchIndexes(List.of(new SearchIndexModel(indexDefinition.getName(), index.get("definition", Document.class),
72-
SearchIndexType.of(new BsonString(indexDefinition.getType())))));
72+
.createSearchIndexes(List.of(new SearchIndexModel(indexDefinition.getName(),
73+
index.get("definition", Document.class), SearchIndexType.of(new BsonString(indexDefinition.getType())))));
7374

7475
return indexDefinition.getName();
7576
}
7677

7778
@Override
78-
public void updateIndex(SearchIndexDefinition index) {
79-
80-
if (index instanceof VectorIndex) {
81-
throw new UnsupportedOperationException("Vector Index definitions cannot be updated");
82-
}
79+
public void updateIndex(SearchIndexDefinition indexDefinition) {
8380

84-
Document indexDocument = index.getIndexDocument(entityTypeInformation,
81+
Document indexDocument = indexDefinition.getIndexDocument(entityTypeInformation,
8582
mongoOperations.getConverter().getMappingContext());
8683

87-
mongoOperations.getCollection(collectionName).updateSearchIndex(index.getName(), indexDocument);
84+
mongoOperations.getCollection(collectionName).updateSearchIndex(indexDefinition.getName(), indexDocument);
8885
}
8986

9087
@Override
9188
public boolean exists(String indexName) {
92-
93-
List<Document> indexes = getSearchIndexes(indexName);
94-
95-
for (Document index : indexes) {
96-
if (index.getString("name").equals(indexName)) {
97-
return true;
98-
}
99-
}
100-
101-
return false;
89+
return getSearchIndex(indexName) != null;
10290
}
10391

10492
@Override
105-
public SearchIndexStatus status(String name) {
106-
107-
List<Document> indexes = getSearchIndexes(name);
93+
public SearchIndexStatus status(String indexName) {
10894

109-
if (indexes.isEmpty()) {
110-
return SearchIndexStatus.DOES_NOT_EXIST;
111-
}
112-
113-
for (Document index : indexes) {
114-
if (index.getString("name").equals(name)) {
115-
return SearchIndexStatus.valueOf(index.getString("status"));
116-
}
117-
}
118-
return SearchIndexStatus.DOES_NOT_EXIST;
95+
Document searchIndex = getSearchIndex(indexName);
96+
return searchIndex != null ? SearchIndexStatus.valueOf(searchIndex.getString("status"))
97+
: SearchIndexStatus.DOES_NOT_EXIST;
11998
}
12099

121100
@Override
122-
public List<IndexInfo> getIndexInfo() {
123-
124-
List<Document> aggregate = getSearchIndexes(null);
125-
126-
ArrayList<IndexInfo> result = new ArrayList<>();
127-
for (Document doc : aggregate) {
128-
129-
List<IndexField> indexFields = new ArrayList<>();
130-
String name = doc.getString("name");
131-
for (Object field : doc.get("latestDefinition", Document.class).get("fields", List.class)) {
132-
133-
if (field instanceof Document fieldInfo) {
134-
indexFields.add(IndexField.vector(fieldInfo.getString("path")));
135-
}
136-
}
137-
138-
result.add(new IndexInfo(indexFields, name, false, false, null, false));
139-
}
140-
return result;
101+
public void dropAllIndexes() {
102+
getSearchIndexes(null).forEach(indexInfo -> dropIndex(indexInfo.getString("name")));
141103
}
142104

143105
@Override
144-
public void dropAllIndexes() {
145-
getIndexInfo().forEach(indexInfo -> dropIndex(indexInfo.getName()));
106+
public void dropIndex(String indexName) {
107+
mongoOperations.getCollection(collectionName).dropSearchIndex(indexName);
146108
}
147109

148-
@Override
149-
public void dropIndex(String name) {
150-
mongoOperations.getCollection(collectionName).dropSearchIndex(name);
110+
@Nullable
111+
private Document getSearchIndex(String indexName) {
112+
113+
List<Document> indexes = getSearchIndexes(indexName);
114+
return indexes.isEmpty() ? null : indexes.iterator().next();
151115
}
152116

153-
private List<Document> getSearchIndexes(String indexName) {
117+
private List<Document> getSearchIndexes(@Nullable String indexName) {
154118

155119
Document filter = StringUtils.hasText(indexName) ? new Document("name", indexName) : new Document();
156120

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/SearchIndexDefinition.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package org.springframework.data.mongodb.core.index;
1717

1818
import org.bson.Document;
19-
2019
import org.springframework.data.mapping.context.MappingContext;
2120
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
2221
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
@@ -47,9 +46,9 @@ public interface SearchIndexDefinition {
4746
* resulting document contains the index name, type and {@link #getDefinition(TypeInformation, MappingContext)
4847
* definition}.
4948
*
50-
* @param entity
49+
* @param entity can be {@literal null}.
5150
* @param mappingContext
52-
* @return
51+
* @return never {@literal null}.
5352
*/
5453
default Document getIndexDocument(@Nullable TypeInformation<?> entity,
5554
MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext) {
@@ -66,13 +65,11 @@ default Document getIndexDocument(@Nullable TypeInformation<?> entity,
6665
* Returns the actual index definition for this index in the context of a potential entity to resolve field name
6766
* mappings.
6867
*
69-
* @param entity
68+
* @param entity can be {@literal null}.
7069
* @param mappingContext
71-
* @return
70+
* @return never {@literal null}.
7271
*/
7372
Document getDefinition(@Nullable TypeInformation<?> entity,
7473
MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext);
7574

76-
77-
7875
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/SearchIndexOperations.java

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,69 +15,61 @@
1515
*/
1616
package org.springframework.data.mongodb.core.index;
1717

18-
import java.util.List;
18+
import org.springframework.dao.DataAccessException;
1919

2020
/**
2121
* Search Index operations on a collection for Atlas Search.
2222
*
2323
* @author Christoph Strobl
2424
* @author Mark Paluch
2525
* @since 4.5
26+
* @see VectorIndex
2627
*/
2728
public interface SearchIndexOperations {
2829

2930
/**
30-
* Ensure that an index for the provided {@link SearchIndexDefinition} exists for the collection indicated by the
31-
* entity class. If not it will be created.
31+
* Create the index for the given {@link SearchIndexDefinition} in the collection indicated by the entity class.
3232
*
3333
* @param indexDefinition must not be {@literal null}.
3434
* @return the index name.
3535
*/
3636
String ensureIndex(SearchIndexDefinition indexDefinition);
3737

3838
/**
39-
* Alters the search {@code index}.
39+
* Alters the search index matching the index {@link SearchIndexDefinition#getName() name}.
4040
* <p>
41-
* Note that Atlas Search does not support updating Vector Search Indices resulting in
42-
* {@link UnsupportedOperationException}.
41+
* Atlas Search might not support updating indices which raises a {@link DataAccessException}.
4342
*
44-
* @param index the index definition.
43+
* @param indexDefinition the index definition.
4544
*/
46-
void updateIndex(SearchIndexDefinition index);
45+
void updateIndex(SearchIndexDefinition indexDefinition);
4746

4847
/**
49-
* Check whether an index with the {@code name} exists. To ensure an existing index is queryable it is recommended to
50-
* check its {@link #status(String) status}.
48+
* Check whether an index with the given {@code indexName} exists for the collection indicated by the entity class. To
49+
* ensure an existing index is queryable it is recommended to check its {@link #status(String) status}.
5150
*
52-
* @param name name of index to check for presence.
51+
* @param indexName name of index to check for presence.
5352
* @return {@literal true} if the index exists; {@literal false} otherwise.
5453
*/
55-
boolean exists(String name);
54+
boolean exists(String indexName);
5655

5756
/**
58-
* Check the actual {@link SearchIndexStatus} of an index.
57+
* Check the actual {@link SearchIndexStatus status} of an index.
5958
*
60-
* @param name name of index to check for presence.
61-
* @return {@literal true} if the index exists; {@literal false} otherwise.
59+
* @param indexName name of index to get the status for.
60+
* @return the current status of the index or {@link SearchIndexStatus#DOES_NOT_EXIST} if the index cannot be found.
6261
*/
63-
SearchIndexStatus status(String name);
62+
SearchIndexStatus status(String indexName);
6463

6564
/**
66-
* Drops an index from this collection.
65+
* Drops an index from the collection indicated by the entity class.
6766
*
68-
* @param name name of index to drop.
67+
* @param indexName name of index to drop.
6968
*/
70-
void dropIndex(String name);
69+
void dropIndex(String indexName);
7170

7271
/**
73-
* Drops all search indices from this collection.
72+
* Drops all search indices from the collection indicated by the entity class.
7473
*/
7574
void dropAllIndexes();
76-
77-
/**
78-
* Returns the index information on the collection.
79-
*
80-
* @return index information on the collection
81-
*/
82-
List<IndexInfo> getIndexInfo();
8375
}

0 commit comments

Comments
 (0)