Commit LUCENE-1588 DistanceFieldComparatorSource

deprecate BoundaryBoxFilter

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@762801 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Patrick O'Leary 2009-04-07 14:54:50 +00:00
parent 13ae26b7fe
commit 42783c57e5
5 changed files with 152 additions and 8 deletions

View File

@ -33,7 +33,8 @@ import org.apache.lucene.spatial.NumberUtils;
/**
* An implementation of org.apache.lucene.search.RangeFilter that
* caches values extracted from the index.
*
* @deprecated
* @see CartesianShapeFilter
*/
public class BoundaryBoxFilter extends Filter {

View File

@ -0,0 +1,130 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.lucene.spatial.tier;
import java.io.IOException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.FieldComparator;
import org.apache.lucene.search.FieldComparatorSource;
import org.apache.lucene.search.SortField;
public class DistanceFieldComparatorSource extends FieldComparatorSource {
private static final long serialVersionUID = 1L;
private DistanceFilter distanceFilter;
private DistanceScoreDocLookupComparator dsdlc;
public DistanceFieldComparatorSource(Filter distanceFilter) {
this.distanceFilter = (DistanceFilter) distanceFilter;
}
public void cleanUp() {
distanceFilter = null;
if (dsdlc != null)
dsdlc.cleanUp();
dsdlc = null;
}
@Override
public FieldComparator newComparator(String fieldname,
IndexReader[] subReaders, int numHits, int sortPos, boolean reversed)
throws IOException {
dsdlc = new DistanceScoreDocLookupComparator(distanceFilter, numHits);
return dsdlc;
}
private class DistanceScoreDocLookupComparator extends FieldComparator {
private DistanceFilter distanceFilter;
private double[] values;
private double bottom;
public DistanceScoreDocLookupComparator(DistanceFilter distanceFilter, int numHits) {
this.distanceFilter = distanceFilter;
values = new double[numHits];
return;
}
@Override
public int compare(int slot1, int slot2) {
double a = values[slot1];
double b = values[slot2];
if (a > b)
return 1;
if (a < b)
return -1;
return 0;
}
public void cleanUp() {
distanceFilter = null;
}
@Override
public int compareBottom(int doc, float score) {
final double v2 = distanceFilter.getDistance(doc);
if (bottom > v2) {
return 1;
} else if (bottom < v2) {
return -1;
}
return 0;
}
@Override
public void copy(int slot, int doc, float score) {
values[slot] = distanceFilter.getDistance(doc);
}
@Override
public void setBottom(int slot) {
this.bottom = values[slot];
}
@Override
public void setNextReader(IndexReader reader, int docBase, int numSlotsFull)
throws IOException {
// TODO Auto-generated method stub
}
@Override
public Comparable<Double> value(int slot) {
return values[slot];
}
@Override
public int sortType() {
return SortField.DOUBLE;
}
}
}

View File

@ -142,6 +142,11 @@ public class DistanceQueryBuilder {
return new ConstantScoreQuery(getFilter());
}
public Query getQuery(Query query){
return new ConstantScoreQuery(getFilter(query));
}
public double getLat() {
return lat;
}

View File

@ -26,6 +26,11 @@ import org.apache.lucene.search.ScoreDocComparator;
import org.apache.lucene.search.SortComparatorSource;
import org.apache.lucene.search.SortField;
/**
*
* @deprecated
* @see DistanceFieldComparatorSource
*/
public class DistanceSortSource implements SortComparatorSource {
private static final long serialVersionUID = 1L;

View File

@ -138,9 +138,9 @@ public class TestCartesian extends TestCase{
addPoint(writer,"Iota Club and Cafe",38.8890000,-77.0923000);
addPoint(writer,"Hilton Washington Embassy Row",38.9103000,-77.0451000);
addPoint(writer,"HorseFeathers, Bar & Grill", 39.01220000000001, -77.3942);
writer.flush();
writer.commit();
//writer.close();
writer.close();
}
public void testRange() throws IOException, InvalidGeoException {
@ -179,12 +179,12 @@ public class TestCartesian extends TestCase{
// As the radius filter has performed the distance calculations
// already, pass in the filter to reuse the results.
//
DistanceSortSource dsort = new DistanceSortSource(dq.distanceFilter);
Sort sort = new Sort(new SortField("foo", dsort));
DistanceFieldComparatorSource dsort = new DistanceFieldComparatorSource(dq.distanceFilter);
Sort sort = new Sort(new SortField("foo", dsort,false));
// Perform the search, using the term query, the serial chain filter, and the
// distance sort
Hits hits = searcher.search(customScore, dq.getFilter()); //,sort);
Hits hits = searcher.search(customScore, dq.getFilter(),sort);
int results = hits.length();
@ -206,7 +206,7 @@ public class TestCartesian extends TestCase{
assertEquals(14, distances.size());
assertEquals(7, results);
double lastDistance = 0;
for(int i =0 ; i < results; i++){
Document d = hits.doc(i);
@ -217,9 +217,11 @@ public class TestCartesian extends TestCase{
double distance = DistanceUtils.getInstance().getDistanceMi(lat, lng, rsLat, rsLng);
double llm = DistanceUtils.getInstance().getLLMDistance(lat, lng, rsLat, rsLng);
System.out.println("Name: "+ name +", Distance (res, ortho, harvesine):"+ distance +" |"+ geo_distance +"|"+ llm +" | score "+ hits.score(i));
System.out.println("Name: "+ name +", Distance "+ distance); //(res, ortho, harvesine):"+ distance +" |"+ geo_distance +"|"+ llm +" | score "+ hits.score(i));
assertTrue(Math.abs((distance - llm)) < 1);
assertTrue((distance < miles ));
assertTrue(geo_distance > lastDistance);
lastDistance = geo_distance;
}
}
@ -302,6 +304,7 @@ public class TestCartesian extends TestCase{
System.out.println("Name: "+ name +", Distance (res, ortho, harvesine):"+ distance +" |"+ geo_distance +"|"+ llm +" | score "+ hits.score(i));
assertTrue(Math.abs((distance - llm)) < 1);
assertTrue((distance < miles ));
}
}