Apache Solr open-source search software
SOLR-3150: NPE when facetting using facet.prefix on an empty field
Yonik Seeleyf50e4dbf5184
diff --git a/solr/core/src/java/org/apache/solr/request/UnInvertedField.java b/solr/core/src/java/org/apache/solr/request/UnInvertedField.javaindex af126949bf5..eeba3ded5a2 100755--- a/solr/core/src/java/org/apache/solr/request/UnInvertedField.java+++ b/solr/core/src/java/org/apache/solr/request/UnInvertedField.java@@ -227,7 +227,7 @@ public class UnInvertedField extends DocTermOrds {int endTerm = numTermsInField; // one past the endTermsEnum te = getOrdTermsEnum(searcher.getAtomicReader());- if (prefix != null && prefix.length() > 0) {+ if (te != null && prefix != null && prefix.length() > 0) {final BytesRef prefixBr = new BytesRef(prefix);if (te.seekCeil(prefixBr, true) == TermsEnum.SeekStatus.END) {startTerm = numTermsInField;diff --git a/solr/core/src/test/org/apache/solr/request/SimpleFacetsTest.java b/solr/core/src/test/org/apache/solr/request/SimpleFacetsTest.javaindex 47f31f6f591..bc2747c5542 100644--- a/solr/core/src/test/org/apache/solr/request/SimpleFacetsTest.java+++ b/solr/core/src/test/org/apache/solr/request/SimpleFacetsTest.java@@ -17,12 +17,17 @@package org.apache.solr.request;+import org.apache.noggit.ObjectBuilder;import org.apache.solr.SolrTestCaseJ4;+import org.apache.solr.common.params.ModifiableSolrParams;+import org.apache.solr.schema.SchemaField;import org.junit.BeforeClass;import org.junit.Test;import java.util.ArrayList;import java.util.Collections;+import java.util.List;+import java.util.Map;public class SimpleFacetsTest extends SolrTestCaseJ4 {@@ -53,7 +58,9 @@ public class SimpleFacetsTest extends SolrTestCaseJ4 {}- static void createIndex() {+ static void createIndex() throws Exception {+ doEmptyFacetCounts(); // try on empty index+indexSimpleFacetCounts();indexDateFacets();indexFacetSingleValued();@@ -95,6 +102,67 @@ public class SimpleFacetsTest extends SolrTestCaseJ4 {"zerolen_s","");}+ @Test+ public void testEmptyFacetCounts() throws Exception {+ doEmptyFacetCounts();+ }++ // static so we can try both with and without an empty index+ static void doEmptyFacetCounts() throws Exception {+ doEmptyFacetCounts("empty_t", new String[]{null, "myprefix",""});+ doEmptyFacetCounts("empty_i", new String[]{null});+ doEmptyFacetCounts("empty_f", new String[]{null});+ doEmptyFacetCounts("empty_s", new String[]{null, "myprefix",""});+ doEmptyFacetCounts("empty_d", new String[]{null});+ }++ static void doEmptyFacetCounts(String field, String[] prefixes) throws Exception {+ SchemaField sf = h.getCore().getSchema().getField(field);++ String response = JQ(req("q", "*:*"));+ Map rsp = (Map) ObjectBuilder.fromJSON(response);+ Long numFound = (Long)(((Map)rsp.get("response")).get("numFound"));++ ModifiableSolrParams params = params("q","*:*", "rows","0", "facet","true", "facet.field","{!key=myalias}"+field);++ String[] methods = {null, "fc","enum","fcs"};+ if (sf.multiValued() || sf.getType().multiValuedFieldCache()) {+ methods = new String[]{null, "fc","enum"};+ }++ prefixes = prefixes==null ? new String[]{null} : prefixes;+++ for (String method : methods) {+ if (method == null) {+ params.remove("facet.method");+ } else {+ params.set("facet.method", method);+ }+ for (String prefix : prefixes) {+ if (prefix == null) {+ params.remove("facet.prefix");+ } else {+ params.set("facet.prefix", prefix);+ }++ for (String missing : new String[] {null, "true"}) {+ if (missing == null) {+ params.remove("facet.missing");+ } else {+ params.set("facet.missing", missing);+ }++ String expected = missing==null ? "[]" : "[null," + numFound + "]";++ assertJQ(req(params),+ "/facet_counts/facet_fields/myalias==" + expected);+ }+ }+ }+ }++@Testpublic void testSimpleFacetCounts() {