Skip to content

Commit 2cfea9e

Browse files
committed
Speedup field exists check
1 parent 505f21b commit 2cfea9e

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/stats/SearchContextStats.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,6 @@ private SearchContextStats(List<SearchExecutionContext> contexts) {
8080
assert contexts != null && contexts.isEmpty() == false;
8181
}
8282

83-
public boolean exists(String field) {
84-
var stat = cache.computeIfAbsent(field, this::makeFieldStats);
85-
return stat.config.exists;
86-
}
87-
8883
private FieldStats makeFieldStats(String field) {
8984
var stat = new FieldStats();
9085
stat.config = makeFieldConfig(field);
@@ -123,19 +118,30 @@ private FieldConfig makeFieldConfig(String field) {
123118
}
124119
}
125120

121+
private boolean fastNoCacheFieldExists(String field) {
122+
for (SearchExecutionContext context : contexts) {
123+
if (context.isFieldMapped(field)) {
124+
return true;
125+
}
126+
}
127+
return false;
128+
}
129+
130+
public boolean exists(String field) {
131+
var stat = cache.get(field);
132+
return stat != null ? stat.config.exists : fastNoCacheFieldExists(field);
133+
}
134+
126135
public boolean isIndexed(String field) {
127-
var stat = cache.computeIfAbsent(field, this::makeFieldStats);
128-
return stat.config.indexed;
136+
return cache.computeIfAbsent(field, this::makeFieldStats).config.indexed;
129137
}
130138

131139
public boolean hasDocValues(String field) {
132-
var stat = cache.computeIfAbsent(field, this::makeFieldStats);
133-
return stat.config.hasDocValues;
140+
return cache.computeIfAbsent(field, this::makeFieldStats).config.hasDocValues;
134141
}
135142

136143
public boolean hasExactSubfield(String field) {
137-
var stat = cache.computeIfAbsent(field, this::makeFieldStats);
138-
return stat.config.hasExactSubfield;
144+
return cache.computeIfAbsent(field, this::makeFieldStats).config.hasExactSubfield;
139145
}
140146

141147
public long count() {
@@ -218,7 +224,7 @@ public boolean isSingleValue(String field) {
218224
var stat = cache.computeIfAbsent(field, this::makeFieldStats);
219225
if (stat.singleValue == null) {
220226
// there's no such field so no need to worry about multi-value fields
221-
if (exists(field) == false) {
227+
if (stat.config.exists == false) {
222228
stat.singleValue = true;
223229
} else {
224230
// fields are MV per default

0 commit comments

Comments
 (0)