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.OrdFieldSource;
24
import org.apache.lucene.analysis.Analyzer;
25
import org.apache.lucene.analysis.Tokenizer;
26
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
27
import org.apache.lucene.document.Fieldable;
28
import org.apache.solr.response.TextResponseWriter;
29
import org.apache.solr.response.XMLWriter;
30
import org.apache.solr.analysis.SolrAnalyzer;
33
import java.io.Reader;
34
import java.io.IOException;
36
* @version $Id: BoolField.java 1071480 2011-02-17 02:23:10Z hossman $
38
public class BoolField extends FieldType {
40
protected void init(IndexSchema schema, Map<String,String> args) {
44
public SortField getSortField(SchemaField field,boolean reverse) {
45
field.checkSortability();
46
return getStringSort(field,reverse);
50
public ValueSource getValueSource(SchemaField field, QParser qparser) {
51
field.checkFieldCacheSource(qparser);
52
return new OrdFieldSource(field.name);
55
// avoid instantiating every time...
56
protected final static char[] TRUE_TOKEN = {'T'};
57
protected final static char[] FALSE_TOKEN = {'F'};
59
////////////////////////////////////////////////////////////////////////
60
// TODO: look into creating my own queryParser that can more efficiently
61
// handle single valued non-text fields (int,bool,etc) if needed.
63
protected final static Analyzer boolAnalyzer = new SolrAnalyzer() {
65
public TokenStreamInfo getStream(String fieldName, Reader reader) {
66
Tokenizer tokenizer = new Tokenizer(reader) {
67
final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
71
public void reset(Reader input) throws IOException {
77
public boolean incrementToken() throws IOException {
79
if (done) return false;
81
int ch = input.read();
82
if (ch==-1) return false;
84
((ch=='t' || ch=='T' || ch=='1') ? TRUE_TOKEN : FALSE_TOKEN)
90
return new TokenStreamInfo(tokenizer, tokenizer);
96
public Analyzer getAnalyzer() {
101
public Analyzer getQueryAnalyzer() {
106
public String toInternal(String val) {
107
char ch = (val!=null && val.length()>0) ? val.charAt(0) : 0;
108
return (ch=='1' || ch=='t' || ch=='T') ? "T" : "F";
112
public String toExternal(Fieldable f) {
113
return indexedToReadable(f.stringValue());
117
public Boolean toObject(Fieldable f) {
118
return Boolean.valueOf( toExternal(f) );
122
public String indexedToReadable(String indexedForm) {
123
char ch = indexedForm.charAt(0);
124
return ch=='T' ? "true" : "false";
128
public void write(XMLWriter xmlWriter, String name, Fieldable f) throws IOException {
129
xmlWriter.writeBool(name, f.stringValue().charAt(0) =='T');
133
public void write(TextResponseWriter writer, String name, Fieldable f) throws IOException {
134
writer.writeBool(name, f.stringValue().charAt(0) =='T');