SOLR-5846: EnumField supports DocValues functionality

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1665107 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Shalin Shekhar Mangar 2015-03-09 01:29:28 +00:00
parent 66e5099e15
commit 059e5259c6
4 changed files with 49 additions and 16 deletions

View File

@ -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
----------------------

View File

@ -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

View File

@ -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>

View File

@ -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'"
);
}