~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/ScaleFloatFunction.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.index.IndexReader;
21
 
import org.apache.lucene.search.Searcher;
22
 
 
23
 
import java.io.IOException;
24
 
import java.util.Map;
25
 
 
26
 
/**
27
 
 * Scales values to be between min and max.
28
 
 * <p>This implementation currently traverses all of the source values to obtain
29
 
 * their min and max.
30
 
 * <p>This implementation currently cannot distinguish when documents have been
31
 
 * deleted or documents that have no value, and 0.0 values will be used for
32
 
 * these cases.  This means that if values are normally all greater than 0.0, one can
33
 
 * still end up with 0.0 as the min value to map from.  In these cases, an
34
 
 * appropriate map() function could be used as a workaround to change 0.0
35
 
 * to a value in the real range.
36
 
 */
37
 
public class ScaleFloatFunction extends ValueSource {
38
 
  protected final ValueSource source;
39
 
  protected final float min;
40
 
  protected final float max;
41
 
 
42
 
  public ScaleFloatFunction(ValueSource source, float min, float max) {
43
 
    this.source = source;
44
 
    this.min = min;
45
 
    this.max = max;
46
 
  }
47
 
 
48
 
  @Override
49
 
  public String description() {
50
 
    return "scale(" + source.description() + "," + min + "," + max + ")";
51
 
  }
52
 
 
53
 
  @Override
54
 
  public DocValues getValues(Map context, IndexReader reader) throws IOException {
55
 
    final DocValues vals =  source.getValues(context, reader);
56
 
    int maxDoc = reader.maxDoc();
57
 
 
58
 
    // this doesn't take into account deleted docs!
59
 
    float minVal=0.0f;
60
 
    float maxVal=0.0f;
61
 
 
62
 
    if (maxDoc>0) {
63
 
      minVal = maxVal = vals.floatVal(0);      
64
 
    }
65
 
 
66
 
    // Traverse the complete set of values to get the min and the max.
67
 
    // Future alternatives include being able to ask a DocValues for min/max
68
 
    // Another memory-intensive option is to cache the values in
69
 
    // a float[] on this first pass.
70
 
 
71
 
    for (int i=0; i<maxDoc; i++) {
72
 
      float val = vals.floatVal(i);
73
 
      if ((Float.floatToRawIntBits(val) & (0xff<<23)) == 0xff<<23) {
74
 
        // if the exponent in the float is all ones, then this is +Inf, -Inf or NaN
75
 
        // which don't make sense to factor into the scale function
76
 
        continue;
77
 
      }
78
 
      if (val < minVal) {
79
 
        minVal = val;
80
 
      } else if (val > maxVal) {
81
 
        maxVal = val;
82
 
      }
83
 
    }
84
 
 
85
 
    final float scale = (maxVal-minVal==0) ? 0 : (max-min)/(maxVal-minVal);
86
 
    final float minSource = minVal;
87
 
    final float maxSource = maxVal;
88
 
 
89
 
    return new DocValues() {
90
 
      @Override
91
 
      public float floatVal(int doc) {
92
 
        return (vals.floatVal(doc) - minSource) * scale + min;
93
 
      }
94
 
      @Override
95
 
      public int intVal(int doc) {
96
 
        return (int)floatVal(doc);
97
 
      }
98
 
      @Override
99
 
      public long longVal(int doc) {
100
 
        return (long)floatVal(doc);
101
 
      }
102
 
      @Override
103
 
      public double doubleVal(int doc) {
104
 
        return (double)floatVal(doc);
105
 
      }
106
 
      @Override
107
 
      public String strVal(int doc) {
108
 
        return Float.toString(floatVal(doc));
109
 
      }
110
 
      @Override
111
 
      public String toString(int doc) {
112
 
        return "scale(" + vals.toString(doc) + ",toMin=" + min + ",toMax=" + max
113
 
                + ",fromMin=" + minSource
114
 
                + ",fromMax=" + maxSource
115
 
                + ")";
116
 
      }
117
 
    };
118
 
  }
119
 
 
120
 
  @Override
121
 
  public void createWeight(Map context, Searcher searcher) throws IOException {
122
 
    source.createWeight(context, searcher);
123
 
  }
124
 
 
125
 
  @Override
126
 
  public int hashCode() {
127
 
    int h = Float.floatToIntBits(min);
128
 
    h = h*29;
129
 
    h += Float.floatToIntBits(max);
130
 
    h = h*29;
131
 
    h += source.hashCode();
132
 
    return h;
133
 
  }
134
 
 
135
 
  @Override
136
 
  public boolean equals(Object o) {
137
 
    if (ScaleFloatFunction.class != o.getClass()) return false;
138
 
    ScaleFloatFunction other = (ScaleFloatFunction)o;
139
 
    return this.min == other.min
140
 
         && this.max == other.max
141
 
         && this.source.equals(other.source);
142
 
  }
143
 
}