1
package org.apache.solr.search.function.distance;
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
10
* http://www.apache.org/licenses/LICENSE-2.0
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.
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.MultiValueSource;
24
import org.apache.solr.search.function.DocValues;
25
import org.apache.solr.search.function.ValueSource;
27
import java.io.IOException;
32
* Calculate the Haversine formula (distance) between any two points on a sphere
33
* Takes in four value sources: (latA, lonA); (latB, lonB).
35
* Assumes the value sources are in radians unless
37
* See http://en.wikipedia.org/wiki/Great-circle_distance and
38
* http://en.wikipedia.org/wiki/Haversine_formula for the actual formula and
39
* also http://www.movable-type.co.uk/scripts/latlong.html
41
public class HaversineFunction extends ValueSource {
43
private MultiValueSource p1;
44
private MultiValueSource p2;
45
private boolean convertToRadians = false;
46
private double radius;
48
public HaversineFunction(MultiValueSource p1, MultiValueSource p2, double radius) {
49
this(p1, p2, radius, false);
52
public HaversineFunction(MultiValueSource p1, MultiValueSource p2, double radius, boolean convertToRads){
55
if (p1.dimension() != 2 || p2.dimension() != 2) {
56
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Illegal dimension for value sources");
59
this.convertToRadians = convertToRads;
62
protected String name() {
67
* @param doc The doc to score
70
* @return The haversine distance formula
72
protected double distance(int doc, DocValues p1DV, DocValues p2DV) {
74
double[] p1D = new double[2];
75
double[] p2D = new double[2];
76
p1DV.doubleVal(doc, p1D);
77
p2DV.doubleVal(doc, p2D);
82
if (convertToRadians) {
83
x1 = p1D[0] * DistanceUtils.DEGREES_TO_RADIANS;
84
y1 = p1D[1] * DistanceUtils.DEGREES_TO_RADIANS;
85
x2 = p2D[0] * DistanceUtils.DEGREES_TO_RADIANS;
86
y2 = p2D[1] * DistanceUtils.DEGREES_TO_RADIANS;
93
return DistanceUtils.haversine(x1, y1, x2, y2, radius);
98
public DocValues getValues(Map context, IndexReader reader) throws IOException {
99
final DocValues vals1 = p1.getValues(context, reader);
101
final DocValues vals2 = p2.getValues(context, reader);
102
return new DocValues() {
104
public float floatVal(int doc) {
105
return (float) doubleVal(doc);
109
public int intVal(int doc) {
110
return (int) doubleVal(doc);
114
public long longVal(int doc) {
115
return (long) doubleVal(doc);
119
public double doubleVal(int doc) {
120
return distance(doc, vals1, vals2);
124
public String strVal(int doc) {
125
return Double.toString(doubleVal(doc));
129
public String toString(int doc) {
130
StringBuilder sb = new StringBuilder();
131
sb.append(name()).append('(');
132
sb.append(vals1.toString(doc)).append(',').append(vals2.toString(doc));
134
return sb.toString();
140
public void createWeight(Map context, Searcher searcher) throws IOException {
141
p1.createWeight(context, searcher);
142
p2.createWeight(context, searcher);
147
public boolean equals(Object o) {
148
if (this.getClass() != o.getClass()) return false;
149
HaversineFunction other = (HaversineFunction) o;
150
return this.name().equals(other.name())
151
&& p1.equals(other.p1) &&
152
p2.equals(other.p2) && radius == other.radius;
156
public int hashCode() {
159
result = p1.hashCode();
160
result = 31 * result + p2.hashCode();
161
result = 31 * result + name().hashCode();
162
temp = Double.doubleToRawLongBits(radius);
163
result = 31 * result + (int) (temp ^ (temp >>> 32));
168
public String description() {
169
StringBuilder sb = new StringBuilder();
170
sb.append(name()).append('(');
171
sb.append(p1).append(',').append(p2);
173
return sb.toString();