Skip to content

Commit b08e96e

Browse files
marko-bekhtayrodiere
authored andcommitted
HSEARCH-5021 Make dimension "optional" in IndexFieldTypeFactory
1 parent 9b7b0fb commit b08e96e

File tree

23 files changed

+239
-78
lines changed

23 files changed

+239
-78
lines changed

backend/elasticsearch/src/main/java/org/hibernate/search/backend/elasticsearch/types/dsl/impl/ElasticsearchIndexFieldTypeFactoryImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,17 +236,17 @@ public ScaledNumberIndexFieldTypeOptionsStep<?, BigInteger> asBigInteger() {
236236
}
237237

238238
@Override
239-
public <F> VectorFieldTypeOptionsStep<?, F> asVector(int dimension, Class<F> valueType) {
239+
public <F> VectorFieldTypeOptionsStep<?, F> asVector(Class<F> valueType) {
240240
throw new UnsupportedOperationException( "The Elasticsearch backend does not support vector field yet." );
241241
}
242242

243243
@Override
244-
public VectorFieldTypeOptionsStep<?, byte[]> asByteVector(int dimension) {
244+
public VectorFieldTypeOptionsStep<?, byte[]> asByteVector() {
245245
throw new UnsupportedOperationException( "The Elasticsearch backend does not support vector field yet." );
246246
}
247247

248248
@Override
249-
public VectorFieldTypeOptionsStep<?, float[]> asFloatVector(int dimension) {
249+
public VectorFieldTypeOptionsStep<?, float[]> asFloatVector() {
250250
throw new UnsupportedOperationException( "The Elasticsearch backend does not support vector field yet." );
251251
}
252252

backend/lucene/src/main/java/org/hibernate/search/backend/lucene/logging/impl/Log.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,4 +727,8 @@ SearchException vectorKnnMatchVectorTypeDiffersFromField(String absoluteFieldPat
727727
+ " Matching against an array with length of '%3$s' is unsupported."
728728
+ " Use the array of the same size as the vector field.")
729729
SearchException vectorKnnMatchVectorDimensionDiffersFromField(String absoluteFieldPath, int expected, int actual);
730+
731+
@Message(id = ID_OFFSET + 179, value = "Invalid index field type: missing vector dimension."
732+
+ " Define the vector dimension explicitly. %1$s")
733+
SearchException nullVectorDimension(String hint, @Param EventContext eventContext);
730734
}

backend/lucene/src/main/java/org/hibernate/search/backend/lucene/types/dsl/impl/AbstractLuceneVectorFieldTypeOptionsStep.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,15 @@ abstract class AbstractLuceneVectorFieldTypeOptionsStep<S extends AbstractLucene
4343
private static final int MAX_MAX_CONNECTIONS = 512;
4444

4545
protected VectorSimilarity vectorSimilarity = VectorSimilarity.DEFAULT;
46-
protected final int dimension;
46+
protected Integer dimension;
4747
protected int beamWidth = MAX_MAX_CONNECTIONS;
4848
protected int maxConnections = 16;
4949
private Projectable projectable = Projectable.DEFAULT;
5050
private Searchable searchable = Searchable.DEFAULT;
5151
private F indexNullAsValue = null;
5252

53-
AbstractLuceneVectorFieldTypeOptionsStep(LuceneIndexFieldTypeBuildContext buildContext, Class<F> valueType, int dimension) {
53+
AbstractLuceneVectorFieldTypeOptionsStep(LuceneIndexFieldTypeBuildContext buildContext, Class<F> valueType) {
5454
super( buildContext, valueType );
55-
if ( dimension < 1 || dimension > DEFAULT_MAX_DIMENSIONS ) {
56-
throw log.vectorPropertyUnsupportedValue( "dimension", dimension, DEFAULT_MAX_DIMENSIONS );
57-
}
58-
this.dimension = dimension;
5955
}
6056

6157
@Override
@@ -94,6 +90,15 @@ public S maxConnections(int maxConnections) {
9490
return thisAsS();
9591
}
9692

93+
@Override
94+
public S dimension(int dimension) {
95+
if ( dimension < 1 || dimension > DEFAULT_MAX_DIMENSIONS ) {
96+
throw log.vectorPropertyUnsupportedValue( "dimension", dimension, DEFAULT_MAX_DIMENSIONS );
97+
}
98+
this.dimension = dimension;
99+
return thisAsS();
100+
}
101+
97102
@Override
98103
public S indexNullAs(F indexNullAsValue) {
99104
this.indexNullAsValue = indexNullAsValue;
@@ -102,6 +107,9 @@ public S indexNullAs(F indexNullAsValue) {
102107

103108
@Override
104109
public LuceneIndexValueFieldType<F> toIndexFieldType() {
110+
if ( dimension == null ) {
111+
throw log.nullVectorDimension( buildContext.hints().missingVectorDimension(), buildContext.getEventContext() );
112+
}
105113
VectorSimilarityFunction resolvedVectorSimilarity = resolveDefault( vectorSimilarity );
106114
boolean resolvedProjectable = resolveDefault( projectable );
107115
boolean resolvedSearchable = resolveDefault( searchable );

backend/lucene/src/main/java/org/hibernate/search/backend/lucene/types/dsl/impl/LuceneByteVectorFieldTypeOptionsStep.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
class LuceneByteVectorFieldTypeOptionsStep
1818
extends AbstractLuceneVectorFieldTypeOptionsStep<LuceneByteVectorFieldTypeOptionsStep, byte[]> {
1919

20-
LuceneByteVectorFieldTypeOptionsStep(LuceneIndexFieldTypeBuildContext buildContext, int dimension) {
21-
super( buildContext, byte[].class, dimension );
20+
LuceneByteVectorFieldTypeOptionsStep(LuceneIndexFieldTypeBuildContext buildContext) {
21+
super( buildContext, byte[].class );
2222
}
2323

2424
@Override

backend/lucene/src/main/java/org/hibernate/search/backend/lucene/types/dsl/impl/LuceneFloatVectorFieldTypeOptionsStep.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
class LuceneFloatVectorFieldTypeOptionsStep
1818
extends AbstractLuceneVectorFieldTypeOptionsStep<LuceneFloatVectorFieldTypeOptionsStep, float[]> {
1919

20-
LuceneFloatVectorFieldTypeOptionsStep(LuceneIndexFieldTypeBuildContext buildContext, int dimension) {
21-
super( buildContext, float[].class, dimension );
20+
LuceneFloatVectorFieldTypeOptionsStep(LuceneIndexFieldTypeBuildContext buildContext) {
21+
super( buildContext, float[].class );
2222
}
2323

2424
@Override

backend/lucene/src/main/java/org/hibernate/search/backend/lucene/types/dsl/impl/LuceneIndexFieldTypeFactoryImpl.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,12 @@ else if ( BigInteger.class.equals( valueType ) ) {
128128
}
129129

130130
@SuppressWarnings("unchecked")
131-
public <F> VectorFieldTypeOptionsStep<?, F> asVector(int dimension, Class<F> valueType) {
131+
public <F> VectorFieldTypeOptionsStep<?, F> asVector(Class<F> valueType) {
132132
if ( byte[].class.equals( valueType ) ) {
133-
return (VectorFieldTypeOptionsStep<?, F>) asByteVector( dimension );
133+
return (VectorFieldTypeOptionsStep<?, F>) asByteVector();
134134
}
135135
else if ( float[].class.equals( valueType ) ) {
136-
return (VectorFieldTypeOptionsStep<?, F>) asFloatVector( dimension );
136+
return (VectorFieldTypeOptionsStep<?, F>) asFloatVector();
137137
}
138138
else {
139139
throw log.cannotGuessVectorFieldType( valueType, getEventContext() );
@@ -246,13 +246,13 @@ public ScaledNumberIndexFieldTypeOptionsStep<?, BigInteger> asBigInteger() {
246246
}
247247

248248
@Override
249-
public VectorFieldTypeOptionsStep<?, byte[]> asByteVector(int dimension) {
250-
return new LuceneByteVectorFieldTypeOptionsStep( this, dimension );
249+
public VectorFieldTypeOptionsStep<?, byte[]> asByteVector() {
250+
return new LuceneByteVectorFieldTypeOptionsStep( this );
251251
}
252252

253253
@Override
254-
public VectorFieldTypeOptionsStep<?, float[]> asFloatVector(int dimension) {
255-
return new LuceneFloatVectorFieldTypeOptionsStep( this, dimension );
254+
public VectorFieldTypeOptionsStep<?, float[]> asFloatVector() {
255+
return new LuceneFloatVectorFieldTypeOptionsStep( this );
256256
}
257257

258258
@Override

engine/src/main/java/org/hibernate/search/engine/backend/reporting/spi/BackendMappingHints.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,7 @@ public interface BackendMappingHints {
2323
@Message("")
2424
String missingDecimalScale();
2525

26+
@Message("")
27+
String missingVectorDimension();
28+
2629
}

engine/src/main/java/org/hibernate/search/engine/backend/types/dsl/IndexFieldTypeFactory.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ public interface IndexFieldTypeFactory {
4444
/**
4545
* Define a vector field type whose values are represented as a given type in Hibernate Search.
4646
* <p>
47-
* When possible, prefer the other methods such as {@link #asByteVector(int)} or {@link #asFloatVector(int)}
47+
* When possible, prefer the other methods such as {@link #asByteVector()} or {@link #asFloatVector()}
4848
* to avoid unnecessary type checks.
4949
*
5050
* @param <F> The type of values for this field type.
51-
* @param dimension The number of dimensions (array length) of vectors to be indexed.
5251
* @param valueType The type of values for this field type. Should be an array type like {@code byte[]} or {@code float[]}.
5352
* @return A DSL step where the index vector field type can be defined in more details.
5453
*/
55-
<F> VectorFieldTypeOptionsStep<?, F> asVector(int dimension, Class<F> valueType);
54+
@Incubating
55+
<F> VectorFieldTypeOptionsStep<?, F> asVector(Class<F> valueType);
5656

5757
/**
5858
* Define a field type whose values are represented as a {@link String} in Hibernate Search.
@@ -180,26 +180,23 @@ public interface IndexFieldTypeFactory {
180180
*/
181181
ScaledNumberIndexFieldTypeOptionsStep<?, BigInteger> asBigInteger();
182182

183-
184183
/**
185184
* Define a field type intended for use in vector search
186185
* and whose values are represented as a {@code byte[]} in Hibernate Search.
187186
*
188-
* @param dimension The number of dimensions (array length) of vectors to be indexed.
189187
* @return A DSL step where the index field type can be defined in more details.
190188
*/
191189
@Incubating
192-
VectorFieldTypeOptionsStep<?, byte[]> asByteVector(int dimension);
190+
VectorFieldTypeOptionsStep<?, byte[]> asByteVector();
193191

194192
/**
195193
* Define a field type intended for use in vector search
196194
* and whose values are represented as a {@code float[]} in Hibernate Search.
197195
*
198-
* @param dimension The number of dimensions (array length) of vectors to be indexed.
199196
* @return A DSL step where the index field type can be defined in more details.
200197
*/
201198
@Incubating
202-
VectorFieldTypeOptionsStep<?, float[]> asFloatVector(int dimension);
199+
VectorFieldTypeOptionsStep<?, float[]> asFloatVector();
203200

204201
/**
205202
* Extend the current factory with the given extension,

engine/src/main/java/org/hibernate/search/engine/backend/types/dsl/VectorFieldTypeOptionsStep.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,10 @@ public interface VectorFieldTypeOptionsStep<S extends VectorFieldTypeOptionsStep
3737
*/
3838
S maxConnections(int maxConnections);
3939

40+
/**
41+
* @param dimension The number of dimensions (array length) of vectors to be indexed.
42+
* @return {@code this}, for method chaining.
43+
*/
44+
S dimension(int dimension);
45+
4046
}

integrationtest/backend/lucene/src/test/java/org/hibernate/search/integrationtest/backend/lucene/lowlevel/reader/LuceneIndexReaderCodecLoadingIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ private static class IndexBinding {
8989
final IndexFieldReference<byte[]> vectorField;
9090

9191
IndexBinding(IndexSchemaElement root) {
92-
vectorField = root.field( "vector", c -> c.asByteVector( 2 ).maxConnections( 10 ) ).toReference();
92+
vectorField = root.field( "vector", c -> c.asByteVector().dimension( 2 ).maxConnections( 10 ) ).toReference();
9393
}
9494
}
9595
}

0 commit comments

Comments
 (0)