Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

SOLR-482: add byte/short sorting support

Grant Ingersoll6b251c96856a
diff --git a/CHANGES.txt b/CHANGES.txt
index 98f3fecd94a..d85ed18f5ee 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -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
diff --git a/src/java/org/apache/solr/request/JSONResponseWriter.java b/src/java/org/apache/solr/request/JSONResponseWriter.java
index 3dd0638e2c5..202e05e40ff 100644
--- a/src/java/org/apache/solr/request/JSONResponseWriter.java
+++ b/src/java/org/apache/solr/request/JSONResponseWriter.java
@@ -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
diff --git a/src/java/org/apache/solr/request/TextResponseWriter.java b/src/java/org/apache/solr/request/TextResponseWriter.java
index 9b6971106a3..f7a21c6638e 100644
--- a/src/java/org/apache/solr/request/TextResponseWriter.java
+++ b/src/java/org/apache/solr/request/TextResponseWriter.java
@@ -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));
+ }
}
diff --git a/src/java/org/apache/solr/request/XMLWriter.java b/src/java/org/apache/solr/request/XMLWriter.java
index fb1df1022da..1735159f9ca 100644
--- a/src/java/org/apache/solr/request/XMLWriter.java
+++ b/src/java/org/apache/solr/request/XMLWriter.java
@@ -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);
}
diff --git a/src/java/org/apache/solr/schema/ByteField.java b/src/java/org/apache/solr/schema/ByteField.java
new file mode 100644
index 00000000000..e442642050a
--- /dev/null
+++ b/src/java/org/apache/solr/schema/ByteField.java
@@ -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));
+ }
+}
\ No newline at end of file
diff --git a/src/java/org/apache/solr/schema/ShortField.java b/src/java/org/apache/solr/schema/ShortField.java
new file mode 100644
index 00000000000..6240ec64c80
--- /dev/null
+++ b/src/java/org/apache/solr/schema/ShortField.java
@@ -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));
+ }
+
+}
diff --git a/src/java/org/apache/solr/search/function/ByteFieldSource.java b/src/java/org/apache/solr/search/function/ByteFieldSource.java
new file mode 100644
index 00000000000..d59f0c310a7
--- /dev/null
+++ b/src/java/org/apache/solr/search/function/ByteFieldSource.java
@@ -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;
+ }
+}
\ No newline at end of file
diff --git a/src/java/org/apache/solr/search/function/ShortFieldSource.java b/src/java/org/apache/solr/search/function/ShortFieldSource.java
new file mode 100644
index 00000000000..517c135971b
--- /dev/null
+++ b/src/java/org/apache/solr/search/function/ShortFieldSource.java
@@ -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;
+ }
+}