lucene 对地理位置的搜索

package com.lucenetest;

import java.io.IOException;
import java.text.ParseException;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.IntField;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexableField;
import org.apache.lucene.queries.function.ValueSource;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.spatial.SpatialStrategy;
import org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy;
import org.apache.lucene.spatial.prefix.tree.GeohashPrefixTree;
import org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree;
import org.apache.lucene.spatial.query.SpatialArgs;
import org.apache.lucene.spatial.query.SpatialOperation;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;

import com.spatial4j.core.context.SpatialContext;
import com.spatial4j.core.distance.DistanceUtils;
import com.spatial4j.core.shape.Point;
import com.spatial4j.core.shape.Shape;

public class NewLocation {
	private SpatialContext ctx;
	private SpatialStrategy strategy;

	private Directory directory;

	protected void init() {
		this.ctx = SpatialContext.GEO;
		int maxLevels = 11;
		SpatialPrefixTree grid = new GeohashPrefixTree(ctx, maxLevels);
		this.strategy = new RecursivePrefixTreeStrategy(grid, "myGeoField");
		this.directory = new RAMDirectory();
	}
	
	protected void indexPoints() throws IOException, ParseException {
		IndexWriterConfig iwConfig = new IndexWriterConfig(Version.LUCENE_4_9,null);
		IndexWriter indexWriter = new IndexWriter(directory, iwConfig);
		
		indexWriter.addDocument(newSampleDocument( 2, ctx.makePoint(-80.93, 33.77)));
		indexWriter.addDocument(newSampleDocument( 4, ctx.readShapeFromWkt("POINT(60.9289094 -50.7693246)")));
		indexWriter.addDocument(newSampleDocument( 20, ctx.makePoint(0.1,0.1), ctx.makePoint(0, 0)));
		
		indexWriter.close();
	}
	
	private Document newSampleDocument(int id, Shape... shapes) {
		Document doc = new Document();
		doc.add(new IntField("id", id, Field.Store.YES));
		for (Shape shape : shapes) {
			for (IndexableField f : strategy.createIndexableFields(shape)) {
				doc.add(f);
			}
			Point pt = (Point) shape;
			doc.add(new StoredField(strategy.getFieldName(), pt.getX()+" "+pt.getY()));
		}
		return doc;
	}
	
	protected void search() throws Exception {
		IndexReader indexReader = DirectoryReader.open(directory);
		IndexSearcher indexSearcher = new IndexSearcher(indexReader);
		Sort idSort = new Sort(new SortField("id", SortField.Type.INT));
		
		{
			SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects,
					          ctx.makeCircle(-80.0, 33.0, DistanceUtils.dist2Degrees(200, DistanceUtils.EARTH_MEAN_RADIUS_KM)));
			Filter filter = strategy.makeFilter(args);
			TopDocs docs = indexSearcher.search(new MatchAllDocsQuery(), filter, 10, idSort);
			
			for (ScoreDoc doc : docs.scoreDocs) {
				Document doc1 = indexSearcher.doc(doc.doc);
				String doc1Str = doc1.getField(strategy.getFieldName()).stringValue();
				int spaceIdx = doc1Str.indexOf(' ');
				double x = Double.parseDouble(doc1Str.substring(0, spaceIdx));
				double y = Double.parseDouble(doc1Str.substring(spaceIdx+1));
				double doc1DistDEG = ctx.calcDistance(args.getShape().getCenter(), x, y);
				System.out.println("x=>" + x + " y=>" + y + "distance=>" + doc1DistDEG);
			}
		}
		
		{
			Point pt = ctx.makePoint(60, -50);
			ValueSource valueSource = strategy.makeDistanceValueSource(pt, DistanceUtils.DEG_TO_KM);//the distance (in km)
			Sort distSort = new Sort(valueSource.getSortField(false)).rewrite(indexSearcher);//false=asc dist
			TopDocs docs = indexSearcher.search(new MatchAllDocsQuery(), 10, distSort);
			for (ScoreDoc doc : docs.scoreDocs) {
				Document doc1 = indexSearcher.doc(doc.doc);
				String doc1Str = doc1.getField(strategy.getFieldName()).stringValue();
				int spaceIdx = doc1Str.indexOf(' ');
				double x = Double.parseDouble(doc1Str.substring(0, spaceIdx));
				double y = Double.parseDouble(doc1Str.substring(spaceIdx+1));
				double doc1DistDEG = ctx.calcDistance(pt, x, y);
				System.out.println("x=>" + x + " y=>" + y + "distance=>" + doc1DistDEG);
			}
		}
		indexReader.close();
	}
	
	public static void main(String[] orgs) {
		NewLocation n = new NewLocation();
		n.init();
		try {
			n.indexPoints();
			n.search();
		} catch (IOException | ParseException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}

从Lucene的doc中抄过来的.


阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭