Skip to content

Commit ec8401a

Browse files
committed
Fix similarity field mapping.
Original Pull Request #2666 Closes #2659 (cherry picked from commit 8c5ff92) (cherry picked from commit 9b9136d) # Conflicts: # src/main/java/org/springframework/data/elasticsearch/core/index/MappingParameters.java # src/test/java/org/springframework/data/elasticsearch/core/index/MappingBuilderIntegrationTests.java
1 parent 19ec05d commit ec8401a

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 != Similarity.Default) {
334334
objectNode.put(FIELD_PARAM_SIMILARITY, similarity.toString());
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
@@ -279,6 +279,12 @@ void shouldWriteSourceExcludes() {
279279
indexOps.createWithMapping();
280280
}
281281

282+
@Test // #2659
283+
@DisplayName("should write correct mapping for dense vector property")
284+
void shouldWriteCorrectMappingForDenseVectorProperty() {
285+
operations.indexOps(SimilarityEntity.class).createWithMapping();
286+
}
287+
282288
// region Entities
283289
@Document(indexName = "#{@indexNameProvider.indexName()}")
284290
static class Book {
@@ -901,5 +907,14 @@ private static class EntityWithAllTypes {
901907
@Nullable
902908
@Field(type = FieldType.Dense_Vector, dims = 1) String denseVectorField;
903909
}
910+
911+
@Document(indexName = "#{@indexNameProvider.indexName()}")
912+
static class SimilarityEntity {
913+
@Nullable
914+
@Id private String id;
915+
916+
@Field(type = FieldType.Dense_Vector, dims = 42, similarity = "cosine") private double[] denseVector;
917+
}
918+
904919
// endregion
905920
}

0 commit comments

Comments
 (0)