You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by yo...@apache.org on 2016/04/27 21:31:09 UTC

lucene-solr:branch_5_5: SOLR-8886: fix TrieField.toObject(IndexableField) for docValues

Repository: lucene-solr
Updated Branches:
  refs/heads/branch_5_5 51895a76b -> fedbbfe29


SOLR-8886: fix TrieField.toObject(IndexableField) for docValues


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/fedbbfe2
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/fedbbfe2
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/fedbbfe2

Branch: refs/heads/branch_5_5
Commit: fedbbfe29fea6f7382de63162fc973c9c2964bea
Parents: 51895a7
Author: yonik <yo...@apache.org>
Authored: Wed Apr 27 15:29:40 2016 -0400
Committer: yonik <yo...@apache.org>
Committed: Wed Apr 27 15:30:56 2016 -0400

----------------------------------------------------------------------
 solr/CHANGES.txt                                |  4 ++
 .../java/org/apache/solr/schema/TrieField.java  | 43 +++++++++++++++-----
 .../solr/collection1/conf/schema-docValues.xml  | 11 +++++
 .../org/apache/solr/schema/DocValuesTest.java   | 28 ++++++++++++-
 4 files changed, 74 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/fedbbfe2/solr/CHANGES.txt
----------------------------------------------------------------------
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index ae3f389..7b5798b 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -127,6 +127,10 @@ Bug Fixes
 
 * SOLR-9016: Fix SolrIdentifierValidator to not allow empty identifiers. (Shai Erera)
 
+* SOLR-8886: Fix TrieField.toObject(IndexableField) to work for field with docValues
+  enabled. (yonik)
+
+
 Other Changes
 ----------------------
 

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/fedbbfe2/solr/core/src/java/org/apache/solr/schema/TrieField.java
----------------------------------------------------------------------
diff --git a/solr/core/src/java/org/apache/solr/schema/TrieField.java b/solr/core/src/java/org/apache/solr/schema/TrieField.java
index 13417d9..02e85a8 100644
--- a/solr/core/src/java/org/apache/solr/schema/TrieField.java
+++ b/solr/core/src/java/org/apache/solr/schema/TrieField.java
@@ -33,6 +33,8 @@ import org.apache.lucene.document.IntField;
 import org.apache.lucene.document.LongField;
 import org.apache.lucene.document.NumericDocValuesField;
 import org.apache.lucene.document.SortedSetDocValuesField;
+import org.apache.lucene.index.DocValuesType;
+import org.apache.lucene.index.IndexOptions;
 import org.apache.lucene.index.IndexableField;
 import org.apache.lucene.index.IndexOptions;
 import org.apache.lucene.queries.function.ValueSource;
@@ -111,30 +113,51 @@ public class TrieField extends PrimitiveFieldType {
     }
   }
 
+
   @Override
   public Object toObject(IndexableField f) {
     final Number val = f.numericValue();
     if (val != null) {
+
+      if (f.fieldType().stored() == false && f.fieldType().docValuesType() == DocValuesType.NUMERIC ) {
+        long bits = val.longValue();
+        switch (type) {
+          case INTEGER:
+            return (int)bits;
+          case FLOAT:
+            return Float.intBitsToFloat((int)bits);
+          case LONG:
+            return bits;
+          case DOUBLE:
+            return Double.longBitsToDouble(bits);
+          case DATE:
+            return new Date(bits);
+          default:
+            throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unknown type for trie field: " + f.name());
+        }
+      }
+
+      // normal stored case
       return (type == TrieTypes.DATE) ? new Date(val.longValue()) : val;
     } else {
-      // the following code is "deprecated" and only to support pre-3.2 indexes using the old BinaryField encoding:
-      final BytesRef bytes = f.binaryValue();
-      if (bytes==null) return badFieldString(f);
+      // multi-valued numeric docValues currently use SortedSet on the indexed terms.
+      BytesRef term = f.binaryValue();
       switch (type) {
         case INTEGER:
-          return toInt(bytes.bytes, bytes.offset);
+          return NumericUtils.prefixCodedToInt(term);
         case FLOAT:
-          return Float.intBitsToFloat(toInt(bytes.bytes, bytes.offset));
+          return NumericUtils.sortableIntToFloat(NumericUtils.prefixCodedToInt(term));
         case LONG:
-          return toLong(bytes.bytes, bytes.offset);
+          return NumericUtils.prefixCodedToLong(term);
         case DOUBLE:
-          return Double.longBitsToDouble(toLong(bytes.bytes, bytes.offset));
+          return NumericUtils.sortableLongToDouble(NumericUtils.prefixCodedToLong(term));
         case DATE:
-          return new Date(toLong(bytes.bytes, bytes.offset));
+          return new Date(NumericUtils.prefixCodedToLong(term));
         default:
           throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unknown type for trie field: " + f.name());
       }
     }
+
   }
 
   @Override
@@ -421,7 +444,7 @@ public class TrieField extends PrimitiveFieldType {
 
     return query;
   }
-  
+
   @Override
   public Query getFieldQuery(QParser parser, SchemaField field, String externalVal) {
     if (!field.indexed() && field.hasDocValues()) {
@@ -436,7 +459,7 @@ public class TrieField extends PrimitiveFieldType {
   static int toInt(byte[] arr, int offset) {
     return (arr[offset]<<24) | ((arr[offset+1]&0xff)<<16) | ((arr[offset+2]&0xff)<<8) | (arr[offset+3]&0xff);
   }
-  
+
   @Deprecated
   static long toLong(byte[] arr, int offset) {
     int high = (arr[offset]<<24) | ((arr[offset+1]&0xff)<<16) | ((arr[offset+2]&0xff)<<8) | (arr[offset+3]&0xff);

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/fedbbfe2/solr/core/src/test-files/solr/collection1/conf/schema-docValues.xml
----------------------------------------------------------------------
diff --git a/solr/core/src/test-files/solr/collection1/conf/schema-docValues.xml b/solr/core/src/test-files/solr/collection1/conf/schema-docValues.xml
index 851ff65..114afbc 100644
--- a/solr/core/src/test-files/solr/collection1/conf/schema-docValues.xml
+++ b/solr/core/src/test-files/solr/collection1/conf/schema-docValues.xml
@@ -67,6 +67,17 @@
     <field name="datedv" type="date" indexed="false" stored="false" docValues="true" default="1995-12-31T23:59:59.999Z" />
 
     <field name="stringdv" type="string" indexed="false" stored="false" docValues="true" default="solr" />
+
+    <field name="floatdvs" type="float" indexed="false" stored="false" docValues="true" default="1" />
+    <field name="intdvs" type="int" indexed="false" stored="false" docValues="true" default="2" />
+    <field name="doubledvs" type="double" indexed="false" stored="false" docValues="true" default="3" />
+    <field name="longdvs" type="long" indexed="false" stored="false" docValues="true" default="4" />
+    <field name="datedvs" type="date" indexed="false" stored="false" docValues="true" default="1995-12-31T23:59:59.999Z" />
+    <field name="stringdvs" type="string" indexed="false" stored="false" docValues="true" default="solr" />
+
+
+
+
   </fields>
 
   <uniqueKey>id</uniqueKey>

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/fedbbfe2/solr/core/src/test/org/apache/solr/schema/DocValuesTest.java
----------------------------------------------------------------------
diff --git a/solr/core/src/test/org/apache/solr/schema/DocValuesTest.java b/solr/core/src/test/org/apache/solr/schema/DocValuesTest.java
index a9aa401..7a1e371 100644
--- a/solr/core/src/test/org/apache/solr/schema/DocValuesTest.java
+++ b/solr/core/src/test/org/apache/solr/schema/DocValuesTest.java
@@ -16,6 +16,7 @@
  */
 package org.apache.solr.schema;
 
+import org.apache.lucene.index.IndexableField;
 import org.apache.lucene.index.LeafReader;
 import org.apache.lucene.index.DocValuesType;
 import org.apache.lucene.index.FieldInfos;
@@ -27,6 +28,9 @@ import org.apache.solr.util.RefCounted;
 import org.junit.BeforeClass;
 
 import java.io.IOException;
+import java.util.Date;
+import java.util.List;
+
 
 public class DocValuesTest extends SolrTestCaseJ4 {
 
@@ -79,12 +83,32 @@ public class DocValuesTest extends SolrTestCaseJ4 {
         values = longDv.getType().getValueSource(longDv, null).getValues(null, searcher.getLeafReader().leaves().get(0));
         assertEquals(4L, values.longVal(0));
         assertEquals(4L, values.objectVal(0));
+
+        // check reversability of created fields
+        tstToObj(schema.getField("floatdv"), -1.5f);
+        tstToObj(schema.getField("floatdvs"), -1.5f);
+        tstToObj(schema.getField("doubledv"), -1.5d);
+        tstToObj(schema.getField("doubledvs"), -1.5d);
+        tstToObj(schema.getField("intdv"), -7);
+        tstToObj(schema.getField("intdvs"), -7);
+        tstToObj(schema.getField("longdv"), -11L);
+        tstToObj(schema.getField("longdvs"), -11L);
+        tstToObj(schema.getField("datedv"), new Date(1000));
+        tstToObj(schema.getField("datedvs"), new Date(1000));
+
       } finally {
         searcherRef.decref();
       }
     }
   }
 
+  private void tstToObj(SchemaField sf, Object o) {
+    List<IndexableField> fields = sf.createFields(o, 1.0f);
+    for (IndexableField field : fields) {
+      assertEquals( sf.getType().toObject(field), o);
+    }
+  }
+
   public void testDocValuesSorting() {
     assertU(adoc("id", "1", "floatdv", "2", "intdv", "3", "doubledv", "4", "longdv", "5", "datedv", "1995-12-31T23:59:59.999Z", "stringdv", "b"));
     assertU(adoc("id", "2", "floatdv", "5", "intdv", "4", "doubledv", "3", "longdv", "2", "datedv", "1997-12-31T23:59:59.999Z", "stringdv", "a"));
@@ -252,7 +276,7 @@ public class DocValuesTest extends SolrTestCaseJ4 {
     assertU(adoc("id", "3", "floatdv", "3", "intdv", "1", "doubledv", "2", "longdv", "1", "datedv", "1996-12-31T23:59:59.999Z", "stringdv", "c"));
     assertU(adoc("id", "4", "floatdv", "3", "intdv", "1", "doubledv", "2", "longdv", "1", "datedv", "1996-12-31T23:59:59.999Z", "stringdv", "car"));
     assertU(commit());
-    
+
     // string: termquery
     assertQ(req("q", "stringdv:car", "sort", "id asc"),
         "//*[@numFound='1']",
@@ -314,7 +338,7 @@ public class DocValuesTest extends SolrTestCaseJ4 {
         "//result/doc[1]/str[@name='id'][.=1]",
         "//result/doc[2]/str[@name='id'][.=2]"
     );
-    
+
     // long: termquery
     assertQ(req("q", "longdv:1", "sort", "id asc"),
         "//*[@numFound='2']",