1
package org.apache.solr.schema;
3
* Licensed to the Apache Software Foundation (ASF) under one or more
4
* contributor license agreements. See the NOTICE file distributed with
5
* this work for additional information regarding copyright ownership.
6
* The ASF licenses this file to You under the Apache License, Version 2.0
7
* (the "License"); you may not use this file except in compliance with
8
* the License. You may obtain a copy of the License at
10
* http://www.apache.org/licenses/LICENSE-2.0
12
* Unless required by applicable law or agreed to in writing, software
13
* distributed under the License is distributed on an "AS IS" BASIS,
14
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
* See the License for the specific language governing permissions and
16
* limitations under the License.
19
import org.apache.lucene.document.Fieldable;
20
import org.apache.lucene.search.SortField;
22
import org.apache.solr.response.TextResponseWriter;
23
import org.apache.solr.response.XMLWriter;
24
import org.apache.solr.search.function.ValueSource;
25
import org.apache.solr.search.function.ByteFieldSource;
26
import org.apache.solr.search.QParser;
28
import java.io.IOException;
34
public class ByteField extends FieldType {
36
protected void init(IndexSchema schema, Map<String, String> args) {
37
restrictProps(SORT_MISSING_FIRST | SORT_MISSING_LAST);
40
/////////////////////////////////////////////////////////////
42
public SortField getSortField(SchemaField field, boolean reverse) {
43
field.checkSortability();
44
return new SortField(field.name, SortField.BYTE, reverse);
48
public ValueSource getValueSource(SchemaField field, QParser qparser) {
49
field.checkFieldCacheSource(qparser);
50
return new ByteFieldSource(field.name);
55
public void write(XMLWriter xmlWriter, String name, Fieldable f) throws IOException {
56
xmlWriter.writeByte(name, f.stringValue());
60
public void write(TextResponseWriter writer, String name, Fieldable f) throws IOException {
61
String s = f.stringValue();
63
// these values may be from a legacy lucene index, which may
64
// not be properly formatted in some output formats, or may
65
// incorrectly have a zero length.
68
// zero length value means someone mistakenly indexed the value
69
// instead of simply leaving it out. Write a null value instead of a numeric.
70
writer.writeNull(name);
75
byte val = Byte.parseByte(s);
76
writer.writeByte(name, val);
77
} catch (NumberFormatException e){
78
// can't parse - write out the contents as a string so nothing is lost and
79
// clients don't get a parse error.
80
writer.writeStr(name, s, true);
85
public Byte toObject(Fieldable f) {
86
return Byte.valueOf(toExternal(f));