SOLR-324: fixed support for longs/doubles, still need to add short and byte, per next Lucene update from trunk.

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@629334 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Grant Ingersoll 2008-02-20 03:36:49 +00:00
parent 191e72edde
commit 427cbbfb93
7 changed files with 230 additions and 23 deletions

View File

@ -255,6 +255,8 @@ Bug Fixes
17. SOLR-481: Handle UnknownHostException in _info.jsp (gsingers)
18. SOLR-324: Add proper support for Long and Doubles in sorting, etc. (gsingers)
Other Changes
1. SOLR-135: Moved common classes to org.apache.solr.common and altered the
build scripts to make two jars: apache-solr-1.3.jar and

View File

@ -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 {
@ -56,6 +56,6 @@ public class DoubleField extends FieldType {
@Override
public Double toObject(Fieldable f) {
return Double.valueOf( toExternal(f) );
return Double.valueOf(toExternal(f));
}
}

View File

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

View File

@ -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 objects
public 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(); }

View File

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

View File

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

View File

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