Hi, all


I was trying to add some distance search capabilities to an index using spatial lucene in lucene 3.0 but could not get it working. So I wrote a very small test example to try out the api. However, even the simplest test case would NOT function correctly either.


Anyone has ideas/experiences about spatial lucene in 3.0?


My codes


//tested using core-3.0, misc-3.0, spatial-3.0, the following code snippet does not return any results but it should


import java.io.IOException;


import org.apache.lucene.analysis.WhitespaceAnalyzer;

import org.apache.lucene.document.Document;

import org.apache.lucene.document.Field;

import org.apache.lucene.document.Field.Index;

import org.apache.lucene.document.Field.Store;

import org.apache.lucene.index.IndexWriter;

import org.apache.lucene.index.Term;

import org.apache.lucene.index.IndexWriter.MaxFieldLength;

import org.apache.lucene.search.IndexSearcher;

import org.apache.lucene.search.Query;

import org.apache.lucene.search.TermQuery;

import org.apache.lucene.search.TopDocs;

import org.apache.lucene.spatial.tier.DistanceQueryBuilder;

import org.apache.lucene.spatial.tier.projections.CartesianTierPlotter;

import org.apache.lucene.spatial.tier.projections.IProjector;

import org.apache.lucene.spatial.tier.projections.SinusoidalProjector;

import org.apache.lucene.store.Directory;

import org.apache.lucene.store.RAMDirectory;

import org.apache.lucene.util.NumericUtils;


public class SpatialLuceneExample{

    private static final String LAT_FIELD = "lat", LON_FIELD = "lon", TIER_PREFIX_FIELD = "_localTier";


    private double maxMiles = 25, minMiles = 1; // anything lower than 1 mile will just give tier 15

    // see http://www.nsshutdown.com/projects/lucene/whitepaper/locallucene_v2.html

    private IProjector projector = new SinusoidalProjector();

    private CartesianTierPlotter ctp = new CartesianTierPlotter(0, projector, TIER_PREFIX_FIELD);

    //startTier is 14 for 25 miles, 15 for 1 miles in lucene 3.0

    private int startTier = 5, endTier = 15;   



     * Add the lat, lon, and tier box id to the document

     * see http://www.nsshutdown.com/projects/lucene/whitepaper/locallucene_v2.html

     * @param lat

     * @param lon

     * @param document a geo document


    private void addSpatialLcnFields(double lat, double lon, Document document){

        document.add(new Field(LAT_FIELD, NumericUtils.doubleToPrefixCoded(lat), Field.Store.YES, Field.Index.NOT_ANALYZED));

        document.add(new Field(LON_FIELD, NumericUtils.doubleToPrefixCoded(lon), Field.Store.YES, Field.Index.NOT_ANALYZED));


        for(int tier = startTier ; tier<= endTier; tier++){

            CartesianTierPlotter ctp = new CartesianTierPlotter(tier, projector, TIER_PREFIX_FIELD);

            double boxId = ctp.getTierBoxId(lat, lon);

            document.add(new Field(ctp.getTierFieldName(), NumericUtils.doubleToPrefixCoded(boxId), Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));





    private void addLocation(IndexWriter writer, String name, double lat, double lon) throws IOException{

      Document doc = new Document();

      doc.add(new Field("name", name, Field.Store.YES, Index.ANALYZED));

      doc.add(new Field("metafile", "doc", Store.YES, Index.ANALYZED));

      addSpatialLcnFields(lat, lon, doc);




      public static void main(String[] args) throws Exception{

            SpatialLuceneExample sle = new SpatialLuceneExample();

            Directory dir = new RAMDirectory();

            IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), MaxFieldLength.UNLIMITED);


            sle.addLocation(writer, "test", 39.9260, -75.1566);





            IndexSearcher searcher = new IndexSearcher(dir);

            DistanceQueryBuilder dq = new DistanceQueryBuilder(39.9260, -75.1566, 10d, LAT_FIELD, LON_FIELD, true);


            Query tq = new TermQuery(new Term("metafile", "doc"));

            TopDocs hits = searcher.search(tq, dq.getFilter(), 10);

            for(int i =0; i<hits.totalHits; i++){

                  Document doc = searcher.doc(hits.scoreDocs[i].doc);