Skip to content

Commit b07ddf4

Browse files
committed
Added IgnoreAboveTest
1 parent 24c6e3b commit b07ddf4

File tree

4 files changed

+105
-5
lines changed

4 files changed

+105
-5
lines changed

server/src/main/java/org/elasticsearch/index/IgnoreAbove.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,15 @@
1111

1212
import org.elasticsearch.xcontent.XContentString;
1313

14+
import java.util.Objects;
15+
1416
import static org.elasticsearch.index.IndexSettings.IGNORE_ABOVE_DEFAULT_LOGSDB_INDICES;
1517
import static org.elasticsearch.index.IndexSettings.IGNORE_ABOVE_DEFAULT_STANDARD_INDICES;
1618

17-
public record IgnoreAbove(Integer value, int defaultValue) {
19+
/**
20+
* This class models the ignore_above parameter in indices.
21+
*/
22+
public class IgnoreAbove {
1823

1924
public static final IgnoreAbove IGNORE_ABOVE_STANDARD_INDICES = IgnoreAbove.builder()
2025
.defaultValue(IGNORE_ABOVE_DEFAULT_STANDARD_INDICES)
@@ -24,14 +29,29 @@ public record IgnoreAbove(Integer value, int defaultValue) {
2429
.defaultValue(IGNORE_ABOVE_DEFAULT_LOGSDB_INDICES)
2530
.build();
2631

32+
private final Integer value;
33+
private final Integer defaultValue;
34+
35+
public IgnoreAbove(Integer value, Integer defaultValue) {
36+
this.value = value;
37+
this.defaultValue = Objects.requireNonNull(defaultValue);
38+
}
39+
2740
public int get() {
2841
return value != null ? value : defaultValue;
2942
}
3043

44+
/**
45+
* Returns whether ignore_above is set; at field or index level.
46+
*/
3147
public boolean isSet() {
32-
return value != null && value != defaultValue;
48+
// if ignore_above equals default, its not considered to be set, even if it was explicitly set to the default value
49+
return value != null && value.equals(defaultValue) == false;
3350
}
3451

52+
/**
53+
* Returns whether the given string will be ignored.
54+
*/
3555
public boolean isIgnored(final String s) {
3656
if (s == null) return false;
3757
return lengthExceedsIgnoreAbove(s.length());
@@ -53,7 +73,7 @@ public static Builder builder() {
5373
public static final class Builder {
5474

5575
private Integer value;
56-
private int defaultValue; // cannot be null, hence int
76+
private Integer defaultValue;
5777

5878
private Builder() {}
5979

@@ -62,7 +82,7 @@ public Builder value(Integer value) {
6282
return this;
6383
}
6484

65-
public Builder defaultValue(int defaultValue) {
85+
public Builder defaultValue(Integer defaultValue) {
6686
this.defaultValue = defaultValue;
6787
return this;
6888
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.index;
11+
12+
import org.elasticsearch.test.ESTestCase;
13+
import org.elasticsearch.xcontent.Text;
14+
15+
public class IgnoreAboveTests extends ESTestCase {
16+
17+
public void test_ignore_above_with_value_and_default() {
18+
// given
19+
IgnoreAbove ignoreAbove = IgnoreAbove.builder().value(123).defaultValue(456).build();
20+
21+
// when/then
22+
assertEquals(123, ignoreAbove.get());
23+
assertTrue(ignoreAbove.isSet());
24+
}
25+
26+
public void test_ignore_above_with_default_only_is_valid() {
27+
// given
28+
IgnoreAbove ignoreAbove = IgnoreAbove.builder().defaultValue(456).build();
29+
30+
// when/then
31+
assertEquals(456, ignoreAbove.get());
32+
assertFalse(ignoreAbove.isSet());
33+
}
34+
35+
public void test_ignore_above_with_same_value_and_default() {
36+
// given
37+
IgnoreAbove ignoreAbove = IgnoreAbove.builder().value(123).defaultValue(123).build();
38+
39+
// when/then
40+
assertEquals(123, ignoreAbove.get());
41+
assertFalse(ignoreAbove.isSet());
42+
}
43+
44+
public void test_ignore_above_with_value_only_should_throw() {
45+
// given/when/then
46+
assertThrows(NullPointerException.class, () -> IgnoreAbove.builder().value(123).build());
47+
}
48+
49+
public void test_ignore_above_with_nothing_should_throw() {
50+
// given/when/then
51+
assertThrows(NullPointerException.class, () -> IgnoreAbove.builder().build());
52+
}
53+
54+
public void test_string_isIgnored() {
55+
// given
56+
IgnoreAbove ignoreAbove = IgnoreAbove.builder().value(10).defaultValue(456).build();
57+
58+
// when/then
59+
assertFalse(ignoreAbove.isIgnored("potato"));
60+
assertFalse(ignoreAbove.isIgnored("1234567890"));
61+
assertTrue(ignoreAbove.isIgnored("12345678901"));
62+
assertTrue(ignoreAbove.isIgnored("potato potato tomato tomato"));
63+
}
64+
65+
public void test_XContentString_isIgnored() {
66+
// given
67+
IgnoreAbove ignoreAbove = IgnoreAbove.builder().value(10).defaultValue(456).build();
68+
69+
// when/then
70+
assertFalse(ignoreAbove.isIgnored(new Text("potato")));
71+
assertFalse(ignoreAbove.isIgnored(new Text("1234567890")));
72+
assertTrue(ignoreAbove.isIgnored(new Text("12345678901")));
73+
assertTrue(ignoreAbove.isIgnored(new Text("potato potato tomato tomato")));
74+
}
75+
76+
}

server/src/test/java/org/elasticsearch/index/mapper/KeywordFieldTypeTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
package org.elasticsearch.index.mapper;
1010

1111
import com.carrotsearch.randomizedtesting.generators.RandomStrings;
12+
1213
import org.apache.lucene.analysis.Analyzer;
1314
import org.apache.lucene.analysis.LowerCaseFilter;
1415
import org.apache.lucene.analysis.TokenFilter;

x-pack/plugin/wildcard/src/test/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldMapperTests.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,10 @@ protected Object getSampleValueForDocument() {
721721
@Override
722722
protected void registerParameters(ParameterChecker checker) throws IOException {
723723
checker.registerConflictCheck("null_value", b -> b.field("null_value", "foo"));
724-
checker.registerUpdateCheck(b -> b.field("ignore_above", 256), m -> assertEquals(256, ((WildcardFieldMapper) m).ignoreAbove().get()));
724+
checker.registerUpdateCheck(
725+
b -> b.field("ignore_above", 256),
726+
m -> assertEquals(256, ((WildcardFieldMapper) m).ignoreAbove().get())
727+
);
725728

726729
}
727730

0 commit comments

Comments
 (0)