Skip to content

Commit a9c3058

Browse files
mars05sothawo
authored andcommitted
DATAES-639 - Add ignore_above mapping parameter support.
Original pull request: spring-projects#304.
1 parent 2712a1e commit a9c3058

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* @author Jakub Vavrik
3333
* @author Kevin Leturc
3434
* @author Peter-Josef Meisch
35+
* @author Xiao Yu
3536
*/
3637
@Retention(RetentionPolicy.RUNTIME)
3738
@Target(ElementType.FIELD)
@@ -77,4 +78,9 @@
7778
boolean includeInParent() default false;
7879

7980
String[] copyTo() default {};
81+
82+
/**
83+
* @since 4.0
84+
*/
85+
int ignoreAbove() default -1;
8086
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* @author Artur Konczak
2525
* @author Mohsin Husen
2626
* @author Sascha Woo
27+
* @author Xiao Yu
2728
*/
2829
@Retention(RetentionPolicy.RUNTIME)
2930
@Target(ElementType.FIELD)
@@ -48,4 +49,9 @@
4849
String analyzer() default "";
4950

5051
String normalizer() default "";
52+
53+
/**
54+
* @since 4.0
55+
*/
56+
int ignoreAbove() default -1;
5157
}

src/main/java/org/springframework/data/elasticsearch/core/MappingBuilder.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.springframework.data.mapping.PropertyHandler;
4949
import org.springframework.data.util.TypeInformation;
5050
import org.springframework.lang.Nullable;
51+
import org.springframework.util.Assert;
5152
import org.springframework.util.StringUtils;
5253

5354
import com.fasterxml.jackson.databind.JsonNode;
@@ -67,6 +68,7 @@
6768
* @author Robert Gruendler
6869
* @author Petr Kukral
6970
* @author Peter-Josef Meisch
71+
* @author Xiao Yu
7072
*/
7173
class MappingBuilder {
7274

@@ -85,6 +87,7 @@ class MappingBuilder {
8587
private static final String FIELD_CONTEXT_TYPE = "type";
8688
private static final String FIELD_CONTEXT_PRECISION = "precision";
8789
private static final String FIELD_DYNAMIC_TEMPLATES = "dynamic_templates";
90+
private static final String FIELD_IGNORE_ABOVE = "ignore_above";
8891

8992
private static final String COMPLETION_PRESERVE_SEPARATORS = "preserve_separators";
9093
private static final String COMPLETION_PRESERVE_POSITION_INCREMENTS = "preserve_position_increments";
@@ -346,6 +349,7 @@ private void addFieldMappingParameters(XContentBuilder builder, Object annotatio
346349
String searchAnalyzer = null;
347350
String normalizer = null;
348351
String[] copyTo = null;
352+
Integer ignoreAbove = null;
349353

350354
if (annotation instanceof Field) {
351355
// @Field
@@ -360,6 +364,7 @@ private void addFieldMappingParameters(XContentBuilder builder, Object annotatio
360364
searchAnalyzer = fieldAnnotation.searchAnalyzer();
361365
normalizer = fieldAnnotation.normalizer();
362366
copyTo = fieldAnnotation.copyTo();
367+
ignoreAbove = fieldAnnotation.ignoreAbove();
363368
} else if (annotation instanceof InnerField) {
364369
// @InnerField
365370
InnerField fieldAnnotation = (InnerField) annotation;
@@ -372,6 +377,7 @@ private void addFieldMappingParameters(XContentBuilder builder, Object annotatio
372377
analyzer = fieldAnnotation.analyzer();
373378
searchAnalyzer = fieldAnnotation.searchAnalyzer();
374379
normalizer = fieldAnnotation.normalizer();
380+
ignoreAbove = fieldAnnotation.ignoreAbove();
375381
} else {
376382
throw new IllegalArgumentException("annotation must be an instance of @Field or @InnerField");
377383
}
@@ -404,6 +410,10 @@ private void addFieldMappingParameters(XContentBuilder builder, Object annotatio
404410
if (copyTo != null && copyTo.length > 0) {
405411
builder.field(FIELD_COPY_TO, copyTo);
406412
}
413+
if (ignoreAbove != -1) {
414+
Assert.isTrue(ignoreAbove >= 0, "ignore_above must be a positive value");
415+
builder.field(FIELD_IGNORE_ABOVE, ignoreAbove);
416+
}
407417
}
408418

409419
/**

src/test/java/org/springframework/data/elasticsearch/core/MappingBuilderTests.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
* @author Don Wellington
8181
* @author Sascha Woo
8282
* @author Peter-Josef Meisch
83+
* @author Xiao Yu
8384
*/
8485
@RunWith(SpringRunner.class)
8586
@ContextConfiguration("classpath:elasticsearch-template-test.xml")
@@ -409,6 +410,35 @@ public void shouldUseFieldNameOnMultiField() throws IOException, JSONException {
409410
assertEquals(expected, mapping, false);
410411
}
411412

413+
@Test // DATAES-639
414+
public void shouldUseIgnoreAbove() throws IOException, JSONException {
415+
416+
// given
417+
String expected = "{\"ignore-above-type\":{\"properties\":{\"message\":{\"store\":false,\"type\":\"keyword\",\"ignore_above\":10}}}}";
418+
419+
// when
420+
String mapping = getMappingBuilder().buildPropertyMapping(IgnoreAboveEntity.class);
421+
422+
// then
423+
assertEquals(expected, mapping, false);
424+
}
425+
426+
/**
427+
* @author Xiao Yu
428+
*/
429+
@Setter
430+
@Getter
431+
@NoArgsConstructor
432+
@AllArgsConstructor
433+
@Builder
434+
@Document(indexName = "ignore-above-index", type = "ignore-above-type")
435+
static class IgnoreAboveEntity {
436+
437+
@Id private String id;
438+
439+
@Field(type = FieldType.Keyword, ignoreAbove = 10) private String message;
440+
}
441+
412442
/**
413443
* @author Peter-Josef Meisch
414444
*/

0 commit comments

Comments
 (0)