Index: src/test/org/apache/lucene/spatial/tier/TestDistance.java =================================================================== --- src/test/org/apache/lucene/spatial/tier/TestDistance.java (revision 784551) +++ src/test/org/apache/lucene/spatial/tier/TestDistance.java (working copy) @@ -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.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,17 +47,21 @@ 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{ Document doc = new Document(); @@ -90,7 +99,18 @@ 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() { Index: src/java/org/apache/lucene/spatial/tier/LatLongDistanceFilter.java =================================================================== --- src/java/org/apache/lucene/spatial/tier/LatLongDistanceFilter.java (revision 784551) +++ src/java/org/apache/lucene/spatial/tier/LatLongDistanceFilter.java (working copy) @@ -25,6 +25,7 @@ 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 @@ /* store calculated distances for reuse by other components */ distances = new HashMap(maxdocs); - if (distances == null){ distances = new HashMap(); } - - 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,13 @@ 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); - + // i = bits.nextSetBit(i+1); } int size = bits.cardinality(); nextOffset += reader.maxDoc(); // this should be something that's part of indexReader @@ -175,8 +175,14 @@ /* 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 // filter chain, lat / lngs can be retrived from // memory rather than document base.