Skip to content

Commit 73d5d62

Browse files
authored
Enable custom similarity value.
Original Pull Request spring-projects#2429 Closes spring-projects#2424
1 parent 5a36f5e commit 73d5d62

File tree

5 files changed

+17
-21
lines changed

5 files changed

+17
-21
lines changed

src/main/asciidoc/reference/elasticsearch-migration-guide-5.0-5.1.adoc

+7-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ This section describes breaking changes from version 5.0.x to 5.1.x and how remo
66
[[elasticsearch-migration-guide-5.0-5.1.breaking-changes]]
77
== Breaking Changes
88

9-
In the `org.springframework.data.elasticsearch.core.index.AliasData` class, which is used for alias information
10-
returned from Elasticsearch, the property `filter` (of type `Document`) is replaced by `filterQuery` which is of type
9+
In the `org.springframework.data.elasticsearch.core.index.AliasData` class, which is used for alias information returned from Elasticsearch, the property `filter` (of type `Document`) is replaced by `filterQuery` which is of type
1110
`org.springframework.data.elasticsearch.core.query.Query`.
11+
12+
`org.springframework.data.elasticsearch.annotations.Similarity` was an enum class until 5.1. This enum was used in the `@Field` annotation to specify a similarity value.
13+
But besides the values defined by the enum, it is possible to have similarities with custom names in Elasticsearch.
14+
Therefore, the annotation property was changed from the type of the enum to a simple `String`.
15+
The previous enum values like `Similarity.Default` do still exist as String constants, so existing code will compile unmodified.
16+
Adaptions are necessary when this enum was used at other places than as a property of the `@Field` annotation.

src/main/java/org/springframework/data/elasticsearch/annotations/Field.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@
140140
/**
141141
* @since 4.0
142142
*/
143-
Similarity similarity() default Similarity.Default;
143+
String similarity() default Similarity.Default;
144144

145145
/**
146146
* @since 4.0

src/main/java/org/springframework/data/elasticsearch/annotations/InnerField.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
/**
110110
* @since 4.0
111111
*/
112-
Similarity similarity() default Similarity.Default;
112+
String similarity() default Similarity.Default;
113113

114114
/**
115115
* @since 4.0

src/main/java/org/springframework/data/elasticsearch/annotations/Similarity.java

+5-14
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,9 @@
1919
* @author Peter-Josef Meisch
2020
* @since 4.0
2121
*/
22-
public enum Similarity {
23-
Default("default"), BM25("BM25"), classic("classic"), Boolean("boolean");
24-
25-
// need to use a custom name because 'boolean' can't be used as enum name
26-
private final String toStringName;
27-
28-
Similarity(String name) {
29-
this.toStringName = name;
30-
}
31-
32-
@Override
33-
public String toString() {
34-
return toStringName;
35-
}
22+
public final class Similarity {
23+
public final static String Default = "default";
24+
public final static String BM25 = "BM25";
25+
public final static String classic = "classic";
26+
public final static String Boolean = "boolean";
3627
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public final class MappingParameters {
110110
private final Integer dims;
111111
private final String searchAnalyzer;
112112
private final double scalingFactor;
113-
private final Similarity similarity;
113+
private final String similarity;
114114
private final boolean store;
115115
private final TermVector termVector;
116116
private final FieldType type;
@@ -330,8 +330,8 @@ public void writeTypeAndParametersTo(ObjectNode objectNode) throws IOException {
330330
objectNode.put(FIELD_PARAM_POSITION_INCREMENT_GAP, positionIncrementGap);
331331
}
332332

333-
if (similarity != Similarity.Default) {
334-
objectNode.put(FIELD_PARAM_SIMILARITY, similarity.toString());
333+
if (!Similarity.Default.equals(similarity)) {
334+
objectNode.put(FIELD_PARAM_SIMILARITY, similarity);
335335
}
336336

337337
if (termVector != TermVector.none) {

0 commit comments

Comments
 (0)