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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.lucene.spatial.tier;
20 import java.io.IOException;
22 import org.apache.lucene.index.IndexReader;
23 import org.apache.lucene.search.Filter;
24 import org.apache.lucene.search.FieldComparator;
25 import org.apache.lucene.search.FieldComparatorSource;
28 * <p><font color="red"><b>NOTE:</b> This API is still in
29 * flux and might change in incompatible ways in the next
32 public class DistanceFieldComparatorSource extends FieldComparatorSource {
34 private static final long serialVersionUID = 1L;
36 private DistanceFilter distanceFilter;
37 private DistanceScoreDocLookupComparator dsdlc;
39 public DistanceFieldComparatorSource(Filter distanceFilter) {
40 this.distanceFilter = (DistanceFilter) distanceFilter;
43 public void cleanUp() {
44 distanceFilter = null;
54 public FieldComparator newComparator(String fieldname, int numHits,
55 int sortPos, boolean reversed) throws IOException {
56 dsdlc = new DistanceScoreDocLookupComparator(numHits);
60 private class DistanceScoreDocLookupComparator extends FieldComparator<Double> {
62 private double[] values;
63 private double bottom;
64 private int offset =0;
66 public DistanceScoreDocLookupComparator(int numHits) {
67 values = new double[numHits];
72 public int compare(int slot1, int slot2) {
73 double a = values[slot1];
74 double b = values[slot2];
83 public void cleanUp() {
84 distanceFilter = null;
88 public int compareBottom(int doc) {
89 double v2 = distanceFilter.getDistance(doc+ offset);
93 } else if (bottom < v2) {
100 public void copy(int slot, int doc) {
101 values[slot] = distanceFilter.getDistance(doc + offset);
105 public void setBottom(int slot) {
106 this.bottom = values[slot];
110 public void setNextReader(IndexReader reader, int docBase)
113 // each reader in a segmented base
114 // has an offset based on the maxDocs of previous readers
119 public Double value(int slot) {