From 1661ce5b8c665b915e71c5488243f13176444d29 Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Wed, 18 Nov 2015 18:21:11 +0100 Subject: [PATCH] field stats: Fix NPE when index constraint has been set on mapped field but the index is empty --- .../TransportFieldStatsTransportAction.java | 4 ++++ .../fieldstats/FieldStatsTests.java | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/core/src/main/java/org/elasticsearch/action/fieldstats/TransportFieldStatsTransportAction.java b/core/src/main/java/org/elasticsearch/action/fieldstats/TransportFieldStatsTransportAction.java index f92571a53df..4607560ccb9 100644 --- a/core/src/main/java/org/elasticsearch/action/fieldstats/TransportFieldStatsTransportAction.java +++ b/core/src/main/java/org/elasticsearch/action/fieldstats/TransportFieldStatsTransportAction.java @@ -119,6 +119,10 @@ public class TransportFieldStatsTransportAction extends TransportBroadcastAction while (iterator.hasNext()) { Map.Entry> 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 diff --git a/core/src/test/java/org/elasticsearch/fieldstats/FieldStatsTests.java b/core/src/test/java/org/elasticsearch/fieldstats/FieldStatsTests.java index 0f36bdcfcc1..713f3f107a3 100644 --- a/core/src/test/java/org/elasticsearch/fieldstats/FieldStatsTests.java +++ b/core/src/test/java/org/elasticsearch/fieldstats/FieldStatsTests.java @@ -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)); + } + } \ No newline at end of file