@@ -47,6 +47,7 @@ public DefaultSearchIndexOperations(MongoOperations mongoOperations, Class<?> ty
47
47
}
48
48
49
49
public DefaultSearchIndexOperations (MongoOperations mongoOperations , String collectionName , @ Nullable Class <?> type ) {
50
+
50
51
this .collectionName = collectionName ;
51
52
52
53
if (type != null ) {
@@ -68,89 +69,52 @@ public String ensureIndex(SearchIndexDefinition indexDefinition) {
68
69
mongoOperations .getConverter ().getMappingContext ());
69
70
70
71
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 ())))));
73
74
74
75
return indexDefinition .getName ();
75
76
}
76
77
77
78
@ 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 ) {
83
80
84
- Document indexDocument = index .getIndexDocument (entityTypeInformation ,
81
+ Document indexDocument = indexDefinition .getIndexDocument (entityTypeInformation ,
85
82
mongoOperations .getConverter ().getMappingContext ());
86
83
87
- mongoOperations .getCollection (collectionName ).updateSearchIndex (index .getName (), indexDocument );
84
+ mongoOperations .getCollection (collectionName ).updateSearchIndex (indexDefinition .getName (), indexDocument );
88
85
}
89
86
90
87
@ Override
91
88
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 ;
102
90
}
103
91
104
92
@ Override
105
- public SearchIndexStatus status (String name ) {
106
-
107
- List <Document > indexes = getSearchIndexes (name );
93
+ public SearchIndexStatus status (String indexName ) {
108
94
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 ;
119
98
}
120
99
121
100
@ 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" )));
141
103
}
142
104
143
105
@ Override
144
- public void dropAllIndexes ( ) {
145
- getIndexInfo (). forEach ( indexInfo -> dropIndex ( indexInfo . getName ()) );
106
+ public void dropIndex ( String indexName ) {
107
+ mongoOperations . getCollection ( collectionName ). dropSearchIndex ( indexName );
146
108
}
147
109
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 ();
151
115
}
152
116
153
- private List <Document > getSearchIndexes (String indexName ) {
117
+ private List <Document > getSearchIndexes (@ Nullable String indexName ) {
154
118
155
119
Document filter = StringUtils .hasText (indexName ) ? new Document ("name" , indexName ) : new Document ();
156
120
0 commit comments