mirror of https://github.com/apache/lucene.git
SOLR-482: add byte/short sorting support
git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@673221 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
bd9fa2fff7
commit
6b251c9685
|
@ -300,6 +300,8 @@ New Features
|
|||
54. SOLR-423: Added Request Handler close hook notification so that RequestHandlers can be notified when a core is closing. (gsingers, ryan)
|
||||
|
||||
55. SOLR-603: Added ability to partially optimize. (gsingers)
|
||||
|
||||
56. SOLR-483: Add byte/short sorting support (gsingers)
|
||||
|
||||
Changes in runtime behavior
|
||||
1. SOLR-559: use Lucene updateDocument, deleteDocuments methods. This
|
||||
|
|
|
@ -719,6 +719,16 @@ class JSONWriter extends TextResponseWriter {
|
|||
writer.write(val);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeShort(String name, String val) throws IOException {
|
||||
writer.write(val);
|
||||
}
|
||||
|
||||
public void writeByte(String name, String val) throws IOException {
|
||||
writer.write(val);
|
||||
|
||||
}
|
||||
|
||||
// TODO: refactor this out to a DateUtils class or something...
|
||||
public void writeDate(String name, Date val) throws IOException {
|
||||
// using a stringBuilder for numbers can be nice since
|
||||
|
|
|
@ -228,4 +228,16 @@ public abstract class TextResponseWriter {
|
|||
|
||||
/** if this form of the method is called, val is the Solr ISO8601 based date format */
|
||||
public abstract void writeDate(String name, String val) throws IOException;
|
||||
|
||||
public abstract void writeShort(String name, String val) throws IOException;
|
||||
|
||||
public void writeShort(String name, short val) throws IOException{
|
||||
writeShort(name, Short.toString(val));
|
||||
}
|
||||
|
||||
public abstract void writeByte(String name, String s) throws IOException;
|
||||
|
||||
public void writeByte(String name, byte val) throws IOException{
|
||||
writeByte(name, Byte.toString(val));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -640,6 +640,24 @@ final public class XMLWriter {
|
|||
writeBool(name,Boolean.toString(val));
|
||||
}
|
||||
|
||||
public void writeShort(String name, String val) throws IOException {
|
||||
writePrim("short",name,val,false);
|
||||
}
|
||||
|
||||
public void writeShort(String name, short val) throws IOException {
|
||||
writeInt(name,Short.toString(val));
|
||||
}
|
||||
|
||||
|
||||
public void writeByte(String name, String val) throws IOException {
|
||||
writePrim("byte",name,val,false);
|
||||
}
|
||||
|
||||
public void writeByte(String name, byte val) throws IOException {
|
||||
writeInt(name,Byte.toString(val));
|
||||
}
|
||||
|
||||
|
||||
public void writeFloat(String name, String val) throws IOException {
|
||||
writePrim("float",name,val,false);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,60 @@
|
|||
package org.apache.solr.schema;
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
||||
import org.apache.lucene.document.Fieldable;
|
||||
import org.apache.lucene.search.SortField;
|
||||
|
||||
import org.apache.solr.request.TextResponseWriter;
|
||||
import org.apache.solr.request.XMLWriter;
|
||||
import org.apache.solr.search.function.ValueSource;
|
||||
import org.apache.solr.search.function.ByteFieldSource;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @version $Id: LongField.java 555343 2007-07-11 17:46:25Z hossman $
|
||||
*/
|
||||
public class ByteField extends FieldType {
|
||||
protected void init(IndexSchema schema, Map<String, String> args) {
|
||||
restrictProps(SORT_MISSING_FIRST | SORT_MISSING_LAST);
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////
|
||||
public SortField getSortField(SchemaField field, boolean reverse) {
|
||||
return new SortField(field.name, SortField.BYTE, reverse);
|
||||
}
|
||||
|
||||
public ValueSource getValueSource(SchemaField field) {
|
||||
return new ByteFieldSource(field.name);
|
||||
}
|
||||
|
||||
|
||||
public void write(XMLWriter xmlWriter, String name, Fieldable f) throws IOException {
|
||||
xmlWriter.writeByte(name, f.stringValue());
|
||||
}
|
||||
|
||||
public void write(TextResponseWriter writer, String name, Fieldable f) throws IOException {
|
||||
writer.writeByte(name, f.stringValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Byte toObject(Fieldable f) {
|
||||
return Byte.valueOf(toExternal(f));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
package org.apache.solr.schema;
|
||||
|
||||
import org.apache.lucene.document.Fieldable;
|
||||
import org.apache.lucene.search.SortField;
|
||||
|
||||
import org.apache.solr.request.TextResponseWriter;
|
||||
import org.apache.solr.request.XMLWriter;
|
||||
import org.apache.solr.search.function.ValueSource;
|
||||
import org.apache.solr.search.function.ShortFieldSource;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
**/
|
||||
public class ShortField extends FieldType {
|
||||
protected void init(IndexSchema schema, Map<String, String> args) {
|
||||
restrictProps(SORT_MISSING_FIRST | SORT_MISSING_LAST);
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////
|
||||
|
||||
public SortField getSortField(SchemaField field, boolean reverse) {
|
||||
|
||||
return new SortField(field.name, SortField.SHORT, reverse);
|
||||
}
|
||||
|
||||
public ValueSource getValueSource(SchemaField field) {
|
||||
|
||||
return new ShortFieldSource(field.name);
|
||||
}
|
||||
|
||||
|
||||
public void write(XMLWriter xmlWriter, String name, Fieldable f) throws IOException {
|
||||
xmlWriter.writeShort(name, f.stringValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(TextResponseWriter writer, String name, Fieldable f) throws IOException {
|
||||
writer.writeShort(name, f.stringValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Short toObject(Fieldable f) {
|
||||
return Short.valueOf(toExternal(f));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,104 @@
|
|||
package org.apache.solr.search.function;
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
||||
import org.apache.lucene.index.IndexReader;
|
||||
import org.apache.lucene.search.FieldCache;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Obtains int field values from the {@link org.apache.lucene.search.FieldCache}
|
||||
* using <code>getInts()</code>
|
||||
* and makes those values available as other numeric types, casting as needed. *
|
||||
*
|
||||
* @version $Id: IntFieldSource.java 555343 2007-07-11 17:46:25Z hossman $
|
||||
*/
|
||||
|
||||
public class ByteFieldSource extends FieldCacheSource {
|
||||
FieldCache.ByteParser parser;
|
||||
|
||||
public ByteFieldSource(String field) {
|
||||
this(field, null);
|
||||
}
|
||||
|
||||
public ByteFieldSource(String field, FieldCache.ByteParser parser) {
|
||||
super(field);
|
||||
this.parser = parser;
|
||||
}
|
||||
|
||||
public String description() {
|
||||
return "byte(" + field + ')';
|
||||
}
|
||||
|
||||
public DocValues getValues(IndexReader reader) throws IOException {
|
||||
final byte[] arr = (parser == null) ?
|
||||
cache.getBytes(reader, field) :
|
||||
cache.getBytes(reader, field, parser);
|
||||
return new DocValues() {
|
||||
@Override
|
||||
public byte byteVal(int doc) {
|
||||
return (byte) arr[doc];
|
||||
}
|
||||
|
||||
@Override
|
||||
public short shortVal(int doc) {
|
||||
return (short) arr[doc];
|
||||
}
|
||||
|
||||
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 (double) arr[doc];
|
||||
}
|
||||
|
||||
public String strVal(int doc) {
|
||||
return Byte.toString(arr[doc]);
|
||||
}
|
||||
|
||||
public String toString(int doc) {
|
||||
return description() + '=' + byteVal(doc);
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,85 @@
|
|||
package org.apache.solr.search.function;
|
||||
|
||||
import org.apache.lucene.search.FieldCache;
|
||||
import org.apache.lucene.index.IndexReader;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
**/
|
||||
public class ShortFieldSource extends FieldCacheSource{
|
||||
FieldCache.ShortParser parser;
|
||||
|
||||
public ShortFieldSource(String field) {
|
||||
this(field, null);
|
||||
}
|
||||
|
||||
public ShortFieldSource(String field, FieldCache.ShortParser parser) {
|
||||
super(field);
|
||||
this.parser = parser;
|
||||
}
|
||||
|
||||
public String description() {
|
||||
return "short(" + field + ')';
|
||||
}
|
||||
|
||||
public DocValues getValues(IndexReader reader) throws IOException {
|
||||
final short[] arr = (parser == null) ?
|
||||
cache.getShorts(reader, field) :
|
||||
cache.getShorts(reader, field, parser);
|
||||
return new DocValues() {
|
||||
@Override
|
||||
public byte byteVal(int doc) {
|
||||
return (byte) arr[doc];
|
||||
}
|
||||
|
||||
@Override
|
||||
public short shortVal(int doc) {
|
||||
return (short) arr[doc];
|
||||
}
|
||||
|
||||
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 (double) arr[doc];
|
||||
}
|
||||
|
||||
public String strVal(int doc) {
|
||||
return Short.toString(arr[doc]);
|
||||
}
|
||||
|
||||
public String toString(int doc) {
|
||||
return description() + '=' + shortVal(doc);
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue