Skip to content

Commit 9b9136d

Browse files
committed
Fix similarity field mapping.
Original Pull Request #2666 Closes #2659 (cherry picked from commit 8c5ff92)
1 parent a266d7c commit 9b9136d

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
@@ -287,6 +287,12 @@ void shouldWriteMappingWithFieldNameWithDots() {
287287
indexOps.createWithMapping();
288288
}
289289

290+
@Test // #2659
291+
@DisplayName("should write correct mapping for dense vector property")
292+
void shouldWriteCorrectMappingForDenseVectorProperty() {
293+
operations.indexOps(SimilarityEntity.class).createWithMapping();
294+
}
295+
290296
// region Entities
291297
@Document(indexName = "#{@indexNameProvider.indexName()}")
292298
static class Book {
@@ -916,5 +922,14 @@ private static class FieldNameDotsEntity {
916922
@Nullable
917923
@Field(name = "dotted.field", type = Text) private String dottedField;
918924
}
925+
926+
@Document(indexName = "#{@indexNameProvider.indexName()}")
927+
static class SimilarityEntity {
928+
@Nullable
929+
@Id private String id;
930+
931+
@Field(type = FieldType.Dense_Vector, dims = 42, similarity = "cosine") private double[] denseVector;
932+
}
933+
919934
// endregion
920935
}

0 commit comments

Comments
 (0)