Skip to content

Commit 54240d3

Browse files
authored
Refactor IndexFieldCapabilities creation by adding a proper builder object (#125219)
Reduce boilerplate associated with creating `IndexFieldCapabilities` instances. Since it's a class with a large number of fields, it makes sense to define a builder object, as that can also help with all the Boolean and null blindness going on. As with `FieldCapabilitiesBuilder`, this is only used in tests, to avoid any potential performance hit in production code.
1 parent 5290e0c commit 54240d3

File tree

6 files changed

+83
-10
lines changed

6 files changed

+83
-10
lines changed

server/src/test/java/org/elasticsearch/action/fieldcaps/ResponseRewriterTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import org.elasticsearch.common.Strings;
1414
import org.elasticsearch.test.ESTestCase;
1515

16-
import java.util.Collections;
1716
import java.util.Map;
1817

1918
public class ResponseRewriterTests extends ESTestCase {
@@ -147,7 +146,7 @@ public void testAllowedTypes() {
147146
}
148147

149148
private static IndexFieldCapabilities fieldCaps(String name, String type, boolean isMetadata) {
150-
return new IndexFieldCapabilities(name, type, isMetadata, true, true, false, null, Collections.emptyMap());
149+
return new IndexFieldCapabilitiesBuilder(name, type).isMetadataField(isMetadata).build();
151150
}
152151

153152
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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.action.fieldcaps;
11+
12+
import org.elasticsearch.core.Nullable;
13+
import org.elasticsearch.index.mapper.TimeSeriesParams;
14+
15+
import java.util.Collections;
16+
import java.util.Map;
17+
import java.util.TreeMap;
18+
19+
public class IndexFieldCapabilitiesBuilder {
20+
private final String name;
21+
private final String type;
22+
23+
private boolean isMetadataField;
24+
private boolean isSearchable;
25+
private boolean isAggregatable;
26+
private boolean isDimension;
27+
private @Nullable TimeSeriesParams.MetricType metricType;
28+
private Map<String, String> meta;
29+
30+
public IndexFieldCapabilitiesBuilder(String name, String type) {
31+
this.name = name;
32+
this.type = type;
33+
34+
this.isSearchable = true;
35+
this.isAggregatable = true;
36+
37+
this.meta = Collections.emptyMap();
38+
}
39+
40+
public IndexFieldCapabilitiesBuilder isMetadataField(boolean isMetadataField) {
41+
this.isMetadataField = isMetadataField;
42+
return this;
43+
}
44+
45+
public IndexFieldCapabilitiesBuilder isSearchable(boolean isSearchable) {
46+
this.isSearchable = isSearchable;
47+
return this;
48+
}
49+
50+
public IndexFieldCapabilitiesBuilder isAggregatable(boolean isAggregatable) {
51+
this.isAggregatable = isAggregatable;
52+
return this;
53+
}
54+
55+
public IndexFieldCapabilitiesBuilder isDimension(boolean isDimension) {
56+
this.isDimension = isDimension;
57+
return this;
58+
}
59+
60+
public IndexFieldCapabilitiesBuilder metricType(TimeSeriesParams.MetricType metricType) {
61+
this.metricType = metricType;
62+
return this;
63+
}
64+
65+
public IndexFieldCapabilitiesBuilder meta(@Nullable Map<String, String> meta) {
66+
this.meta = meta != null ? new TreeMap<>(meta) : null;
67+
return this;
68+
}
69+
70+
public IndexFieldCapabilities build() {
71+
return new IndexFieldCapabilities(name, type, isMetadataField, isSearchable, isAggregatable, isDimension, metricType, meta);
72+
}
73+
}

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/analysis/AnalyzerTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.elasticsearch.action.fieldcaps.FieldCapabilitiesIndexResponse;
1212
import org.elasticsearch.action.fieldcaps.FieldCapabilitiesResponse;
1313
import org.elasticsearch.action.fieldcaps.IndexFieldCapabilities;
14+
import org.elasticsearch.action.fieldcaps.IndexFieldCapabilitiesBuilder;
1415
import org.elasticsearch.common.settings.Settings;
1516
import org.elasticsearch.index.IndexMode;
1617
import org.elasticsearch.index.analysis.IndexAnalyzers;
@@ -3186,7 +3187,6 @@ public void testRrfError() {
31863187
assertThat(e.getMessage(), containsString("Unknown column [_id]"));
31873188
}
31883189

3189-
// TODO There's too much boilerplate involved here! We need a better way of creating FieldCapabilitiesResponses from a mapping or index.
31903190
private static FieldCapabilitiesIndexResponse fieldCapabilitiesIndexResponse(
31913191
String indexName,
31923192
Map<String, IndexFieldCapabilities> fields
@@ -3195,7 +3195,7 @@ private static FieldCapabilitiesIndexResponse fieldCapabilitiesIndexResponse(
31953195
}
31963196

31973197
private static Map<String, IndexFieldCapabilities> messageResponseMap(String date) {
3198-
return Map.of("message", new IndexFieldCapabilities("message", date, false, true, true, false, null, null));
3198+
return Map.of("message", new IndexFieldCapabilitiesBuilder("message", date).build());
31993199
}
32003200

32013201
private void verifyUnsupported(String query, String errorMessage) {

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/enrich/EnrichPolicyResolverTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.elasticsearch.action.fieldcaps.FieldCapabilitiesRequest;
1818
import org.elasticsearch.action.fieldcaps.FieldCapabilitiesResponse;
1919
import org.elasticsearch.action.fieldcaps.IndexFieldCapabilities;
20+
import org.elasticsearch.action.fieldcaps.IndexFieldCapabilitiesBuilder;
2021
import org.elasticsearch.action.support.PlainActionFuture;
2122
import org.elasticsearch.client.internal.FilterClient;
2223
import org.elasticsearch.cluster.ClusterName;
@@ -489,7 +490,7 @@ protected <Request extends ActionRequest, Response extends ActionResponse> void
489490
if (mapping != null) {
490491
Map<String, IndexFieldCapabilities> fieldCaps = new HashMap<>();
491492
for (Map.Entry<String, String> e : mapping.entrySet()) {
492-
var f = new IndexFieldCapabilities(e.getKey(), e.getValue(), false, false, false, false, null, Map.of());
493+
var f = new IndexFieldCapabilitiesBuilder(e.getKey(), e.getValue()).isSearchable(false).isAggregatable(false).build();
493494
fieldCaps.put(e.getKey(), f);
494495
}
495496
var indexResponse = new FieldCapabilitiesIndexResponse(alias, null, fieldCaps, true, IndexMode.STANDARD);

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/telemetry/PlanExecutorMetricsTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import org.elasticsearch.action.fieldcaps.FieldCapabilitiesBuilder;
1414
import org.elasticsearch.action.fieldcaps.FieldCapabilitiesIndexResponse;
1515
import org.elasticsearch.action.fieldcaps.FieldCapabilitiesResponse;
16-
import org.elasticsearch.action.fieldcaps.IndexFieldCapabilities;
16+
import org.elasticsearch.action.fieldcaps.IndexFieldCapabilitiesBuilder;
1717
import org.elasticsearch.action.support.IndicesOptions;
1818
import org.elasticsearch.client.internal.Client;
1919
import org.elasticsearch.common.settings.ClusterSettings;
@@ -228,8 +228,8 @@ private List<FieldCapabilitiesIndexResponse> indexFieldCapabilities(String[] ind
228228
idx,
229229
idx,
230230
Map.ofEntries(
231-
Map.entry("foo", new IndexFieldCapabilities("foo", "integer", false, true, true, false, null, Map.of())),
232-
Map.entry("bar", new IndexFieldCapabilities("bar", "long", false, true, true, false, null, Map.of()))
231+
Map.entry("foo", new IndexFieldCapabilitiesBuilder("foo", "integer").build()),
232+
Map.entry("bar", new IndexFieldCapabilitiesBuilder("bar", "long").build())
233233
),
234234
true,
235235
IndexMode.STANDARD

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/type/EsqlDataTypeRegistryTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import org.elasticsearch.action.fieldcaps.FieldCapabilitiesIndexResponse;
1010
import org.elasticsearch.action.fieldcaps.FieldCapabilitiesResponse;
11-
import org.elasticsearch.action.fieldcaps.IndexFieldCapabilities;
11+
import org.elasticsearch.action.fieldcaps.IndexFieldCapabilitiesBuilder;
1212
import org.elasticsearch.index.IndexMode;
1313
import org.elasticsearch.index.mapper.TimeSeriesParams;
1414
import org.elasticsearch.test.ESTestCase;
@@ -46,7 +46,7 @@ private void resolve(String esTypeName, TimeSeriesParams.MetricType metricType,
4646
new FieldCapabilitiesIndexResponse(
4747
idx,
4848
idx,
49-
Map.of(field, new IndexFieldCapabilities(field, esTypeName, false, true, true, false, metricType, Map.of())),
49+
Map.of(field, new IndexFieldCapabilitiesBuilder(field, esTypeName).metricType(metricType).build()),
5050
true,
5151
IndexMode.TIME_SERIES
5252
)

0 commit comments

Comments
 (0)