Skip to content

Commit 4ac2283

Browse files
so there are still test failures. let's check tomorrow
1 parent b74f1d7 commit 4ac2283

File tree

10 files changed

+339
-256
lines changed

10 files changed

+339
-256
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/VectorSearchOperation.java

+12
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,18 @@ default LimitContributor vector(float... vector) {
452452
return vector(Vector.of(vector));
453453
}
454454

455+
/**
456+
* Array of byte numbers that represent the query vector. The number type must match the indexed field value type.
457+
* Otherwise, Atlas Vector Search doesn't return any results or errors.
458+
*
459+
* @param vector the query vector.
460+
* @return
461+
*/
462+
@Contract("_ -> this")
463+
default LimitContributor vector(byte... vector) {
464+
return vector(BinaryVector.int8Vector(vector));
465+
}
466+
455467
/**
456468
* Array of double numbers that represent the query vector. The number type must match the indexed field value type.
457469
* Otherwise, Atlas Vector Search doesn't return any results or errors.

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoConverters.java

+34-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package org.springframework.data.mongodb.core.convert;
1717

18-
import static org.springframework.data.convert.ConverterBuilder.*;
18+
import static org.springframework.data.convert.ConverterBuilder.reading;
1919

2020
import java.math.BigDecimal;
2121
import java.math.BigInteger;
@@ -27,6 +27,7 @@
2727
import java.util.Collection;
2828
import java.util.Currency;
2929
import java.util.List;
30+
import java.util.Set;
3031
import java.util.UUID;
3132
import java.util.concurrent.atomic.AtomicInteger;
3233
import java.util.concurrent.atomic.AtomicLong;
@@ -47,10 +48,10 @@
4748
import org.bson.types.Code;
4849
import org.bson.types.Decimal128;
4950
import org.bson.types.ObjectId;
50-
5151
import org.springframework.core.convert.ConversionFailedException;
5252
import org.springframework.core.convert.TypeDescriptor;
5353
import org.springframework.core.convert.converter.ConditionalConverter;
54+
import org.springframework.core.convert.converter.ConditionalGenericConverter;
5455
import org.springframework.core.convert.converter.Converter;
5556
import org.springframework.core.convert.converter.ConverterFactory;
5657
import org.springframework.data.convert.ReadingConverter;
@@ -60,6 +61,7 @@
6061
import org.springframework.data.mongodb.core.mapping.MongoVector;
6162
import org.springframework.data.mongodb.core.query.Term;
6263
import org.springframework.data.mongodb.core.script.NamedMongoScript;
64+
import org.springframework.lang.Nullable;
6365
import org.springframework.util.Assert;
6466
import org.springframework.util.NumberUtils;
6567
import org.springframework.util.StringUtils;
@@ -473,6 +475,36 @@ public Vector convert(BinaryVector source) {
473475
}
474476
}
475477

478+
// @WritingConverter
479+
// enum BytesToBinaryVectorConverter implements ConditionalGenericConverter {
480+
// INSTANCE;
481+
//
482+
// @Nullable
483+
// public BinaryVector convert(byte[] source) {
484+
// return BinaryVector.int8Vector(source);
485+
// }
486+
//
487+
// @Override
488+
// public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
489+
// return sourceType.getType() == byte[].class && targetType.getType() == BinaryVector.class;
490+
// }
491+
//
492+
// @Nullable
493+
// @Override
494+
// public Set<ConvertiblePair> getConvertibleTypes() {
495+
// return Set.of(new ConvertiblePair(byte[].class, BinaryVector.class));
496+
// }
497+
//
498+
// @Nullable
499+
// @Override
500+
// public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
501+
// if(!matches(sourceType, targetType)) {
502+
// return source;
503+
// }
504+
// return convert((byte[]) source);
505+
// }
506+
// }
507+
476508
/**
477509
* {@link ConverterFactory} implementation converting {@link AtomicLong} into {@link Long}.
478510
*

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,20 @@ public interface SearchIndexOperations {
4646
void updateIndex(SearchIndexDefinition index);
4747

4848
/**
49-
* Check whether an index with the {@code name} exists.
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}.
5051
*
5152
* @param name name of index to check for presence.
5253
* @return {@literal true} if the index exists; {@literal false} otherwise.
5354
*/
5455
boolean exists(String name);
5556

56-
57+
/**
58+
* Check the actual {@link SearchIndexStatus} of an index.
59+
*
60+
* @param name name of index to check for presence.
61+
* @return {@literal true} if the index exists; {@literal false} otherwise.
62+
*/
5763
SearchIndexStatus status(String name);
5864

5965
/**

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* https://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -16,8 +16,10 @@
1616
package org.springframework.data.mongodb.core.index;
1717

1818
/**
19+
* Representation of different conditions a search index can be in.
20+
*
1921
* @author Christoph Strobl
20-
* @since 2025/01
22+
* @since 4.5
2123
*/
2224
public enum SearchIndexStatus {
2325

@@ -30,7 +32,7 @@ public enum SearchIndexStatus {
3032
/** will cease to exist - no longer queryable */
3133
DELETING,
3234

33-
/** well, that did not work - not queryable */
35+
/** well, this one is broken - not queryable */
3436
FAILED,
3537

3638
/** busy with other things, check back later - not queryable */

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/FieldType.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import java.util.Date;
1919
import java.util.regex.Pattern;
2020

21+
import org.bson.BinaryVector;
22+
import org.bson.BsonBinary;
2123
import org.bson.types.BSONTimestamp;
2224
import org.bson.types.Binary;
2325
import org.bson.types.Code;
@@ -55,7 +57,8 @@ public enum FieldType {
5557
INT32(15, Integer.class), //
5658
TIMESTAMP(16, BSONTimestamp.class), //
5759
INT64(17, Long.class), //
58-
DECIMAL128(18, Decimal128.class);
60+
DECIMAL128(18, Decimal128.class),
61+
VECTOR(5, BinaryVector.class);
5962

6063
private final int bsonType;
6164
private final Class<?> javaClass;

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/util/BsonUtils.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -301,11 +301,19 @@ public static Object toJavaType(BsonValue value) {
301301
case BOOLEAN -> value.asBoolean().getValue();
302302
case OBJECT_ID -> value.asObjectId().getValue();
303303
case DB_POINTER -> new DBRef(value.asDBPointer().getNamespace(), value.asDBPointer().getId());
304-
case BINARY -> value.asBinary().getData();
304+
case BINARY -> {
305+
306+
BsonBinary binary = value.asBinary();
307+
if(binary.getType() != BsonBinarySubType.VECTOR.getValue()) {
308+
yield binary.getData();
309+
}
310+
yield value.asBinary().asVector();
311+
}
305312
case DATE_TIME -> new Date(value.asDateTime().getValue());
306313
case SYMBOL -> value.asSymbol().getSymbol();
307314
case ARRAY -> value.asArray().toArray();
308315
case DOCUMENT -> Document.parse(value.asDocument().toJson());
316+
309317
default -> value;
310318
};
311319
}

0 commit comments

Comments
 (0)