incubator-blur-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From amccu...@apache.org
Subject git commit: Adding sorting support to all numeric types.
Date Wed, 29 Jan 2014 15:05:46 GMT
Updated Branches:
  refs/heads/apache-blur-0.2 5e4956712 -> d2935accc


Adding sorting support to all numeric types.


Project: http://git-wip-us.apache.org/repos/asf/incubator-blur/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-blur/commit/d2935acc
Tree: http://git-wip-us.apache.org/repos/asf/incubator-blur/tree/d2935acc
Diff: http://git-wip-us.apache.org/repos/asf/incubator-blur/diff/d2935acc

Branch: refs/heads/apache-blur-0.2
Commit: d2935accc3538172121c57703c84c93fa2e2fb40
Parents: 5e49567
Author: Aaron McCurry <amccurry@gmail.com>
Authored: Wed Jan 29 10:05:36 2014 -0500
Committer: Aaron McCurry <amccurry@gmail.com>
Committed: Wed Jan 29 10:05:36 2014 -0500

----------------------------------------------------------------------
 .../blur/analysis/FieldTypeDefinition.java      |  8 +++++++
 .../analysis/type/DateFieldTypeDefinition.java  | 17 ++++++++++++++-
 .../type/DoubleFieldTypeDefinition.java         | 21 +++++++++++++++++--
 .../analysis/type/FloatFieldTypeDefinition.java | 20 ++++++++++++++++--
 .../analysis/type/IntFieldTypeDefinition.java   | 22 +++++++++++++++++---
 .../analysis/type/LongFieldTypeDefinition.java  | 20 ++++++++++++++++--
 .../type/NumericFieldTypeDefinition.java        | 20 +++++++++++-------
 .../type/StringFieldTypeDefinition.java         | 19 ++++++++++-------
 8 files changed, 123 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/d2935acc/blur-query/src/main/java/org/apache/blur/analysis/FieldTypeDefinition.java
----------------------------------------------------------------------
diff --git a/blur-query/src/main/java/org/apache/blur/analysis/FieldTypeDefinition.java b/blur-query/src/main/java/org/apache/blur/analysis/FieldTypeDefinition.java
index d8862ab..bd82b64 100644
--- a/blur-query/src/main/java/org/apache/blur/analysis/FieldTypeDefinition.java
+++ b/blur-query/src/main/java/org/apache/blur/analysis/FieldTypeDefinition.java
@@ -146,6 +146,14 @@ public abstract class FieldTypeDefinition {
     };
   }
 
+  protected String getFieldName() {
+    if (_subColumnName == null) {
+      return getName(_family, _columnName);
+    } else {
+      return getName(_family, _columnName, _subColumnName);
+    }
+  }
+
   protected String getName(String family, String name) {
     return family + "." + name;
   }

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/d2935acc/blur-query/src/main/java/org/apache/blur/analysis/type/DateFieldTypeDefinition.java
----------------------------------------------------------------------
diff --git a/blur-query/src/main/java/org/apache/blur/analysis/type/DateFieldTypeDefinition.java
b/blur-query/src/main/java/org/apache/blur/analysis/type/DateFieldTypeDefinition.java
index e3c8cc3..45c06fb 100644
--- a/blur-query/src/main/java/org/apache/blur/analysis/type/DateFieldTypeDefinition.java
+++ b/blur-query/src/main/java/org/apache/blur/analysis/type/DateFieldTypeDefinition.java
@@ -29,9 +29,12 @@ import org.apache.hadoop.conf.Configuration;
 import org.apache.lucene.document.Field;
 import org.apache.lucene.document.FieldType;
 import org.apache.lucene.document.LongField;
+import org.apache.lucene.document.NumericDocValuesField;
 import org.apache.lucene.document.StoredField;
 import org.apache.lucene.search.NumericRangeQuery;
 import org.apache.lucene.search.Query;
+import org.apache.lucene.search.SortField;
+import org.apache.lucene.search.SortField.Type;
 
 public class DateFieldTypeDefinition extends NumericFieldTypeDefinition {
 
@@ -93,6 +96,9 @@ public class DateFieldTypeDefinition extends NumericFieldTypeDefinition
{
     List<Field> fields = new ArrayList<Field>();
     fields.add(field);
     fields.add(storedField);
+    if (isSortEnable()) {
+      fields.add(new NumericDocValuesField(name, date));
+    }
     return fields;
   }
 
@@ -100,7 +106,11 @@ public class DateFieldTypeDefinition extends NumericFieldTypeDefinition
{
   public Iterable<? extends Field> getFieldsForSubColumn(String family, Column column,
String subName) {
     String name = getName(family, column.getName(), subName);
     long date = parseDate(column.getValue());
-    return makeIterable(new LongField(name, date, _typeNotStored));
+    LongField field = new LongField(name, date, _typeNotStored);
+    if (isSortEnable()) {
+      return addSort(name, date, field);
+    }
+    return makeIterable(field);
   }
 
   @Override
@@ -110,4 +120,9 @@ public class DateFieldTypeDefinition extends NumericFieldTypeDefinition
{
     return NumericRangeQuery.newLongRange(field, _precisionStep, p1, p2, startInclusive,
endInclusive);
   }
 
+  @Override
+  public SortField getSortField(boolean reverse) {
+    return new SortField(getFieldName(), Type.LONG);
+  }
+
 }

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/d2935acc/blur-query/src/main/java/org/apache/blur/analysis/type/DoubleFieldTypeDefinition.java
----------------------------------------------------------------------
diff --git a/blur-query/src/main/java/org/apache/blur/analysis/type/DoubleFieldTypeDefinition.java
b/blur-query/src/main/java/org/apache/blur/analysis/type/DoubleFieldTypeDefinition.java
index bb6464e..1bdf050 100644
--- a/blur-query/src/main/java/org/apache/blur/analysis/type/DoubleFieldTypeDefinition.java
+++ b/blur-query/src/main/java/org/apache/blur/analysis/type/DoubleFieldTypeDefinition.java
@@ -25,6 +25,8 @@ import org.apache.lucene.document.Field;
 import org.apache.lucene.document.FieldType;
 import org.apache.lucene.search.NumericRangeQuery;
 import org.apache.lucene.search.Query;
+import org.apache.lucene.search.SortField;
+import org.apache.lucene.search.SortField.Type;
 
 public class DoubleFieldTypeDefinition extends NumericFieldTypeDefinition {
 
@@ -57,14 +59,23 @@ public class DoubleFieldTypeDefinition extends NumericFieldTypeDefinition
{
   @Override
   public Iterable<? extends Field> getFieldsForColumn(String family, Column column)
{
     String name = getName(family, column.getName());
-    DoubleField field = new DoubleField(name, Double.parseDouble(column.getValue()), _typeStored);
+    double value = Double.parseDouble(column.getValue());
+    DoubleField field = new DoubleField(name, value, _typeStored);
+    if (isSortEnable()) {
+      return addSort(name, Double.doubleToRawLongBits(value), field);
+    }
     return makeIterable(field);
   }
 
   @Override
   public Iterable<? extends Field> getFieldsForSubColumn(String family, Column column,
String subName) {
     String name = getName(family, column.getName(), subName);
-    return makeIterable(new DoubleField(name, Double.parseDouble(column.getValue()), _typeNotStored));
+    double value = Double.parseDouble(column.getValue());
+    DoubleField field = new DoubleField(name, value, _typeNotStored);
+    if (isSortEnable()) {
+      return addSort(name, Double.doubleToRawLongBits(value), field);
+    }
+    return makeIterable(field);
   }
 
   @Override
@@ -74,6 +85,12 @@ public class DoubleFieldTypeDefinition extends NumericFieldTypeDefinition
{
     return NumericRangeQuery.newDoubleRange(field, _precisionStep, p1, p2, startInclusive,
endInclusive);
   }
   
+
+  @Override
+  public SortField getSortField(boolean reverse) {
+    return new SortField(getFieldName(), Type.DOUBLE);
+  }
+  
   private double parseDouble(String number) {
     if (number.toLowerCase().equals(MIN)) {
       return Double.MIN_VALUE;

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/d2935acc/blur-query/src/main/java/org/apache/blur/analysis/type/FloatFieldTypeDefinition.java
----------------------------------------------------------------------
diff --git a/blur-query/src/main/java/org/apache/blur/analysis/type/FloatFieldTypeDefinition.java
b/blur-query/src/main/java/org/apache/blur/analysis/type/FloatFieldTypeDefinition.java
index 730733b..2b90231 100644
--- a/blur-query/src/main/java/org/apache/blur/analysis/type/FloatFieldTypeDefinition.java
+++ b/blur-query/src/main/java/org/apache/blur/analysis/type/FloatFieldTypeDefinition.java
@@ -25,6 +25,8 @@ import org.apache.lucene.document.FieldType;
 import org.apache.lucene.document.FloatField;
 import org.apache.lucene.search.NumericRangeQuery;
 import org.apache.lucene.search.Query;
+import org.apache.lucene.search.SortField;
+import org.apache.lucene.search.SortField.Type;
 
 public class FloatFieldTypeDefinition extends NumericFieldTypeDefinition {
 
@@ -57,14 +59,23 @@ public class FloatFieldTypeDefinition extends NumericFieldTypeDefinition
{
   @Override
   public Iterable<? extends Field> getFieldsForColumn(String family, Column column)
{
     String name = getName(family, column.getName());
-    FloatField field = new FloatField(name, Float.parseFloat(column.getValue()), _typeStored);
+    float value = Float.parseFloat(column.getValue());
+    FloatField field = new FloatField(name, value, _typeStored);
+    if (isSortEnable()) {
+      return addSort(name, Float.floatToRawIntBits(value), field);
+    }
     return makeIterable(field);
   }
 
   @Override
   public Iterable<? extends Field> getFieldsForSubColumn(String family, Column column,
String subName) {
     String name = getName(family, column.getName(), subName);
-    return makeIterable(new FloatField(name, Float.parseFloat(column.getValue()), _typeNotStored));
+    float value = Float.parseFloat(column.getValue());
+    FloatField field = new FloatField(name, value, _typeNotStored);
+    if (isSortEnable()) {
+      return addSort(name, Float.floatToRawIntBits(value), field);
+    }
+    return makeIterable(field);
   }
 
   @Override
@@ -73,6 +84,11 @@ public class FloatFieldTypeDefinition extends NumericFieldTypeDefinition
{
     float p2 = parseFloat(part2);
     return NumericRangeQuery.newFloatRange(field, _precisionStep, p1, p2, startInclusive,
endInclusive);
   }
+  
+  @Override
+  public SortField getSortField(boolean reverse) {
+    return new SortField(getFieldName(), Type.FLOAT);
+  }
 
   private float parseFloat(String number) {
     if (number.toLowerCase().equals(MIN)) {

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/d2935acc/blur-query/src/main/java/org/apache/blur/analysis/type/IntFieldTypeDefinition.java
----------------------------------------------------------------------
diff --git a/blur-query/src/main/java/org/apache/blur/analysis/type/IntFieldTypeDefinition.java
b/blur-query/src/main/java/org/apache/blur/analysis/type/IntFieldTypeDefinition.java
index 890d0c8..e2751d2 100644
--- a/blur-query/src/main/java/org/apache/blur/analysis/type/IntFieldTypeDefinition.java
+++ b/blur-query/src/main/java/org/apache/blur/analysis/type/IntFieldTypeDefinition.java
@@ -25,6 +25,8 @@ import org.apache.lucene.document.FieldType;
 import org.apache.lucene.document.IntField;
 import org.apache.lucene.search.NumericRangeQuery;
 import org.apache.lucene.search.Query;
+import org.apache.lucene.search.SortField;
+import org.apache.lucene.search.SortField.Type;
 
 public class IntFieldTypeDefinition extends NumericFieldTypeDefinition {
 
@@ -57,14 +59,23 @@ public class IntFieldTypeDefinition extends NumericFieldTypeDefinition
{
   @Override
   public Iterable<? extends Field> getFieldsForColumn(String family, Column column)
{
     String name = getName(family, column.getName());
-    IntField field = new IntField(name, Integer.parseInt(column.getValue()), _typeStored);
+    int value = Integer.parseInt(column.getValue());
+    IntField field = new IntField(name, value, _typeStored);
+    if (isSortEnable()) {
+      return addSort(name, value, field);
+    }
     return makeIterable(field);
   }
 
   @Override
   public Iterable<? extends Field> getFieldsForSubColumn(String family, Column column,
String subName) {
     String name = getName(family, column.getName(), subName);
-    return makeIterable(new IntField(name, Integer.parseInt(column.getValue()), _typeNotStored));
+    int value = Integer.parseInt(column.getValue());
+    IntField field = new IntField(name, value, _typeNotStored);
+    if (isSortEnable()) {
+      return addSort(name, value, field);
+    }
+    return makeIterable(field);
   }
 
   @Override
@@ -73,7 +84,12 @@ public class IntFieldTypeDefinition extends NumericFieldTypeDefinition
{
     int p2 = parseInt(part2);
     return NumericRangeQuery.newIntRange(field, _precisionStep, p1, p2, startInclusive, endInclusive);
   }
-  
+
+  @Override
+  public SortField getSortField(boolean reverse) {
+    return new SortField(getFieldName(), Type.INT);
+  }
+
   private int parseInt(String number) {
     if (number.toLowerCase().equals(MIN)) {
       return Integer.MIN_VALUE;

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/d2935acc/blur-query/src/main/java/org/apache/blur/analysis/type/LongFieldTypeDefinition.java
----------------------------------------------------------------------
diff --git a/blur-query/src/main/java/org/apache/blur/analysis/type/LongFieldTypeDefinition.java
b/blur-query/src/main/java/org/apache/blur/analysis/type/LongFieldTypeDefinition.java
index ec53927..45ed273 100644
--- a/blur-query/src/main/java/org/apache/blur/analysis/type/LongFieldTypeDefinition.java
+++ b/blur-query/src/main/java/org/apache/blur/analysis/type/LongFieldTypeDefinition.java
@@ -25,6 +25,8 @@ import org.apache.lucene.document.FieldType;
 import org.apache.lucene.document.LongField;
 import org.apache.lucene.search.NumericRangeQuery;
 import org.apache.lucene.search.Query;
+import org.apache.lucene.search.SortField;
+import org.apache.lucene.search.SortField.Type;
 
 public class LongFieldTypeDefinition extends NumericFieldTypeDefinition {
 
@@ -57,14 +59,23 @@ public class LongFieldTypeDefinition extends NumericFieldTypeDefinition
{
   @Override
   public Iterable<? extends Field> getFieldsForColumn(String family, Column column)
{
     String name = getName(family, column.getName());
-    LongField field = new LongField(name, Long.parseLong(column.getValue()), _typeStored);
+    long value = Long.parseLong(column.getValue());
+    LongField field = new LongField(name, value, _typeStored);
+    if (isSortEnable()) {
+      return addSort(name, value, field);
+    }
     return makeIterable(field);
   }
 
   @Override
   public Iterable<? extends Field> getFieldsForSubColumn(String family, Column column,
String subName) {
     String name = getName(family, column.getName(), subName);
-    return makeIterable(new LongField(name, Long.parseLong(column.getValue()), _typeNotStored));
+    long value = Long.parseLong(column.getValue());
+    LongField field = new LongField(name, value, _typeNotStored);
+    if (isSortEnable()) {
+      return addSort(name, value, field);
+    }
+    return makeIterable(field);
   }
 
   @Override
@@ -74,6 +85,11 @@ public class LongFieldTypeDefinition extends NumericFieldTypeDefinition
{
     return NumericRangeQuery.newLongRange(field, _precisionStep, p1, p2, startInclusive,
endInclusive);
   }
 
+  @Override
+  public SortField getSortField(boolean reverse) {
+    return new SortField(getFieldName(), Type.LONG);
+  }
+
   private long parseLong(String number) {
     if (number.toLowerCase().equals(MIN)) {
       return Long.MIN_VALUE;

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/d2935acc/blur-query/src/main/java/org/apache/blur/analysis/type/NumericFieldTypeDefinition.java
----------------------------------------------------------------------
diff --git a/blur-query/src/main/java/org/apache/blur/analysis/type/NumericFieldTypeDefinition.java
b/blur-query/src/main/java/org/apache/blur/analysis/type/NumericFieldTypeDefinition.java
index 02ec5b5..bd58e39 100644
--- a/blur-query/src/main/java/org/apache/blur/analysis/type/NumericFieldTypeDefinition.java
+++ b/blur-query/src/main/java/org/apache/blur/analysis/type/NumericFieldTypeDefinition.java
@@ -16,11 +16,15 @@ package org.apache.blur.analysis.type;
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+import java.util.ArrayList;
+import java.util.List;
+
 import org.apache.blur.analysis.FieldTypeDefinition;
 import org.apache.lucene.analysis.Analyzer;
 import org.apache.lucene.analysis.core.KeywordAnalyzer;
+import org.apache.lucene.document.Field;
+import org.apache.lucene.document.NumericDocValuesField;
 import org.apache.lucene.search.Query;
-import org.apache.lucene.search.SortField;
 import org.apache.lucene.util.NumericUtils;
 
 public abstract class NumericFieldTypeDefinition extends FieldTypeDefinition {
@@ -75,15 +79,17 @@ public abstract class NumericFieldTypeDefinition extends FieldTypeDefinition
{
 
   @Override
   public boolean checkSupportForSorting() {
-    return false;
+    return true;
+  }
+  
+  protected Iterable<? extends Field> addSort(String name, long value, Field field)
{
+    List<Field> list = new ArrayList<Field>();
+    list.add(field);
+    list.add(new NumericDocValuesField(name, value));
+    return list;
   }
 
   public abstract Query getNewRangeQuery(String field, String part1, String part2, boolean
startInclusive,
       boolean endInclusive);
 
-  @Override
-  public SortField getSortField(boolean reverse) {
-    throw new RuntimeException("Should never be called.");
-  }
-
 }

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/d2935acc/blur-query/src/main/java/org/apache/blur/analysis/type/StringFieldTypeDefinition.java
----------------------------------------------------------------------
diff --git a/blur-query/src/main/java/org/apache/blur/analysis/type/StringFieldTypeDefinition.java
b/blur-query/src/main/java/org/apache/blur/analysis/type/StringFieldTypeDefinition.java
index eac4b6e..5a1405f 100644
--- a/blur-query/src/main/java/org/apache/blur/analysis/type/StringFieldTypeDefinition.java
+++ b/blur-query/src/main/java/org/apache/blur/analysis/type/StringFieldTypeDefinition.java
@@ -35,7 +35,6 @@ import org.apache.lucene.util.BytesRef;
 public class StringFieldTypeDefinition extends FieldTypeDefinition {
 
   public static final String NAME = "string";
-  private String _fieldNameForThisInstance;
 
   @Override
   public String getName() {
@@ -44,7 +43,6 @@ public class StringFieldTypeDefinition extends FieldTypeDefinition {
 
   @Override
   public void configure(String fieldNameForThisInstance, Map<String, String> properties,
Configuration configuration) {
-    _fieldNameForThisInstance = fieldNameForThisInstance;
   }
 
   @Override
@@ -52,10 +50,7 @@ public class StringFieldTypeDefinition extends FieldTypeDefinition {
     String name = getName(family, column.getName());
     Field field = new Field(name, column.getValue(), StringField.TYPE_STORED);
     if (isSortEnable()) {
-      List<Field> list = new ArrayList<Field>();
-      list.add(field);
-      list.add(new SortedDocValuesField(name, new BytesRef(column.getValue())));
-      return list;
+      return addSort(column, name, field);
     }
     return makeIterable(field);
   }
@@ -64,9 +59,19 @@ public class StringFieldTypeDefinition extends FieldTypeDefinition {
   public Iterable<? extends Field> getFieldsForSubColumn(String family, Column column,
String subName) {
     String name = getName(family, column.getName(), subName);
     Field field = new Field(name, column.getValue(), StringField.TYPE_NOT_STORED);
+    if (isSortEnable()) {
+      return addSort(column, name, field);
+    }
     return makeIterable(field);
   }
 
+  private Iterable<? extends Field> addSort(Column column, String name, Field field)
{
+    List<Field> list = new ArrayList<Field>();
+    list.add(field);
+    list.add(new SortedDocValuesField(name, new BytesRef(column.getValue())));
+    return list;
+  }
+
   @Override
   public Analyzer getAnalyzerForIndex(String fieldName) {
     // shouldn't be used ever
@@ -115,6 +120,6 @@ public class StringFieldTypeDefinition extends FieldTypeDefinition {
 
   @Override
   public SortField getSortField(boolean reverse) {
-    return new SortField(_fieldNameForThisInstance, Type.STRING);
+    return new SortField(getFieldName(), Type.STRING);
   }
 }


Mime
View raw message