Skip to content

Apache Lucene open-source search software

SOLR-2134: Trie* fields now support sortMissingFirst/Last

Ryan McKinleyccb141c00fb7
diff --git a/solr/src/java/org/apache/solr/schema/ByteField.java b/solr/src/java/org/apache/solr/schema/ByteField.java
index 0bd4adbdf04..80c769c8934 100644
--- a/solr/src/java/org/apache/solr/schema/ByteField.java
+++ b/solr/src/java/org/apache/solr/schema/ByteField.java
@@ -18,6 +18,8 @@ package org.apache.solr.schema;
import org.apache.lucene.document.Fieldable;
import org.apache.lucene.search.SortField;
+import org.apache.lucene.search.cache.ByteValuesCreator;
+import org.apache.lucene.search.cache.CachedArrayCreator;
import org.apache.solr.response.TextResponseWriter;
import org.apache.solr.response.XMLWriter;
@@ -41,7 +43,7 @@ public class ByteField extends FieldType {
}
public ValueSource getValueSource(SchemaField field) {
- return new ByteFieldSource(field.name);
+ return new ByteFieldSource( new ByteValuesCreator( field.name, null, CachedArrayCreator.CACHE_VALUES_AND_BITS ) );
}
diff --git a/solr/src/java/org/apache/solr/schema/DoubleField.java b/solr/src/java/org/apache/solr/schema/DoubleField.java
index 659f6111485..f0f2a637cde 100644
--- a/solr/src/java/org/apache/solr/schema/DoubleField.java
+++ b/solr/src/java/org/apache/solr/schema/DoubleField.java
@@ -19,9 +19,13 @@ package org.apache.solr.schema;
import org.apache.lucene.document.Fieldable;
import org.apache.lucene.search.SortField;
+import org.apache.lucene.search.cache.CachedArrayCreator;
+import org.apache.lucene.search.cache.DoubleValuesCreator;
+import org.apache.lucene.search.cache.FloatValuesCreator;
import org.apache.solr.response.TextResponseWriter;
import org.apache.solr.response.XMLWriter;
import org.apache.solr.search.function.DoubleFieldSource;
+import org.apache.solr.search.function.FloatFieldSource;
import org.apache.solr.search.function.ValueSource;
import java.io.IOException;
@@ -42,7 +46,7 @@ public class DoubleField extends FieldType {
public ValueSource getValueSource(SchemaField field) {
// fieldCache doesn't support double
- return new DoubleFieldSource(field.name);
+ return new DoubleFieldSource( new DoubleValuesCreator( field.name, null, CachedArrayCreator.CACHE_VALUES_AND_BITS ) );
}
public void write(XMLWriter xmlWriter, String name, Fieldable f) throws IOException {
diff --git a/solr/src/java/org/apache/solr/schema/FloatField.java b/solr/src/java/org/apache/solr/schema/FloatField.java
index 4aa6d3b62d4..a128537fa7e 100644
--- a/solr/src/java/org/apache/solr/schema/FloatField.java
+++ b/solr/src/java/org/apache/solr/schema/FloatField.java
@@ -18,6 +18,10 @@
package org.apache.solr.schema;
import org.apache.lucene.search.SortField;
+import org.apache.lucene.search.cache.CachedArrayCreator;
+import org.apache.lucene.search.cache.FloatValuesCreator;
+import org.apache.lucene.search.cache.LongValuesCreator;
+import org.apache.solr.search.function.LongFieldSource;
import org.apache.solr.search.function.ValueSource;
import org.apache.solr.search.function.FloatFieldSource;
import org.apache.lucene.document.Fieldable;
@@ -39,7 +43,7 @@ public class FloatField extends FieldType {
}
public ValueSource getValueSource(SchemaField field) {
- return new FloatFieldSource(field.name);
+ return new FloatFieldSource( new FloatValuesCreator( field.name, null, CachedArrayCreator.CACHE_VALUES_AND_BITS ) );
}
public void write(XMLWriter xmlWriter, String name, Fieldable f) throws IOException {
diff --git a/solr/src/java/org/apache/solr/schema/IntField.java b/solr/src/java/org/apache/solr/schema/IntField.java
index 5dd7b4a028e..7aede8961fc 100644
--- a/solr/src/java/org/apache/solr/schema/IntField.java
+++ b/solr/src/java/org/apache/solr/schema/IntField.java
@@ -18,6 +18,8 @@
package org.apache.solr.schema;
import org.apache.lucene.search.SortField;
+import org.apache.lucene.search.cache.CachedArrayCreator;
+import org.apache.lucene.search.cache.IntValuesCreator;
import org.apache.solr.search.function.ValueSource;
import org.apache.solr.search.function.IntFieldSource;
import org.apache.lucene.document.Fieldable;
@@ -39,7 +41,7 @@ public class IntField extends FieldType {
}
public ValueSource getValueSource(SchemaField field) {
- return new IntFieldSource(field.name);
+ return new IntFieldSource(new IntValuesCreator( field.name, null, CachedArrayCreator.CACHE_VALUES_AND_BITS ) );
}
public void write(XMLWriter xmlWriter, String name, Fieldable f) throws IOException {
diff --git a/solr/src/java/org/apache/solr/schema/LongField.java b/solr/src/java/org/apache/solr/schema/LongField.java
index 6634ba52ac6..fdb90e8a984 100644
--- a/solr/src/java/org/apache/solr/schema/LongField.java
+++ b/solr/src/java/org/apache/solr/schema/LongField.java
@@ -19,6 +19,9 @@ package org.apache.solr.schema;
import org.apache.lucene.document.Fieldable;
import org.apache.lucene.search.SortField;
+import org.apache.lucene.search.cache.ByteValuesCreator;
+import org.apache.lucene.search.cache.CachedArrayCreator;
+import org.apache.lucene.search.cache.LongValuesCreator;
import org.apache.solr.response.TextResponseWriter;
import org.apache.solr.response.XMLWriter;
import org.apache.solr.search.function.IntFieldSource;
@@ -43,8 +46,7 @@ public class LongField extends FieldType {
}
public ValueSource getValueSource(SchemaField field) {
-
- return new LongFieldSource(field.name);
+ return new LongFieldSource( new LongValuesCreator( field.name, null, CachedArrayCreator.CACHE_VALUES_AND_BITS ) );
}
diff --git a/solr/src/java/org/apache/solr/schema/ShortField.java b/solr/src/java/org/apache/solr/schema/ShortField.java
index 8cfecb55ff1..50886808414 100644
--- a/solr/src/java/org/apache/solr/schema/ShortField.java
+++ b/solr/src/java/org/apache/solr/schema/ShortField.java
@@ -18,6 +18,9 @@ package org.apache.solr.schema;
import org.apache.lucene.document.Fieldable;
import org.apache.lucene.search.SortField;
+import org.apache.lucene.search.cache.CachedArray;
+import org.apache.lucene.search.cache.CachedArrayCreator;
+import org.apache.lucene.search.cache.ShortValuesCreator;
import org.apache.solr.response.TextResponseWriter;
import org.apache.solr.response.XMLWriter;
@@ -46,7 +49,7 @@ public class ShortField extends FieldType {
public ValueSource getValueSource(SchemaField field) {
- return new ShortFieldSource(field.name);
+ return new ShortFieldSource(new ShortValuesCreator( field.name, null, CachedArrayCreator.CACHE_VALUES_AND_BITS ) );
}
diff --git a/solr/src/java/org/apache/solr/schema/TrieDateField.java b/solr/src/java/org/apache/solr/schema/TrieDateField.java
index cd6a3a6f4c7..953d40b58cf 100755
--- a/solr/src/java/org/apache/solr/schema/TrieDateField.java
+++ b/solr/src/java/org/apache/solr/schema/TrieDateField.java
@@ -33,6 +33,8 @@ import org.apache.lucene.search.SortField;
import org.apache.lucene.search.FieldCache;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.NumericRangeQuery;
+import org.apache.lucene.search.cache.CachedArrayCreator;
+import org.apache.lucene.search.cache.LongValuesCreator;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.NumericUtils;
import org.apache.lucene.analysis.TokenStream;
@@ -72,17 +74,17 @@ public class TrieDateField extends DateField {
@Override
public SortField getSortField(SchemaField field, boolean top) {
- return new SortField(field.getName(), FieldCache.NUMERIC_UTILS_LONG_PARSER, top);
+ return new SortField(new LongValuesCreator( field.getName(), FieldCache.NUMERIC_UTILS_LONG_PARSER, CachedArrayCreator.CACHE_VALUES_AND_BITS ), top);
}
@Override
public ValueSource getValueSource(SchemaField field) {
- return new TrieDateFieldSource(field.getName(), FieldCache.NUMERIC_UTILS_LONG_PARSER);
+ return new TrieDateFieldSource( new LongValuesCreator( field.getName(), FieldCache.NUMERIC_UTILS_LONG_PARSER, CachedArrayCreator.CACHE_VALUES_AND_BITS ));
}
@Override
public ValueSource getValueSource(SchemaField field, QParser parser) {
- return new TrieDateFieldSource(field.getName(), FieldCache.NUMERIC_UTILS_LONG_PARSER);
+ return new TrieDateFieldSource( new LongValuesCreator( field.getName(), FieldCache.NUMERIC_UTILS_LONG_PARSER, CachedArrayCreator.CACHE_VALUES_AND_BITS ));
}
@Override
diff --git a/solr/src/java/org/apache/solr/schema/TrieField.java b/solr/src/java/org/apache/solr/schema/TrieField.java
index 36c101fc515..f5ade52e4e6 100644
--- a/solr/src/java/org/apache/solr/schema/TrieField.java
+++ b/solr/src/java/org/apache/solr/schema/TrieField.java
@@ -19,6 +19,11 @@ package org.apache.solr.schema;
import org.apache.lucene.document.Fieldable;
import org.apache.lucene.document.Field;
import org.apache.lucene.search.*;
+import org.apache.lucene.search.cache.CachedArrayCreator;
+import org.apache.lucene.search.cache.DoubleValuesCreator;
+import org.apache.lucene.search.cache.FloatValuesCreator;
+import org.apache.lucene.search.cache.IntValuesCreator;
+import org.apache.lucene.search.cache.LongValuesCreator;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.NumericUtils;
import org.apache.lucene.analysis.TokenStream;
@@ -62,6 +67,7 @@ public class TrieField extends FieldType {
protected int precisionStepArg = TrieField.DEFAULT_PRECISION_STEP; // the one passed in or defaulted
protected int precisionStep; // normalized
protected TrieTypes type;
+ protected Object missingValue;
/**
* Used for handling date types following the same semantics as DateField
@@ -117,33 +123,71 @@ public class TrieField extends FieldType {
}
public SortField getSortField(SchemaField field, boolean top) {
+ int flags = CachedArrayCreator.CACHE_VALUES_AND_BITS;
+ Object missingValue = null;
+ boolean sortMissingLast = on( SORT_MISSING_LAST, properties );
+ boolean sortMissingFirst = on( SORT_MISSING_FIRST, properties );
+
switch (type) {
case INTEGER:
- return new SortField(field.getName(), FieldCache.NUMERIC_UTILS_INT_PARSER, top);
+ if( sortMissingLast ) {
+ missingValue = top ? Integer.MIN_VALUE : Integer.MAX_VALUE;
+ }
+ else if( sortMissingFirst ) {
+ missingValue = top ? Integer.MAX_VALUE : Integer.MIN_VALUE;
+ }
+ return new SortField( new IntValuesCreator( field.getName(),
+ FieldCache.NUMERIC_UTILS_INT_PARSER, flags ), top).setMissingValue( missingValue );
+
case FLOAT:
- return new SortField(field.getName(), FieldCache.NUMERIC_UTILS_FLOAT_PARSER, top);
+ if( sortMissingLast ) {
+ missingValue = top ? Float.NEGATIVE_INFINITY : Float.POSITIVE_INFINITY;
+ }
+ else if( sortMissingFirst ) {
+ missingValue = top ? Float.POSITIVE_INFINITY : Float.NEGATIVE_INFINITY;
+ }
+ return new SortField( new FloatValuesCreator( field.getName(),
+ FieldCache.NUMERIC_UTILS_FLOAT_PARSER, flags ), top).setMissingValue( missingValue );
+
case DATE: // fallthrough
case LONG:
- return new SortField(field.getName(), FieldCache.NUMERIC_UTILS_LONG_PARSER, top);
+ if( sortMissingLast ) {
+ missingValue = top ? Long.MIN_VALUE : Long.MAX_VALUE;
+ }
+ else if( sortMissingFirst ) {
+ missingValue = top ? Long.MAX_VALUE : Long.MIN_VALUE;
+ }
+ return new SortField( new LongValuesCreator( field.getName(),
+ FieldCache.NUMERIC_UTILS_LONG_PARSER, flags ), top).setMissingValue( missingValue );
+
case DOUBLE:
- return new SortField(field.getName(), FieldCache.NUMERIC_UTILS_DOUBLE_PARSER, top);
+ if( sortMissingLast ) {
+ missingValue = top ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY;
+ }
+ else if( sortMissingFirst ) {
+ missingValue = top ? Double.POSITIVE_INFINITY : Double.NEGATIVE_INFINITY;
+ }
+ return new SortField( new DoubleValuesCreator( field.getName(),
+ FieldCache.NUMERIC_UTILS_DOUBLE_PARSER, flags ), top).setMissingValue( missingValue );
+
default:
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unknown type for trie field: " + field.name);
}
}
public ValueSource getValueSource(SchemaField field) {
+ int flags = CachedArrayCreator.CACHE_VALUES_AND_BITS;
switch (type) {
case INTEGER:
- return new IntFieldSource(field.getName(), FieldCache.NUMERIC_UTILS_INT_PARSER);
+ return new IntFieldSource( new IntValuesCreator( field.getName(), FieldCache.NUMERIC_UTILS_INT_PARSER, flags ) );
case FLOAT:
- return new FloatFieldSource(field.getName(), FieldCache.NUMERIC_UTILS_FLOAT_PARSER);
+ return new FloatFieldSource( new FloatValuesCreator( field.getName(), FieldCache.NUMERIC_UTILS_FLOAT_PARSER, flags ));
case DATE:
- return new TrieDateFieldSource(field.getName(), FieldCache.NUMERIC_UTILS_LONG_PARSER);
+ return new TrieDateFieldSource( new LongValuesCreator( field.getName(), FieldCache.NUMERIC_UTILS_LONG_PARSER, flags ));
case LONG:
- return new LongFieldSource(field.getName(), FieldCache.NUMERIC_UTILS_LONG_PARSER);
+ return new LongFieldSource( new LongValuesCreator( field.getName(), FieldCache.NUMERIC_UTILS_LONG_PARSER, flags ) );
case DOUBLE:
- return new DoubleFieldSource(field.getName(), FieldCache.NUMERIC_UTILS_DOUBLE_PARSER);
+ return new DoubleFieldSource( new DoubleValuesCreator( field.getName(), FieldCache.NUMERIC_UTILS_DOUBLE_PARSER, flags ));
default:
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unknown type for trie field: " + field.name);
}
@@ -547,12 +591,8 @@ public class TrieField extends FieldType {
class TrieDateFieldSource extends LongFieldSource {
- public TrieDateFieldSource(String field, FieldCache.LongParser parser) {
- super(field, parser);
- }
-
- public TrieDateFieldSource(String field) {
- super(field);
+ public TrieDateFieldSource(LongValuesCreator creator) {
+ super(creator);
}
public String description() {
diff --git a/solr/src/java/org/apache/solr/search/function/ByteFieldSource.java b/solr/src/java/org/apache/solr/search/function/ByteFieldSource.java
index 53eb0b48b02..7729cc5d701 100644
--- a/solr/src/java/org/apache/solr/search/function/ByteFieldSource.java
+++ b/solr/src/java/org/apache/solr/search/function/ByteFieldSource.java
@@ -18,6 +18,11 @@ package org.apache.solr.search.function;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.FieldCache;
+import org.apache.lucene.search.cache.ByteValuesCreator;
+import org.apache.lucene.search.cache.LongValuesCreator;
+import org.apache.lucene.search.cache.CachedArray.ByteValues;
+import org.apache.lucene.search.cache.CachedArray.DoubleValues;
+import org.apache.lucene.search.cache.CachedArray.LongValues;
import java.io.IOException;
import java.util.Map;
@@ -30,16 +35,10 @@ import java.util.Map;
* @version $Id: IntFieldSource.java 555343 2007-07-11 17:46:25Z hossman $
*/
-public class ByteFieldSource extends FieldCacheSource {
- FieldCache.ByteParser parser;
+public class ByteFieldSource extends NumericFieldCacheSource<ByteValues> {
- public ByteFieldSource(String field) {
- this(field, null);
- }
-
- public ByteFieldSource(String field, FieldCache.ByteParser parser) {
- super(field);
- this.parser = parser;
+ public ByteFieldSource(ByteValuesCreator creator) {
+ super(creator);
}
public String description() {
@@ -47,9 +46,9 @@ public class ByteFieldSource extends FieldCacheSource {
}
public DocValues getValues(Map context, IndexReader reader) throws IOException {
- final byte[] arr = (parser == null) ?
- cache.getBytes(reader, field) :
- cache.getBytes(reader, field, parser);
+ final ByteValues vals = cache.getBytes(reader, field, creator);
+ final byte[] arr = vals.values;
+
return new DocValues() {
@Override
public byte byteVal(int doc) {
@@ -87,19 +86,4 @@ public class ByteFieldSource extends FieldCacheSource {
};
}
-
- public boolean equals(Object o) {
- if (o.getClass() != ByteFieldSource.class) return false;
- ByteFieldSource
- other = (ByteFieldSource) o;
- return super.equals(other)
- && this.parser == null ? other.parser == null :
- this.parser.getClass() == other.parser.getClass();
- }
-
- public int hashCode() {
- int h = parser == null ? Byte.class.hashCode() : parser.getClass().hashCode();
- h += super.hashCode();
- return h;
- }
}
\ No newline at end of file
diff --git a/solr/src/java/org/apache/solr/search/function/DoubleFieldSource.java b/solr/src/java/org/apache/solr/search/function/DoubleFieldSource.java
index 73249fac664..42f362d4c69 100644
--- a/solr/src/java/org/apache/solr/search/function/DoubleFieldSource.java
+++ b/solr/src/java/org/apache/solr/search/function/DoubleFieldSource.java
@@ -19,6 +19,10 @@ package org.apache.solr.search.function;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.FieldCache;
+import org.apache.lucene.search.cache.DoubleValuesCreator;
+import org.apache.lucene.search.cache.FloatValuesCreator;
+import org.apache.lucene.search.cache.CachedArray.DoubleValues;
+import org.apache.lucene.search.cache.CachedArray.FloatValues;
import org.apache.solr.search.MutableValue;
import org.apache.solr.search.MutableValueDouble;
@@ -33,16 +37,10 @@ import java.util.Map;
* @version $Id:$
*/
-public class DoubleFieldSource extends FieldCacheSource {
- protected FieldCache.DoubleParser parser;
+public class DoubleFieldSource extends NumericFieldCacheSource<DoubleValues> {
- public DoubleFieldSource(String field) {
- this(field, null);
- }
-
- public DoubleFieldSource(String field, FieldCache.DoubleParser parser) {
- super(field);
- this.parser = parser;
+ public DoubleFieldSource(DoubleValuesCreator creator) {
+ super(creator);
}
public String description() {
@@ -50,9 +48,9 @@ public class DoubleFieldSource extends FieldCacheSource {
}
public DocValues getValues(Map context, IndexReader reader) throws IOException {
- final double[] arr = (parser == null) ?
- ((FieldCache) cache).getDoubles(reader, field) :
- ((FieldCache) cache).getDoubles(reader, field, parser);
+ final DoubleValues vals = cache.getDoubles(reader, field, creator);
+ final double[] arr = vals.values;
+
return new DocValues() {
public float floatVal(int doc) {
return (float) arr[doc];
@@ -158,19 +156,4 @@ public class DoubleFieldSource extends FieldCacheSource {
};
}
-
- public boolean equals(Object o) {
- if (o.getClass() != DoubleFieldSource.class) return false;
- DoubleFieldSource other = (DoubleFieldSource) o;
- return super.equals(other)
- && this.parser == null ? other.parser == null :
- this.parser.getClass() == other.parser.getClass();
- }
-
- public int hashCode() {
- int h = parser == null ? Double.class.hashCode() : parser.getClass().hashCode();
- h += super.hashCode();
- return h;
- }
-
}
diff --git a/solr/src/java/org/apache/solr/search/function/FloatFieldSource.java b/solr/src/java/org/apache/solr/search/function/FloatFieldSource.java
index d4bb4abeb60..c6e34ce63ce 100644
--- a/solr/src/java/org/apache/solr/search/function/FloatFieldSource.java
+++ b/solr/src/java/org/apache/solr/search/function/FloatFieldSource.java
@@ -17,14 +17,14 @@
package org.apache.solr.search.function;
+import java.io.IOException;
+import java.util.Map;
+
import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.search.cache.FloatValuesCreator;
+import org.apache.lucene.search.cache.CachedArray.FloatValues;
import org.apache.solr.search.MutableValue;
import org.apache.solr.search.MutableValueFloat;
-import org.apache.solr.search.function.DocValues;
-import org.apache.lucene.search.FieldCache;
-
-import java.io.IOException;
-import java.util.Map;
/**
* Obtains float field values from the {@link org.apache.lucene.search.FieldCache}
@@ -34,16 +34,10 @@ import java.util.Map;
* @version $Id$
*/
-public class FloatFieldSource extends FieldCacheSource {
- protected FieldCache.FloatParser parser;
+public class FloatFieldSource extends NumericFieldCacheSource<FloatValues> {
- public FloatFieldSource(String field) {
- this(field, null);
- }
-
- public FloatFieldSource(String field, FieldCache.FloatParser parser) {
- super(field);
- this.parser = parser;
+ public FloatFieldSource(FloatValuesCreator creator) {
+ super(creator);
}
public String description() {
@@ -51,9 +45,9 @@ public class FloatFieldSource extends FieldCacheSource {
}
public DocValues getValues(Map context, IndexReader reader) throws IOException {
- final float[] arr = (parser==null) ?
- cache.getFloats(reader, field) :
- cache.getFloats(reader, field, parser);
+ final FloatValues vals = cache.getFloats(reader, field, creator);
+ final float[] arr = vals.values;
+
return new DocValues() {
public float floatVal(int doc) {
return arr[doc];
@@ -99,19 +93,4 @@ public class FloatFieldSource extends FieldCacheSource {
};
}
-
- public boolean equals(Object o) {
- if (o.getClass() != FloatFieldSource.class) return false;
- FloatFieldSource other = (FloatFieldSource)o;
- return super.equals(other)
- && this.parser==null ? other.parser==null :
- this.parser.getClass() == other.parser.getClass();
- }
-
- public int hashCode() {
- int h = parser==null ? Float.class.hashCode() : parser.getClass().hashCode();
- h += super.hashCode();
- return h;
- };
-
}
\ No newline at end of file
diff --git a/solr/src/java/org/apache/solr/search/function/IntFieldSource.java b/solr/src/java/org/apache/solr/search/function/IntFieldSource.java
index 8547044c00a..d8bfca61634 100644
--- a/solr/src/java/org/apache/solr/search/function/IntFieldSource.java
+++ b/solr/src/java/org/apache/solr/search/function/IntFieldSource.java
@@ -21,6 +21,11 @@ import org.apache.lucene.index.IndexReader;
import org.apache.solr.search.MutableValueInt;
import org.apache.solr.search.MutableValue;
import org.apache.lucene.search.FieldCache;
+import org.apache.lucene.search.cache.FloatValuesCreator;
+import org.apache.lucene.search.cache.IntValuesCreator;
+import org.apache.lucene.search.cache.CachedArray.DoubleValues;
+import org.apache.lucene.search.cache.CachedArray.FloatValues;
+import org.apache.lucene.search.cache.CachedArray.IntValues;
import java.io.IOException;
import java.util.Map;
@@ -32,16 +37,10 @@ import java.util.Map;
* @version $Id$
*/
-public class IntFieldSource extends FieldCacheSource {
- final FieldCache.IntParser parser;
+public class IntFieldSource extends NumericFieldCacheSource<IntValues> {
- public IntFieldSource(String field) {
- this(field, null);
- }
-
- public IntFieldSource(String field, FieldCache.IntParser parser) {
- super(field);
- this.parser = parser;
+ public IntFieldSource(IntValuesCreator creator) {
+ super(creator);
}
public String description() {
@@ -50,9 +49,9 @@ public class IntFieldSource extends FieldCacheSource {
public DocValues getValues(Map context, IndexReader reader) throws IOException {
- final int[] arr = (parser==null) ?
- cache.getInts(reader, field) :
- cache.getInts(reader, field, parser);
+ final IntValues vals = cache.getInts(reader, field, creator);
+ final int[] arr = vals.values;
+
return new DocValues() {
final MutableValueInt val = new MutableValueInt();
@@ -135,19 +134,4 @@ public class IntFieldSource extends FieldCacheSource {
};
}
-
- public boolean equals(Object o) {
- if (o.getClass() != IntFieldSource.class) return false;
- IntFieldSource other = (IntFieldSource)o;
- return super.equals(other)
- && this.parser==null ? other.parser==null :
- this.parser.getClass() == other.parser.getClass();
- }
-
- public int hashCode() {
- int h = parser==null ? Integer.class.hashCode() : parser.getClass().hashCode();
- h += super.hashCode();
- return h;
- };
-
}
diff --git a/solr/src/java/org/apache/solr/search/function/LongFieldSource.java b/solr/src/java/org/apache/solr/search/function/LongFieldSource.java
index 9056b8977bb..e96cad977f1 100644
--- a/solr/src/java/org/apache/solr/search/function/LongFieldSource.java
+++ b/solr/src/java/org/apache/solr/search/function/LongFieldSource.java
@@ -18,7 +18,8 @@
package org.apache.solr.search.function;
import org.apache.lucene.index.IndexReader;
-import org.apache.lucene.search.FieldCache;
+import org.apache.lucene.search.cache.LongValuesCreator;
+import org.apache.lucene.search.cache.CachedArray.LongValues;
import org.apache.solr.search.MutableValue;
import org.apache.solr.search.MutableValueLong;
@@ -34,31 +35,24 @@ import java.util.Map;
* @version $Id: FloatFieldSource.java 555343 2007-07-11 17:46:25Z hossman $
*/
-public class LongFieldSource extends FieldCacheSource {
- protected FieldCache.LongParser parser;
+public class LongFieldSource extends NumericFieldCacheSource<LongValues> {
- public LongFieldSource(String field) {
- this(field, null);
- }
-
- public LongFieldSource(String field, FieldCache.LongParser parser) {
- super(field);
- this.parser = parser;
+ public LongFieldSource(LongValuesCreator creator) {
+ super(creator);
}
public String description() {
return "long(" + field + ')';
}
-
public long externalToLong(String extVal) {
return Long.parseLong(extVal);
}
public DocValues getValues(Map context, IndexReader reader) throws IOException {
- final long[] arr = (parser == null) ?
- ((FieldCache) cache).getLongs(reader, field) :
- ((FieldCache) cache).getLongs(reader, field, parser);
+ final LongValues vals = cache.getLongs(reader, field, creator);
+ final long[] arr = vals.values;
+
return new DocValues() {
public float floatVal(int doc) {
return (float) arr[doc];
@@ -145,18 +139,4 @@ public class LongFieldSource extends FieldCacheSource {
return new MutableValueLong();
}
- public boolean equals(Object o) {
- if (o.getClass() != this.getClass()) return false;
- LongFieldSource other = (LongFieldSource) o;
- return super.equals(other)
- && this.parser == null ? other.parser == null :
- this.parser.getClass() == other.parser.getClass();
- }
-
- public int hashCode() {
- int h = parser == null ? this.getClass().hashCode() : parser.getClass().hashCode();
- h += super.hashCode();
- return h;
- }
-
}
diff --git a/solr/src/java/org/apache/solr/search/function/NumericFieldCacheSource.java b/solr/src/java/org/apache/solr/search/function/NumericFieldCacheSource.java
new file mode 100644
index 00000000000..9b69c43f346
--- /dev/null
+++ b/solr/src/java/org/apache/solr/search/function/NumericFieldCacheSource.java
@@ -0,0 +1,48 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.solr.search.function;
+
+import org.apache.lucene.search.cache.CachedArray;
+import org.apache.lucene.search.cache.CachedArrayCreator;
+
+/**
+ *
+ * @version $Id: FieldCacheSource.java 794328 2009-07-15 17:21:04Z shalin $
+ */
+public abstract class NumericFieldCacheSource<T extends CachedArray> extends FieldCacheSource {
+ protected final CachedArrayCreator<T> creator;
+
+ public NumericFieldCacheSource( CachedArrayCreator<T> creator ) {
+ super( creator.field );
+ this.creator = creator;
+ }
+
+ public final boolean equals(Object o) {
+ if (o.getClass() != this.getClass()) return false;
+ NumericFieldCacheSource other = (NumericFieldCacheSource) o;
+ return super.equals(other)
+ && this.creator == null ? other.creator == null :
+ this.creator.getClass() == other.creator.getClass();
+ }
+
+ public final int hashCode() {
+ int h = creator == null ? this.getClass().hashCode() : creator.getClass().hashCode();
+ h += super.hashCode();
+ return h;
+ }
+}
diff --git a/solr/src/java/org/apache/solr/search/function/ShortFieldSource.java b/solr/src/java/org/apache/solr/search/function/ShortFieldSource.java
index 8ae93c0945b..27384715122 100644
--- a/solr/src/java/org/apache/solr/search/function/ShortFieldSource.java
+++ b/solr/src/java/org/apache/solr/search/function/ShortFieldSource.java
@@ -17,6 +17,11 @@ package org.apache.solr.search.function;
*/
import org.apache.lucene.search.FieldCache;
+import org.apache.lucene.search.cache.FloatValuesCreator;
+import org.apache.lucene.search.cache.ShortValuesCreator;
+import org.apache.lucene.search.cache.CachedArray.FloatValues;
+import org.apache.lucene.search.cache.CachedArray.LongValues;
+import org.apache.lucene.search.cache.CachedArray.ShortValues;
import org.apache.lucene.index.IndexReader;
import java.io.IOException;
@@ -27,26 +32,21 @@ import java.util.Map;
*
*
**/
-public class ShortFieldSource extends FieldCacheSource{
- FieldCache.ShortParser parser;
+public class ShortFieldSource extends NumericFieldCacheSource<ShortValues> {
- public ShortFieldSource(String field) {
- this(field, null);
+ public ShortFieldSource(ShortValuesCreator creator) {
+ super(creator);
}
- public ShortFieldSource(String field, FieldCache.ShortParser parser) {
- super(field);
- this.parser = parser;
- }
public String description() {
return "short(" + field + ')';
}
public DocValues getValues(Map context, IndexReader reader) throws IOException {
- final short[] arr = (parser == null) ?
- cache.getShorts(reader, field) :
- cache.getShorts(reader, field, parser);
+ final ShortValues vals = cache.getShorts(reader, field, creator);
+ final short[] arr = vals.values;
+
return new DocValues() {
@Override
public byte byteVal(int doc) {
@@ -84,19 +84,4 @@ public class ShortFieldSource extends FieldCacheSource{
};
}
-
- public boolean equals(Object o) {
- if (o.getClass() != ShortFieldSource.class) return false;
- ShortFieldSource
- other = (ShortFieldSource) o;
- return super.equals(other)
- && this.parser == null ? other.parser == null :
- this.parser.getClass() == other.parser.getClass();
- }
-
- public int hashCode() {
- int h = parser == null ? Short.class.hashCode() : parser.getClass().hashCode();
- h += super.hashCode();
- return h;
- }
}
diff --git a/solr/src/test/org/apache/solr/schema/NumericFieldsTest.java b/solr/src/test/org/apache/solr/schema/NumericFieldsTest.java
new file mode 100644
index 00000000000..56a467e85a3
--- /dev/null
+++ b/solr/src/test/org/apache/solr/schema/NumericFieldsTest.java
@@ -0,0 +1,133 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.solr.schema;
+
+import org.apache.solr.SolrTestCaseJ4;
+import org.apache.solr.common.SolrInputDocument;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+
+public class NumericFieldsTest extends SolrTestCaseJ4 {
+ @BeforeClass
+ public static void beforeClass() throws Exception {
+ initCore("solrconfig-master.xml","schema-numeric.xml");
+ }
+
+ static String[] types = new String[] { "int", "long", "float", "double" };
+
+ public static SolrInputDocument getDoc( String id, Integer number )
+ {
+ SolrInputDocument doc = new SolrInputDocument();
+ doc.addField( "id", id );
+ for( String t : types ) {
+ doc.addField( t, number );
+ doc.addField( t+"_last", number );
+ doc.addField( t+"_first", number );
+ }
+ return doc;
+ }
+
+ @Test
+ public void testSortMissingFirstLast()
+ {
+ clearIndex();
+
+ assertU(adoc("id", "M1" ));
+ assertU(adoc( getDoc( "+4", 4 ) ));
+ assertU(adoc( getDoc( "+5", 5 ) ));
+ assertU(adoc( getDoc( "-3", -3 ) ));
+ assertU(adoc("id", "M2" ));
+ assertU(commit());
+
+ // 'normal' sorting. Missing Values are 0
+ String suffix = "";
+ for( String t : types ) {
+ assertQ( "Sorting Asc: "+t+suffix,
+ req("fl", "id", "q", "*:*", "sort", (t+suffix)+" asc" ),
+ "//*[@numFound='5']",
+ "//result/doc[1]/str[@name='id'][.='-3']",
+ "//result/doc[2]/str[@name='id'][.='M1']",
+ "//result/doc[3]/str[@name='id'][.='M2']",
+ "//result/doc[4]/str[@name='id'][.='+4']",
+ "//result/doc[5]/str[@name='id'][.='+5']"
+ );
+
+ assertQ( "Sorting Desc: "+t+suffix,
+ req("fl", "id", "q", "*:*", "sort", (t+suffix)+" desc" ),
+ "//*[@numFound='5']",
+ "//result/doc[1]/str[@name='id'][.='+5']",
+ "//result/doc[2]/str[@name='id'][.='+4']",
+ "//result/doc[3]/str[@name='id'][.='M1']",
+ "//result/doc[4]/str[@name='id'][.='M2']",
+ "//result/doc[5]/str[@name='id'][.='-3']"
+ );
+ }
+
+
+ // sortMissingLast = true
+ suffix = "_last";
+ for( String t : types ) {
+ assertQ( "Sorting Asc: "+t+suffix,
+ req("fl", "id", "q", "*:*", "sort", (t+suffix)+" asc" ),
+ "//*[@numFound='5']",
+ "//result/doc[1]/str[@name='id'][.='-3']",
+ "//result/doc[2]/str[@name='id'][.='+4']",
+ "//result/doc[3]/str[@name='id'][.='+5']",
+ "//result/doc[4]/str[@name='id'][.='M1']",
+ "//result/doc[5]/str[@name='id'][.='M2']"
+ );
+
+ // This does not match
+ assertQ( "Sorting Desc: "+t+suffix,
+ req("fl", "id", "q", "*:*", "sort", (t+suffix)+" desc", "indent", "on" ),
+ "//*[@numFound='5']",
+ "//result/doc[1]/str[@name='id'][.='+5']",
+ "//result/doc[2]/str[@name='id'][.='+4']",
+ "//result/doc[3]/str[@name='id'][.='-3']",
+ "//result/doc[4]/str[@name='id'][.='M1']",
+ "//result/doc[5]/str[@name='id'][.='M2']"
+ );
+ }
+
+ // sortMissingFirst = true
+ suffix = "_first";
+ for( String t : types ) {
+ assertQ( "Sorting Asc: "+t+suffix,
+ req("fl", "id", "q", "*:*", "sort", (t+suffix)+" asc", "indent", "on" ),
+ "//*[@numFound='5']",
+ "//result/doc[1]/str[@name='id'][.='M1']",
+ "//result/doc[2]/str[@name='id'][.='M2']",
+ "//result/doc[3]/str[@name='id'][.='-3']",
+ "//result/doc[4]/str[@name='id'][.='+4']",
+ "//result/doc[5]/str[@name='id'][.='+5']"
+ );
+
+ // This does not match
+ assertQ( "Sorting Desc: "+t+suffix,
+ req("fl", "id", "q", "*:*", "sort", (t+suffix)+" desc", "indent", "on" ),
+ "//*[@numFound='5']",
+ "//result/doc[1]/str[@name='id'][.='M1']",
+ "//result/doc[2]/str[@name='id'][.='M2']",
+ "//result/doc[3]/str[@name='id'][.='+5']",
+ "//result/doc[4]/str[@name='id'][.='+4']",
+ "//result/doc[5]/str[@name='id'][.='-3']"
+ );
+ }
+ }
+}
diff --git a/solr/src/test/test-files/solr/conf/schema-numeric.xml b/solr/src/test/test-files/solr/conf/schema-numeric.xml
new file mode 100644
index 00000000000..e52e4b95441
--- /dev/null
+++ b/solr/src/test/test-files/solr/conf/schema-numeric.xml
@@ -0,0 +1,72 @@
+<?xml version="1.0" ?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+
+<!-- The Solr schema file. This file should be named "schema.xml" and
+ should be located where the classloader for the Solr webapp can find it.
+
+ This schema is used for testing, and as such has everything and the
+ kitchen sink thrown in. See example/solr/conf/schema.xml for a
+ more concise example.
+
+ $Id: schema.xml 382610 2006-03-03 01:43:03Z yonik $
+ $Source: /cvs/main/searching/solr-configs/test/WEB-INF/classes/schema.xml,v $
+ $Name: $
+ -->
+
+<schema name="test" version="1.0">
+ <types>
+ <fieldtype name="string" class="solr.StrField" sortMissingLast="true"/>
+
+ <fieldType name="int" class="solr.TrieIntField" precisionStep="0" sortMissingLast="false" omitNorms="true" positionIncrementGap="0"/>
+ <fieldType name="float" class="solr.TrieFloatField" precisionStep="0" sortMissingLast="false" omitNorms="true" positionIncrementGap="0"/>
+ <fieldType name="long" class="solr.TrieLongField" precisionStep="0" sortMissingLast="false" omitNorms="true" positionIncrementGap="0"/>
+ <fieldType name="double" class="solr.TrieDoubleField" precisionStep="0" sortMissingLast="false" omitNorms="true" positionIncrementGap="0"/>
+
+ <fieldType name="int_last" class="solr.TrieIntField" precisionStep="0" sortMissingLast="true" omitNorms="true" positionIncrementGap="0"/>
+ <fieldType name="float_last" class="solr.TrieFloatField" precisionStep="0" sortMissingLast="true" omitNorms="true" positionIncrementGap="0"/>
+ <fieldType name="long_last" class="solr.TrieLongField" precisionStep="0" sortMissingLast="true" omitNorms="true" positionIncrementGap="0"/>
+ <fieldType name="double_last" class="solr.TrieDoubleField" precisionStep="0" sortMissingLast="true" omitNorms="true" positionIncrementGap="0"/>
+
+ <fieldType name="int_first" class="solr.TrieIntField" precisionStep="0" sortMissingFirst="true" omitNorms="true" positionIncrementGap="0"/>
+ <fieldType name="float_first" class="solr.TrieFloatField" precisionStep="0" sortMissingFirst="true" omitNorms="true" positionIncrementGap="0"/>
+ <fieldType name="long_first" class="solr.TrieLongField" precisionStep="0" sortMissingFirst="true" omitNorms="true" positionIncrementGap="0"/>
+ <fieldType name="double_first" class="solr.TrieDoubleField" precisionStep="0" sortMissingFirst="true" omitNorms="true" positionIncrementGap="0"/>
+
+ </types>
+
+ <fields>
+ <field name="id" type="string" indexed="true" stored="true" multiValued="false" required="true"/>
+
+ <field name="int" type="int" indexed="true" stored="true" multiValued="false"/>
+ <field name="float" type="float" indexed="true" stored="true" multiValued="false"/>
+ <field name="long" type="long" indexed="true" stored="true" multiValued="false"/>
+ <field name="double" type="double" indexed="true" stored="true" multiValued="false"/>
+
+ <field name="int_last" type="int_last" indexed="true" stored="true" multiValued="false"/>
+ <field name="float_last" type="float_last" indexed="true" stored="true" multiValued="false"/>
+ <field name="long_last" type="long_last" indexed="true" stored="true" multiValued="false"/>
+ <field name="double_last" type="double_last" indexed="true" stored="true" multiValued="false"/>
+
+ <field name="int_first" type="int_first" indexed="true" stored="true" multiValued="false"/>
+ <field name="float_first" type="float_first" indexed="true" stored="true" multiValued="false"/>
+ <field name="long_first" type="long_first" indexed="true" stored="true" multiValued="false"/>
+ <field name="double_first" type="double_first" indexed="true" stored="true" multiValued="false"/>
+
+ </fields>
+
+</schema>