Skip to content

Apache Lucene open-source search software

SOLR-5846: EnumField supports DocValues functionality

Shalin Shekhar Mangar059e5259c682
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 205d6177242..ce1c7ff7a65 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -150,6 +150,8 @@ New Features
* SOLR-6841: Visualize lucene segment information in Admin UI.
(Alexey Kozhemiakin, Michal Bienkowski, hossman, Shawn Heisey, Varun Thacker via shalin)
+* SOLR-5846: EnumField supports DocValues functionality. (Elran Dvir, shalin)
+
Bug Fixes
----------------------
diff --git a/solr/core/src/java/org/apache/solr/schema/EnumField.java b/solr/core/src/java/org/apache/solr/schema/EnumField.java
index bea245a8587..0553233ad64 100644
--- a/solr/core/src/java/org/apache/solr/schema/EnumField.java
+++ b/solr/core/src/java/org/apache/solr/schema/EnumField.java
@@ -18,7 +18,10 @@ package org.apache.solr.schema;
*/
import org.apache.lucene.document.FieldType;
+import org.apache.lucene.document.NumericDocValuesField;
+import org.apache.lucene.document.SortedSetDocValuesField;
import org.apache.lucene.index.IndexOptions;
+import org.apache.lucene.index.IndexableField;
import org.apache.lucene.index.StorableField;
import org.apache.lucene.queries.function.ValueSource;
import org.apache.lucene.queries.function.valuesource.EnumFieldSource;
@@ -49,9 +52,7 @@ import javax.xml.xpath.XPathFactory;
import java.io.IOException;
import java.io.InputStream;
-import java.util.HashMap;
-import java.util.Locale;
-import java.util.Map;
+import java.util.*;
/***
* Field type for support of string values with custom sort order.
@@ -222,6 +223,14 @@ public class EnumField extends PrimitiveFieldType {
return false;
}
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public FieldType.NumericType getNumericType() {
+ return FieldType.NumericType.INT;
+ }
+
/**
* {@inheritDoc}
*/
@@ -255,10 +264,7 @@ public class EnumField extends PrimitiveFieldType {
* {@inheritDoc}
*/
@Override
- public void checkSchemaField(final SchemaField field) {
- if (field.hasDocValues() && !field.multiValued() && !(field.isRequired() || field.getDefaultValue() != null)) {
- throw new IllegalStateException("Field " + this + " has single-valued doc values enabled, but has no default value and is not required");
- }
+ public void checkSchemaField(SchemaField field) {
}
/**
@@ -393,6 +399,30 @@ public class EnumField extends PrimitiveFieldType {
return f;
}
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public List<StorableField> createFields(SchemaField sf, Object value, float boost) {
+ if (sf.hasDocValues()) {
+ List<StorableField> fields = new ArrayList<>();
+ final StorableField field = createField(sf, value, boost);
+ fields.add(field);
+
+ if (sf.multiValued()) {
+ BytesRefBuilder bytes = new BytesRefBuilder();
+ readableToIndexed(stringValueToIntValue(value.toString()).toString(), bytes);
+ fields.add(new SortedSetDocValuesField(sf.getName(), bytes.toBytesRef()));
+ } else {
+ final long bits = field.numericValue().intValue();
+ fields.add(new NumericDocValuesField(sf.getName(), bits));
+ }
+ return fields;
+ } else {
+ return Collections.singletonList(createField(sf, value, boost));
+ }
+ }
+
/**
* Converting the (internal) integer value (indicating the sort order) to string (displayed) value
* @param intVal integer value
diff --git a/solr/core/src/test-files/solr/collection1/conf/schema-enums.xml b/solr/core/src/test-files/solr/collection1/conf/schema-enums.xml
index 639ba3d0069..b736b896dc6 100644
--- a/solr/core/src/test-files/solr/collection1/conf/schema-enums.xml
+++ b/solr/core/src/test-files/solr/collection1/conf/schema-enums.xml
@@ -21,6 +21,7 @@
<field name="_version_" type="long" indexed="true" stored="true" multiValued="false"/>
<!-- Test EnumField -->
<field name="severity" type="severityType" indexed="true" stored="true" multiValued="false"/>
+ <field name="severity_dv" type="severityType" indexed="true" stored="true" multiValued="false" docValues="true"/>
<field name="text" type="text" indexed="true" stored="true" multiValued="true"/>
</fields>
<uniqueKey>id</uniqueKey>
diff --git a/solr/core/src/test/org/apache/solr/schema/EnumFieldTest.java b/solr/core/src/test/org/apache/solr/schema/EnumFieldTest.java
index 0b95dc9b464..38c50017bcc 100644
--- a/solr/core/src/test/org/apache/solr/schema/EnumFieldTest.java
+++ b/solr/core/src/test/org/apache/solr/schema/EnumFieldTest.java
@@ -24,7 +24,7 @@ import org.junit.Test;
public class EnumFieldTest extends SolrTestCaseJ4 {
- private final static String FIELD_NAME = "severity";
+ private final String FIELD_NAME = random().nextBoolean() ? "severity" : "severity_dv";
@BeforeClass
public static void beforeClass() throws Exception {
@@ -178,7 +178,7 @@ public class EnumFieldTest extends SolrTestCaseJ4 {
assertU(commit());
- assertQ(req("fl", "" + FIELD_NAME, "q", "*:*"), "//doc[1]/str[@name='severity']/text()='Low'");
+ assertQ(req("fl", "" + FIELD_NAME, "q", "*:*"), "//doc[1]/str[@name='" + FIELD_NAME + "']/text()='Low'");
}
@Test
@@ -198,17 +198,17 @@ public class EnumFieldTest extends SolrTestCaseJ4 {
assertU(commit());
- assertQ(req("fl", "" + FIELD_NAME, "q", "*:*", "sort", FIELD_NAME + " desc"), "//doc[1]/str[@name='severity']/text()='Critical'",
- "//doc[2]/str[@name='severity']/text()='High'", "//doc[3]/str[@name='severity']/text()='Medium'", "//doc[4]/str[@name='severity']/text()='Low'",
- "//doc[5]/str[@name='severity']/text()='Not Available'");
+ assertQ(req("fl", "" + FIELD_NAME, "q", "*:*", "sort", FIELD_NAME + " desc"), "//doc[1]/str[@name='" + FIELD_NAME + "']/text()='Critical'",
+ "//doc[2]/str[@name='" + FIELD_NAME + "']/text()='High'", "//doc[3]/str[@name='" + FIELD_NAME + "']/text()='Medium'", "//doc[4]/str[@name='" + FIELD_NAME + "']/text()='Low'",
+ "//doc[5]/str[@name='" + FIELD_NAME + "']/text()='Not Available'");
//sort ascending - empty values will be first
- assertQ(req("fl", "" + FIELD_NAME, "q", "*:*", "sort", FIELD_NAME + " asc"), "//doc[3]/str[@name='severity']/text()='Not Available'");
+ assertQ(req("fl", "" + FIELD_NAME, "q", "*:*", "sort", FIELD_NAME + " asc"), "//doc[3]/str[@name='" + FIELD_NAME + "']/text()='Not Available'");
//q for not empty docs
- assertQ(req("fl", "" + FIELD_NAME, "q", FIELD_NAME + ":[* TO *]" , "sort", FIELD_NAME + " asc"), "//doc[1]/str[@name='severity']/text()='Not Available'",
- "//doc[2]/str[@name='severity']/text()='Low'", "//doc[3]/str[@name='severity']/text()='Medium'", "//doc[4]/str[@name='severity']/text()='High'",
- "//doc[5]/str[@name='severity']/text()='Critical'"
+ assertQ(req("fl", "" + FIELD_NAME, "q", FIELD_NAME + ":[* TO *]" , "sort", FIELD_NAME + " asc"), "//doc[1]/str[@name='" + FIELD_NAME + "']/text()='Not Available'",
+ "//doc[2]/str[@name='" + FIELD_NAME + "']/text()='Low'", "//doc[3]/str[@name='" + FIELD_NAME + "']/text()='Medium'", "//doc[4]/str[@name='" + FIELD_NAME + "']/text()='High'",
+ "//doc[5]/str[@name='" + FIELD_NAME + "']/text()='Critical'"
);
}