~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/distance/VectorDistanceFunction.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
 
package org.apache.solr.search.function.distance;
2
 
/**
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
9
 
 *
10
 
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 
 *
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.
17
 
 */
18
 
 
19
 
import org.apache.lucene.index.IndexReader;
20
 
import org.apache.lucene.search.Searcher;
21
 
import org.apache.lucene.spatial.DistanceUtils;
22
 
import org.apache.solr.common.SolrException;
23
 
import org.apache.solr.search.function.DocValues;
24
 
import org.apache.solr.search.function.MultiValueSource;
25
 
import org.apache.solr.search.function.ValueSource;
26
 
 
27
 
import java.io.IOException;
28
 
import java.util.Map;
29
 
 
30
 
 
31
 
/**
32
 
 * Calculate the p-norm for a Vector.  See http://en.wikipedia.org/wiki/Lp_space
33
 
 * <p/>
34
 
 * Common cases:
35
 
 * <ul>
36
 
 * <li>0 = Sparseness calculation</li>
37
 
 * <li>1 = Manhattan distance</li>
38
 
 * <li>2 = Euclidean distance</li>
39
 
 * <li>Integer.MAX_VALUE = infinite norm</li>
40
 
 * </ul>
41
 
 *
42
 
 * @see SquaredEuclideanFunction for the special case
43
 
 */
44
 
public class VectorDistanceFunction extends ValueSource {
45
 
  protected MultiValueSource source1, source2;
46
 
  protected float power;
47
 
  protected float oneOverPower;
48
 
 
49
 
  public VectorDistanceFunction(float power, MultiValueSource source1, MultiValueSource source2) {
50
 
    if ((source1.dimension() != source2.dimension())) {
51
 
      throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Illegal number of sources");
52
 
    }
53
 
    this.power = power;
54
 
    this.oneOverPower = 1 / power;
55
 
    this.source1 = source1;
56
 
    this.source2 = source2;
57
 
  }
58
 
 
59
 
  protected String name() {
60
 
    return "dist";
61
 
  }
62
 
 
63
 
  /**
64
 
   * Calculate the distance
65
 
   *
66
 
   * @param doc The current doc
67
 
   * @param dv1 The values from the first MultiValueSource
68
 
   * @param dv2 The values from the second MultiValueSource
69
 
   * @return The distance
70
 
   */
71
 
  protected double distance(int doc, DocValues dv1, DocValues dv2) {
72
 
    //Handle some special cases:
73
 
    double[] vals1 = new double[source1.dimension()];
74
 
    double[] vals2 = new double[source1.dimension()];
75
 
    dv1.doubleVal(doc, vals1);
76
 
    dv2.doubleVal(doc, vals2);
77
 
    return DistanceUtils.vectorDistance(vals1, vals2, power, oneOverPower);
78
 
  }
79
 
 
80
 
  @Override
81
 
  public DocValues getValues(Map context, IndexReader reader) throws IOException {
82
 
 
83
 
    final DocValues vals1 = source1.getValues(context, reader);
84
 
 
85
 
    final DocValues vals2 = source2.getValues(context, reader);
86
 
 
87
 
 
88
 
    return new DocValues() {
89
 
      @Override
90
 
      public byte byteVal(int doc) {
91
 
        return (byte) doubleVal(doc);
92
 
      }
93
 
 
94
 
      @Override
95
 
      public short shortVal(int doc) {
96
 
        return (short) doubleVal(doc);
97
 
      }
98
 
 
99
 
      @Override
100
 
      public float floatVal(int doc) {
101
 
        return (float) doubleVal(doc);
102
 
      }
103
 
 
104
 
      @Override
105
 
      public int intVal(int doc) {
106
 
        return (int) doubleVal(doc);
107
 
      }
108
 
 
109
 
      @Override
110
 
      public long longVal(int doc) {
111
 
        return (long) doubleVal(doc);
112
 
      }
113
 
 
114
 
      @Override
115
 
      public double doubleVal(int doc) {
116
 
        return distance(doc, vals1, vals2);
117
 
      }
118
 
 
119
 
      @Override
120
 
      public String strVal(int doc) {
121
 
        return Double.toString(doubleVal(doc));
122
 
      }
123
 
 
124
 
      @Override
125
 
      public String toString(int doc) {
126
 
        StringBuilder sb = new StringBuilder();
127
 
        sb.append(name()).append('(').append(power).append(',');
128
 
        boolean firstTime = true;
129
 
        sb.append(vals1.toString(doc)).append(',');
130
 
        sb.append(vals2.toString(doc));
131
 
        sb.append(')');
132
 
        return sb.toString();
133
 
      }
134
 
    };
135
 
  }
136
 
 
137
 
  @Override
138
 
  public void createWeight(Map context, Searcher searcher) throws IOException {
139
 
    source1.createWeight(context, searcher);
140
 
    source2.createWeight(context, searcher);
141
 
  }
142
 
 
143
 
  @Override
144
 
  public boolean equals(Object o) {
145
 
    if (this == o) return true;
146
 
    if (!(o instanceof VectorDistanceFunction)) return false;
147
 
 
148
 
    VectorDistanceFunction that = (VectorDistanceFunction) o;
149
 
 
150
 
    if (Float.compare(that.power, power) != 0) return false;
151
 
    if (!source1.equals(that.source1)) return false;
152
 
    if (!source2.equals(that.source2)) return false;
153
 
 
154
 
    return true;
155
 
  }
156
 
 
157
 
  @Override
158
 
  public int hashCode() {
159
 
    int result = source1.hashCode();
160
 
    result = 31 * result + source2.hashCode();
161
 
    result = 31 * result + Float.floatToRawIntBits(power);
162
 
    return result;
163
 
  }
164
 
 
165
 
  @Override
166
 
  public String description() {
167
 
    StringBuilder sb = new StringBuilder();
168
 
    sb.append(name()).append('(').append(power).append(',');
169
 
    sb.append(source1).append(',');
170
 
    sb.append(source2);
171
 
    sb.append(')');
172
 
    return sb.toString();
173
 
  }
174
 
 
175
 
}