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

« back to all changes in this revision

Viewing changes to solr/core/src/java/org/apache/solr/search/function/StringIndexDocValues.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.search.function;
19
 
 
20
 
import org.apache.lucene.search.FieldCache;
21
 
import org.apache.lucene.index.IndexReader;
22
 
 
23
 
import java.io.IOException;
24
 
 
25
 
/** Internal class, subject to change.
26
 
 *  Serves as base class for DocValues based on StringIndex 
27
 
 **/
28
 
public abstract class StringIndexDocValues extends DocValues {
29
 
    protected final FieldCache.StringIndex index;
30
 
    protected final int[] order;
31
 
    protected final String[] lookup;
32
 
    protected final ValueSource vs;
33
 
 
34
 
    public StringIndexDocValues(ValueSource vs, IndexReader reader, String field) throws IOException {
35
 
      try {
36
 
        index = FieldCache.DEFAULT.getStringIndex(reader, field);
37
 
      } catch (RuntimeException e) {
38
 
        throw new StringIndexException(field, e);
39
 
      }
40
 
      order = index.order;
41
 
      lookup = index.lookup;
42
 
      this.vs = vs;
43
 
    }
44
 
  
45
 
    protected abstract String toTerm(String readableValue);
46
 
 
47
 
   @Override
48
 
    public ValueSourceScorer getRangeScorer(IndexReader reader, String lowerVal, String upperVal, boolean includeLower, boolean includeUpper) {
49
 
      // TODO: are lowerVal and upperVal in indexed form or not?
50
 
      lowerVal = lowerVal == null ? null : toTerm(lowerVal);
51
 
      upperVal = upperVal == null ? null : toTerm(upperVal);
52
 
 
53
 
     int lower = Integer.MIN_VALUE;
54
 
     if (lowerVal != null) {
55
 
       lower = index.binarySearchLookup(lowerVal);
56
 
       if (lower < 0) {
57
 
         lower = -lower-1;
58
 
       } else if (!includeLower) {
59
 
         lower++;
60
 
       }
61
 
     }
62
 
 
63
 
     int upper = Integer.MAX_VALUE;
64
 
     if (upperVal != null) {
65
 
       upper = index.binarySearchLookup(upperVal);
66
 
       if (upper < 0) {
67
 
         upper = -upper-2;
68
 
       } else if (!includeUpper) {
69
 
         upper--;
70
 
       }
71
 
     }
72
 
 
73
 
      final int ll = lower;
74
 
      final int uu = upper;
75
 
 
76
 
      return new ValueSourceScorer(reader, this) {
77
 
        @Override
78
 
        public boolean matchesValue(int doc) {
79
 
          int ord = order[doc];
80
 
          return ord >= ll && ord <= uu;
81
 
        }
82
 
      };
83
 
    }
84
 
 
85
 
    @Override
86
 
    public String toString(int doc) {
87
 
      return vs.description() + '=' + strVal(doc);
88
 
    }
89
 
 
90
 
  public static final class StringIndexException extends RuntimeException {
91
 
    public StringIndexException(final String fieldName,
92
 
                                final RuntimeException cause) {
93
 
      super("Can't initialize StringIndex to generate (function) " +
94
 
            "DocValues for field: " + fieldName, cause);
95
 
    }
96
 
  }
97
 
  
98
 
}