lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Liang, Jay (YBUSA-KOP)" <>
Subject Does spatial lucene in lucene 3.0 work at all?
Date Fri, 05 Feb 2010 17:48:40 GMT
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 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.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.util.NumericUtils;

public class SpatialLuceneExample{
    private static final String LAT_FIELD = "lat", LON_FIELD = "lon", TIER_PREFIX_FIELD =

    private double maxMiles = 25, minMiles = 1; // anything lower than 1 mile will just give
tier 15
    // see
    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
     * @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,
        document.add(new Field(LON_FIELD, NumericUtils.doubleToPrefixCoded(lon), Field.Store.YES,

        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
      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 =, dq.getFilter(), 10);
            for(int i =0; i<hits.totalHits; i++){
                  Document doc = searcher.doc(hits.scoreDocs[i].doc);

View raw message