LUCENE-1571: fix LatLongDistanceFilter to respect deleted docs

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@784576 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2009-06-14 15:47:15 +00:00
parent 6671c43458
commit 26b9dea62b
2 changed files with 39 additions and 14 deletions

View File

@ -25,6 +25,7 @@ import java.util.WeakHashMap;
import java.util.logging.Logger;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.TermDocs;
import org.apache.lucene.search.FieldCache;
import org.apache.lucene.spatial.NumberUtils;
import org.apache.lucene.spatial.tier.DistanceHandler.Precision;
@ -96,15 +97,16 @@ public class LatLongDistanceFilter extends DistanceFilter {
/* store calculated distances for reuse by other components */
distances = new HashMap<Integer,Double>(maxdocs);
if (distances == null){
distances = new HashMap<Integer,Double>();
}
for (int i = 0 ; i < maxdocs; i++) {
TermDocs td = reader.termDocs(null);
while(td.next()) {
int doc = td.doc();
String sx = latIndex[i];
String sy = lngIndex[i];
String sx = latIndex[doc];
String sy = lngIndex[doc];
double x = NumberUtils.SortableStr2double(sx);
double y = NumberUtils.SortableStr2double(sy);
@ -125,15 +127,12 @@ public class LatLongDistanceFilter extends DistanceFilter {
d = DistanceUtils.getInstance().getDistanceMi(lat, lng, x, y);
cdistance.put(ck, d);
}
distances.put(i, d);
// why was i storing all distances again?
if (d < distance){
bits.set(i);
distances.put(i+ nextOffset, d); // include nextOffset for multi segment reader
bits.set(doc);
distances.put(doc+ nextOffset, d); // include nextOffset for multi segment reader
}
i = bits.nextSetBit(i+1);
}
int size = bits.cardinality();
nextOffset += reader.maxDoc(); // this should be something that's part of indexReader
@ -175,6 +174,12 @@ public class LatLongDistanceFilter extends DistanceFilter {
/* loop over all set bits (hits from the boundary box filters) */
int i = bits.nextSetBit(0);
while (i >= 0){
if (reader.isDeleted(i)) {
i = bits.nextSetBit(i+1);
continue;
}
double x,y;
// if we have a completed

View File

@ -17,6 +17,7 @@
package org.apache.lucene.spatial.tier;
import java.io.IOException;
import java.util.BitSet;
import junit.framework.TestCase;
@ -24,8 +25,12 @@ import org.apache.lucene.analysis.WhitespaceAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.spatial.NumberUtils;
import org.apache.lucene.spatial.tier.LatLongDistanceFilter;
import org.apache.lucene.store.RAMDirectory;
@ -42,16 +47,20 @@ public class TestDistance extends TestCase{
private double lng= -77.386398;
private String latField = "lat";
private String lngField = "lng";
private IndexWriter writer;
@Override
protected void setUp() throws IOException {
directory = new RAMDirectory();
IndexWriter writer = new IndexWriter(directory, new WhitespaceAnalyzer(), true);
writer = new IndexWriter(directory, new WhitespaceAnalyzer(), true);
addData(writer);
}
@Override
protected void tearDown() throws IOException {
writer.close();
}
private void addPoint(IndexWriter writer, String name, double lat, double lng) throws IOException{
@ -90,7 +99,18 @@ public class TestDistance extends TestCase{
addPoint(writer,"HorseFeathers, Bar & Grill", 39.01220000000001, -77.3942);
writer.flush();
}
public void testLatLongFilterOnDeletedDocs() throws Exception {
writer.deleteDocuments(new Term("name", "Potomac"));
IndexReader r = writer.getReader();
LatLongDistanceFilter f = new LatLongDistanceFilter(lat, lng, 1.0, latField, lngField);
f.bits(r);
BitSet allSet = new BitSet(r.maxDoc());
allSet.set(0, r.maxDoc());
f.bits(r, allSet);
r.close();
}
public void testMiles() {