2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
18
package org.apache.solr.schema;
20
import org.apache.lucene.search.SortField;
21
import org.apache.solr.search.QParser;
22
import org.apache.solr.search.function.ValueSource;
23
import org.apache.solr.search.function.FieldCacheSource;
24
import org.apache.solr.search.function.DocValues;
25
import org.apache.solr.search.function.StringIndexDocValues;
26
import org.apache.lucene.document.Fieldable;
27
import org.apache.lucene.index.IndexReader;
28
import org.apache.solr.util.NumberUtils;
29
import org.apache.solr.response.TextResponseWriter;
30
import org.apache.solr.response.XMLWriter;
33
import java.io.IOException;
35
* @version $Id: SortableLongField.java 1071480 2011-02-17 02:23:10Z hossman $
37
public class SortableLongField extends FieldType {
39
protected void init(IndexSchema schema, Map<String,String> args) {
43
public SortField getSortField(SchemaField field,boolean reverse) {
44
return getStringSort(field,reverse);
48
public ValueSource getValueSource(SchemaField field, QParser qparser) {
49
field.checkFieldCacheSource(qparser);
50
return new SortableLongFieldSource(field.name);
54
public String toInternal(String val) {
55
return NumberUtils.long2sortableStr(val);
59
public String indexedToReadable(String indexedForm) {
60
return NumberUtils.SortableStr2long(indexedForm);
64
public String toExternal(Fieldable f) {
65
return indexedToReadable(f.stringValue());
69
public Long toObject(Fieldable f) {
70
return NumberUtils.SortableStr2long(f.stringValue(),0,5);
74
public void write(XMLWriter xmlWriter, String name, Fieldable f) throws IOException {
75
String sval = f.stringValue();
76
xmlWriter.writeLong(name, NumberUtils.SortableStr2long(sval,0,sval.length()));
80
public void write(TextResponseWriter writer, String name, Fieldable f) throws IOException {
81
String sval = f.stringValue();
82
writer.writeLong(name, NumberUtils.SortableStr2long(sval,0,sval.length()));
90
class SortableLongFieldSource extends FieldCacheSource {
91
protected long defVal;
93
public SortableLongFieldSource(String field) {
97
public SortableLongFieldSource(String field, long defVal) {
103
public String description() {
104
return "slong(" + field + ')';
108
public DocValues getValues(Map context, IndexReader reader) throws IOException {
109
final long def = defVal;
111
return new StringIndexDocValues(this, reader, field) {
113
protected String toTerm(String readableValue) {
114
return NumberUtils.long2sortableStr(readableValue);
118
public float floatVal(int doc) {
119
return (float)longVal(doc);
123
public int intVal(int doc) {
124
return (int)longVal(doc);
128
public long longVal(int doc) {
130
return ord==0 ? def : NumberUtils.SortableStr2long(lookup[ord],0,5);
134
public double doubleVal(int doc) {
135
return (double)longVal(doc);
139
public String strVal(int doc) {
140
return Long.toString(longVal(doc));
144
public String toString(int doc) {
145
return description() + '=' + longVal(doc);
151
public boolean equals(Object o) {
152
return o instanceof SortableLongFieldSource
154
&& defVal == ((SortableLongFieldSource)o).defVal;
157
private static int hcode = SortableLongFieldSource.class.hashCode();
159
public int hashCode() {
160
return hcode + super.hashCode() + (int)defVal;