Skip to content

Commit 315bf7c

Browse files
committed
Fix similarity field mapping.
Closes spring-projects#2659
1 parent a7185b1 commit 315bf7c

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/main/java/org/springframework/data/elasticsearch/core/index/MappingParameters.java

+2
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,8 @@ public void writeTypeAndParametersTo(ObjectNode objectNode) throws IOException {
332332

333333
if (!Similarity.Default.equals(similarity)) {
334334
objectNode.put(FIELD_PARAM_SIMILARITY, similarity);
335+
// similarity must have index explicitly set, otherwise Elasticsearch returns an error
336+
objectNode.put(FIELD_PARAM_INDEX, index);
335337
}
336338

337339
if (termVector != TermVector.none) {

src/test/java/org/springframework/data/elasticsearch/core/index/MappingBuilderIntegrationTests.java

+15
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,12 @@ void shouldWriteMappingWithFieldNameWithDots() {
263263
indexOps.createWithMapping();
264264
}
265265

266+
@Test // #2659
267+
@DisplayName("should write correct mapping for dense vector property")
268+
void shouldWriteCorrectMappingForDenseVectorProperty() {
269+
operations.indexOps(SimilarityEntity.class).createWithMapping();
270+
}
271+
266272
// region Entities
267273
@Document(indexName = "#{@indexNameProvider.indexName()}")
268274
static class Book {
@@ -893,5 +899,14 @@ private static class FieldNameDotsEntity {
893899
@Nullable
894900
@Field(name = "dotted.field", type = Text) private String dottedField;
895901
}
902+
903+
@Document(indexName = "#{@indexNameProvider.indexName()}")
904+
static class SimilarityEntity {
905+
@Nullable
906+
@Id private String id;
907+
908+
@Field(type = FieldType.Dense_Vector, dims = 42, similarity = "cosine") private double[] denseVector;
909+
}
910+
896911
// endregion
897912
}

0 commit comments

Comments
 (0)