Skip to content

Apache Lucene open-source search software

LUCENE-1571: fix LatLongDistanceFilter to respect deleted docs

Michael McCandless26b9dea62b51
diff --git a/contrib/spatial/src/java/org/apache/lucene/spatial/tier/LatLongDistanceFilter.java b/contrib/spatial/src/java/org/apache/lucene/spatial/tier/LatLongDistanceFilter.java
index 65d3f7c1f54..4aaad2c1d1a 100644
--- a/contrib/spatial/src/java/org/apache/lucene/spatial/tier/LatLongDistanceFilter.java
+++ b/contrib/spatial/src/java/org/apache/lucene/spatial/tier/LatLongDistanceFilter.java
@@ -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
diff --git a/contrib/spatial/src/test/org/apache/lucene/spatial/tier/TestDistance.java b/contrib/spatial/src/test/org/apache/lucene/spatial/tier/TestDistance.java
index c5e1341b4a2..ebcc4cb352e 100644
--- a/contrib/spatial/src/test/org/apache/lucene/spatial/tier/TestDistance.java
+++ b/contrib/spatial/src/test/org/apache/lucene/spatial/tier/TestDistance.java
@@ -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() {