~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to solr/core/src/java/org/apache/solr/schema/BoolField.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
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
8
 
 *
9
 
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 
 *
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.
16
 
 */
17
 
 
18
 
package org.apache.solr.schema;
19
 
 
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;
31
 
 
32
 
import java.util.Map;
33
 
import java.io.Reader;
34
 
import java.io.IOException;
35
 
/**
36
 
 * @version $Id: BoolField.java 1071480 2011-02-17 02:23:10Z hossman $
37
 
 */
38
 
public class BoolField extends FieldType {
39
 
  @Override
40
 
  protected void init(IndexSchema schema, Map<String,String> args) {
41
 
  }
42
 
 
43
 
  @Override
44
 
  public SortField getSortField(SchemaField field,boolean reverse) {
45
 
    field.checkSortability();
46
 
    return getStringSort(field,reverse);
47
 
  }
48
 
 
49
 
  @Override
50
 
  public ValueSource getValueSource(SchemaField field, QParser qparser) {
51
 
    field.checkFieldCacheSource(qparser);
52
 
    return new OrdFieldSource(field.name);
53
 
  }
54
 
 
55
 
  // avoid instantiating every time...
56
 
  protected final static char[] TRUE_TOKEN = {'T'};
57
 
  protected final static char[] FALSE_TOKEN = {'F'};
58
 
 
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.
62
 
 
63
 
  protected final static Analyzer boolAnalyzer = new SolrAnalyzer() {
64
 
    @Override
65
 
    public TokenStreamInfo getStream(String fieldName, Reader reader) {
66
 
      Tokenizer tokenizer = new Tokenizer(reader) {
67
 
        final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
68
 
        boolean done = false;
69
 
 
70
 
        @Override
71
 
        public void reset(Reader input) throws IOException {
72
 
          done = false;
73
 
          super.reset(input);
74
 
        }
75
 
 
76
 
        @Override
77
 
        public boolean incrementToken() throws IOException {
78
 
          clearAttributes();
79
 
          if (done) return false;
80
 
          done = true;
81
 
          int ch = input.read();
82
 
          if (ch==-1) return false;
83
 
          termAtt.copyBuffer(
84
 
                  ((ch=='t' || ch=='T' || ch=='1') ? TRUE_TOKEN : FALSE_TOKEN)
85
 
                  ,0,1);
86
 
          return true;
87
 
        }
88
 
      };
89
 
 
90
 
      return new TokenStreamInfo(tokenizer, tokenizer);
91
 
    }
92
 
  };
93
 
 
94
 
 
95
 
  @Override
96
 
  public Analyzer getAnalyzer() {
97
 
    return boolAnalyzer;
98
 
  }
99
 
 
100
 
  @Override
101
 
  public Analyzer getQueryAnalyzer() {
102
 
    return boolAnalyzer;
103
 
  }
104
 
 
105
 
  @Override
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";
109
 
  }
110
 
 
111
 
  @Override
112
 
  public String toExternal(Fieldable f) {
113
 
    return indexedToReadable(f.stringValue());
114
 
  }
115
 
 
116
 
  @Override
117
 
  public Boolean toObject(Fieldable f) {
118
 
    return Boolean.valueOf( toExternal(f) );
119
 
  }
120
 
 
121
 
  @Override
122
 
  public String indexedToReadable(String indexedForm) {
123
 
    char ch = indexedForm.charAt(0);
124
 
    return ch=='T' ? "true" : "false";
125
 
  }
126
 
 
127
 
  @Override
128
 
  public void write(XMLWriter xmlWriter, String name, Fieldable f) throws IOException {
129
 
    xmlWriter.writeBool(name, f.stringValue().charAt(0) =='T');
130
 
  }
131
 
 
132
 
  @Override
133
 
  public void write(TextResponseWriter writer, String name, Fieldable f) throws IOException {
134
 
    writer.writeBool(name, f.stringValue().charAt(0) =='T');
135
 
  }
136
 
}