field stats: Fix NPE when index constraint has been set on mapped field but the index is empty

This commit is contained in:
Martijn van Groningen 2015-11-18 18:21:11 +01:00
parent 3cacec4468
commit 1661ce5b8c
2 changed files with 22 additions and 0 deletions

View File

@ -119,6 +119,10 @@ public class TransportFieldStatsTransportAction extends TransportBroadcastAction
while (iterator.hasNext()) {
Map.Entry<String, Map<String, FieldStats>> entry = iterator.next();
FieldStats indexConstraintFieldStats = entry.getValue().get(indexConstraint.getField());
if (indexConstraintFieldStats == null) {
continue;
}
if (indexConstraintFieldStats.match(indexConstraint)) {
// If the field stats didn't occur in the list of fields in the original request we need to remove the
// field stats, because it was never requested and was only needed to validate the index constraint

View File

@ -390,4 +390,22 @@ public class FieldStatsTests extends ESSingleNodeTestCase {
}
}
public void testEmptyIndex() {
createIndex("test1", Settings.EMPTY, "type", "value", "type=date");
FieldStatsResponse response = client().prepareFieldStats()
.setFields("value")
.setLevel("indices")
.get();
assertThat(response.getIndicesMergedFieldStats().size(), equalTo(1));
assertThat(response.getIndicesMergedFieldStats().get("test1").size(), equalTo(0));
response = client().prepareFieldStats()
.setFields("value")
.setIndexContraints(new IndexConstraint("value", MIN, GTE, "1998-01-01T00:00:00.000Z"))
.setLevel("indices")
.get();
assertThat(response.getIndicesMergedFieldStats().size(), equalTo(1));
assertThat(response.getIndicesMergedFieldStats().get("test1").size(), equalTo(0));
}
}