Apache Solr open-source search software
SOLR-324: fixed support for longs/doubles, still need to add short and byte, per next Lucene update from trunk.
Grant Ingersoll427cbbfb932d
diff --git a/CHANGES.txt b/CHANGES.txtindex 2e4ddadf823..dff0eab6f37 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -255,6 +255,8 @@ Bug Fixes17. SOLR-481: Handle UnknownHostException in _info.jsp (gsingers)+18. SOLR-324: Add proper support for Long and Doubles in sorting, etc. (gsingers)+Other Changes1. SOLR-135: Moved common classes to org.apache.solr.common and altered thebuild scripts to make two jars: apache-solr-1.3.jar anddiff --git a/src/java/org/apache/solr/schema/DoubleField.java b/src/java/org/apache/solr/schema/DoubleField.javaindex 09aba0a7eb5..7104742d4b8 100644--- a/src/java/org/apache/solr/schema/DoubleField.java+++ b/src/java/org/apache/solr/schema/DoubleField.java@@ -17,32 +17,32 @@package org.apache.solr.schema;-import org.apache.lucene.search.SortField;-import org.apache.solr.search.function.ValueSource;-import org.apache.solr.search.function.FloatFieldSource;import org.apache.lucene.document.Fieldable;-import org.apache.solr.request.XMLWriter;+import org.apache.lucene.search.SortField;import org.apache.solr.request.TextResponseWriter;+import org.apache.solr.request.XMLWriter;+import org.apache.solr.search.function.DoubleFieldSource;+import org.apache.solr.search.function.ValueSource;-import java.util.Map;import java.io.IOException;+import java.util.Map;+/*** @version $Id$*/public class DoubleField extends FieldType {- protected void init(IndexSchema schema, Map<String,String> args) {+ protected void init(IndexSchema schema, Map<String, String> args) {restrictProps(SORT_MISSING_FIRST | SORT_MISSING_LAST);}/////////////////////////////////////////////////////////////- // TODO: ACK.. there is currently no SortField.DOUBLE!- public SortField getSortField(SchemaField field,boolean reverse) {- return new SortField(field.name,SortField.FLOAT, reverse);+ public SortField getSortField(SchemaField field, boolean reverse) {+ return new SortField(field.name, SortField.DOUBLE, reverse);}public ValueSource getValueSource(SchemaField field) {// fieldCache doesn't support double- return new FloatFieldSource(field.name);+ return new DoubleFieldSource(field.name);}public void write(XMLWriter xmlWriter, String name, Fieldable f) throws IOException {@@ -52,10 +52,10 @@ public class DoubleField extends FieldType {public void write(TextResponseWriter writer, String name, Fieldable f) throws IOException {writer.writeDouble(name, f.stringValue());}-+@Overridepublic Double toObject(Fieldable f) {- return Double.valueOf( toExternal(f) );+ return Double.valueOf(toExternal(f));}}diff --git a/src/java/org/apache/solr/schema/LongField.java b/src/java/org/apache/solr/schema/LongField.javaindex 159d6213290..0de644a17d6 100644--- a/src/java/org/apache/solr/schema/LongField.java+++ b/src/java/org/apache/solr/schema/LongField.java@@ -17,15 +17,16 @@package org.apache.solr.schema;-import org.apache.lucene.search.SortField;-import org.apache.solr.search.function.ValueSource;-import org.apache.solr.search.function.IntFieldSource;import org.apache.lucene.document.Fieldable;-import org.apache.solr.request.XMLWriter;+import org.apache.lucene.search.SortField;import org.apache.solr.request.TextResponseWriter;+import org.apache.solr.request.XMLWriter;+import org.apache.solr.search.function.IntFieldSource;+import org.apache.solr.search.function.ValueSource;+import org.apache.solr.search.function.LongFieldSource;-import java.util.Map;import java.io.IOException;+import java.util.Map;/*** @version $Id$*/@@ -35,15 +36,15 @@ public class LongField extends FieldType {}/////////////////////////////////////////////////////////////- // TODO: ACK.. there is no SortField.LONG!+public SortField getSortField(SchemaField field,boolean reverse) {- // todo - log warning- return new SortField(field.name,SortField.INT, reverse);++ return new SortField(field.name,SortField.LONG, reverse);}public ValueSource getValueSource(SchemaField field) {- // todo - log warning- return new IntFieldSource(field.name);++ return new LongFieldSource(field.name);}diff --git a/src/java/org/apache/solr/search/function/DocValues.java b/src/java/org/apache/solr/search/function/DocValues.javaindex 00103b070f1..20668045980 100644--- a/src/java/org/apache/solr/search/function/DocValues.java+++ b/src/java/org/apache/solr/search/function/DocValues.java@@ -33,6 +33,9 @@ import org.apache.lucene.search.Explanation;// - For caching, Query objects are often used as keys... you don't// want the Query carrying around big objectspublic abstract class DocValues {+ public byte byteVal(int doc) { throw new UnsupportedOperationException(); }+ public short shortVal(int doc) { throw new UnsupportedOperationException(); }+public float floatVal(int doc) { throw new UnsupportedOperationException(); }public int intVal(int doc) { throw new UnsupportedOperationException(); }public long longVal(int doc) { throw new UnsupportedOperationException(); }diff --git a/src/java/org/apache/solr/search/function/DoubleFieldSource.java b/src/java/org/apache/solr/search/function/DoubleFieldSource.javanew file mode 100644index 00000000000..9cfd6f9e2ca--- /dev/null+++ b/src/java/org/apache/solr/search/function/DoubleFieldSource.java@@ -0,0 +1,96 @@+/**+ * 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.index.IndexReader;+import org.apache.lucene.search.ExtendedFieldCache;++import java.io.IOException;++/**+ * Obtains float field values from the {@link org.apache.lucene.search.FieldCache}+ * using <code>getFloats()</code>+ * and makes those values available as other numeric types, casting as needed.+ *+ * @version $Id:$+ */++public class DoubleFieldSource extends FieldCacheSource {+ protected ExtendedFieldCache.DoubleParser parser;++ public DoubleFieldSource(String field) {+ this(field, null);+ }++ public DoubleFieldSource(String field, ExtendedFieldCache.DoubleParser parser) {+ super(field);+ this.parser = parser;+ }++ public String description() {+ return "double(" + field + ')';+ }++ public DocValues getValues(IndexReader reader) throws IOException {+ final double[] arr = (parser == null) ?+ ((ExtendedFieldCache) cache).getDoubles(reader, field) :+ ((ExtendedFieldCache) cache).getDoubles(reader, field, parser);+ return new DocValues() {+ public float floatVal(int doc) {+ return (float) arr[doc];+ }++ public int intVal(int doc) {+ return (int) arr[doc];+ }++ public long longVal(int doc) {+ return (long) arr[doc];+ }++ public double doubleVal(int doc) {+ return arr[doc];+ }++ public String strVal(int doc) {+ return Double.toString(arr[doc]);+ }++ public String toString(int doc) {+ return description() + '=' + floatVal(doc);+ }+ };+ }++ 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 ? Float.class.hashCode() : parser.getClass().hashCode();+ h += super.hashCode();+ return h;+ }++ ;++}\ No newline at end of filediff --git a/src/java/org/apache/solr/search/function/FieldCacheSource.java b/src/java/org/apache/solr/search/function/FieldCacheSource.javaindex 975d5ea1a96..419645f2d99 100644--- a/src/java/org/apache/solr/search/function/FieldCacheSource.java+++ b/src/java/org/apache/solr/search/function/FieldCacheSource.java@@ -18,6 +18,7 @@package org.apache.solr.search.function;import org.apache.lucene.search.FieldCache;+import org.apache.lucene.search.ExtendedFieldCache;/*** A base class for ValueSource implementations that retrieve values for@@ -27,16 +28,23 @@ import org.apache.lucene.search.FieldCache;*/public abstract class FieldCacheSource extends ValueSource {protected String field;- protected FieldCache cache = FieldCache.DEFAULT;+ protected FieldCache cache = ExtendedFieldCache.EXT_DEFAULT;public FieldCacheSource(String field) {this.field=field;}+ /**+ * If you are using longs or doubles, this needs to be a {@link org.apache.lucene.search.ExtendedFieldCache}.+ *+ * @param cache The {@link org.apache.lucene.search.FieldCache}+ */public void setFieldCache(FieldCache cache) {this.cache = cache;}++public FieldCache getFieldCache() {return cache;}diff --git a/src/java/org/apache/solr/search/function/LongFieldSource.java b/src/java/org/apache/solr/search/function/LongFieldSource.javanew file mode 100644index 00000000000..2ad7d19f0a6--- /dev/null+++ b/src/java/org/apache/solr/search/function/LongFieldSource.java@@ -0,0 +1,97 @@+/**+ * 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.index.IndexReader;+import org.apache.lucene.search.ExtendedFieldCache;+++import java.io.IOException;++/**+ * Obtains float field values from the {@link org.apache.lucene.search.FieldCache}+ * using <code>getFloats()</code>+ * and makes those values available as other numeric types, casting as needed.+ *+ * @version $Id: FloatFieldSource.java 555343 2007-07-11 17:46:25Z hossman $+ */++public class LongFieldSource extends FieldCacheSource {+ protected ExtendedFieldCache.LongParser parser;++ public LongFieldSource(String field) {+ this(field, null);+ }++ public LongFieldSource(String field, ExtendedFieldCache.LongParser parser) {+ super(field);+ this.parser = parser;+ }++ public String description() {+ return "long(" + field + ')';+ }++ public DocValues getValues(IndexReader reader) throws IOException {+ final long[] arr = (parser == null) ?+ ((ExtendedFieldCache) cache).getLongs(reader, field) :+ ((ExtendedFieldCache) cache).getLongs(reader, field, parser);+ return new DocValues() {+ public float floatVal(int doc) {+ return (float) arr[doc];+ }++ public int intVal(int doc) {+ return (int) arr[doc];+ }++ public long longVal(int doc) {+ return (long) arr[doc];+ }++ public double doubleVal(int doc) {+ return arr[doc];+ }++ public String strVal(int doc) {+ return Double.toString(arr[doc]);+ }++ public String toString(int doc) {+ return description() + '=' + floatVal(doc);+ }+ };+ }++ public boolean equals(Object o) {+ if (o.getClass() != LongFieldSource.class) 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 ? Float.class.hashCode() : parser.getClass().hashCode();+ h += super.hashCode();+ return h;+ }++ ;++}\ No newline at end of file