如何使用Lucene 4.0空间API?
我找不到任何关于如何使用此API的完整示例。下面的代码没有给出任何结果。知道为什么吗如何使用Lucene 4.0空间API?,lucene,spatial,Lucene,Spatial,我找不到任何关于如何使用此API的完整示例。下面的代码没有给出任何结果。知道为什么吗 static String spatialPrefix = "_point"; static String latField = spatialPrefix + "lat"; static String lngField = spatialPrefix + "lon"; public static void main(String[] args) throws IOException { Spatia
static String spatialPrefix = "_point";
static String latField = spatialPrefix + "lat";
static String lngField = spatialPrefix + "lon";
public static void main(String[] args) throws IOException {
SpatialLuceneExample spatial = new SpatialLuceneExample();
spatial.addData();
IndexReader reader = DirectoryReader.open(modules.getDirectory());
IndexSearcher searcher = new IndexSearcher(reader);
searchAndUpdateDocument(38.9510000, -77.4107000, 100.0, searcher,
modules);
}
private void addLocation(IndexWriter writer, String name, double lat,
double lng) throws IOException {
Document doc = new Document();
doc.add(new org.apache.lucene.document.TextField("name", name,
Field.Store.YES));
doc.add(new org.apache.lucene.document.DoubleField(latField, lat,
Field.Store.YES));
doc.add(new org.apache.lucene.document.DoubleField(lngField, lng,
Field.Store.YES));
doc.add(new org.apache.lucene.document.TextField("metafile", "doc",
Field.Store.YES));
writer.addDocument(doc);
System.out.println("===== Added Doc to index ====");
}
private void addData() throws IOException {
IndexWriter writer = modules.getWriter();
addLocation(writer, "McCormick & Schmick's Seafood Restaurant",
38.9579000, -77.3572000);
addLocation(writer, "Jimmy's Old Town Tavern", 38.9690000, -77.3862000);
addLocation(writer, "Ned Devine's", 38.9510000, -77.4107000);
addLocation(writer, "Old Brogue Irish Pub", 38.9955000, -77.2884000);
//...
writer.close();
}
private final static Logger logger = LogManager
.getLogger(SpatialTools.class);
public static void searchAndUpdateDocument(double lo, double la,
double dist, IndexSearcher searcher, LuceneModules modules) {
SpatialContext ctx = SpatialContext.GEO;
SpatialArgs args = new SpatialArgs(SpatialOperation.IsWithin,
ctx.makeCircle(lo, la, DistanceUtils.dist2Degrees(dist,
DistanceUtils.EARTH_MEAN_RADIUS_KM)));
PointVectorStrategy strategy = new PointVectorStrategy(ctx, "_point");
// RecursivePrefixTreeStrategy recursivePrefixTreeStrategy = new
// RecursivePrefixTreeStrategy(grid, fieldName);
// How to use it?
Query makeQueryDistanceScore = strategy.makeQueryDistanceScore(args);
LuceneSearcher instance = LuceneSearcher.getInstance(modules);
instance.getTopResults(makeQueryDistanceScore);
//no results
Filter geoFilter = strategy.makeFilter(args);
try {
Sort chainedSort = new Sort().rewrite(searcher);
TopDocs docs = searcher.search(new MatchAllDocsQuery(), geoFilter,
10000, chainedSort);
logger.debug("search finished, num: " + docs.totalHits);
//no results
for (ScoreDoc scoreDoc : docs.scoreDocs) {
Document doc = searcher.doc(scoreDoc.doc);
double la1 = Double.parseDouble(doc.get(latField));
double lo1 = Double.parseDouble(doc.get(latField));
double distDEG = ctx.getDistCalc().distance(
args.getShape().getCenter(), lo1, la1);
logger.debug("dist deg: : " + distDEG);
double distKM = DistanceUtils.degrees2Dist(distDEG,
DistanceUtils.EARTH_MEAN_RADIUS_KM);
logger.debug("dist km: : " + distKM);
}
} catch (IOException e) {
logger.error("fail to get the search result!", e);
}
}
你看到那张照片了吗?这些文档依次指向您要查找的对象。我该怎么做才能让它们更明显
如果您打算使用一对double作为内部索引方法,那么请使用PointVectorStrategy。但是,如果改用RecursivePrefixtReastegy,您将获得更好的过滤器性能。目前,PVS在可伸缩性方面做得更好。您可以使用这两种方法来获得各自的好处
快速看一下您的示例,我发现您没有使用SpatialStrategy.createIndexableFields()。其目的是让您使用它。请参见以下链接,例如:David-您能举个例子吗?目前,我正在使用PointVectorStrategy进行索引,因为它索引速度更快。但是,我无法让pvs过滤器生成结果。如何使用pvs进行索引,如何使用RecursivePrefixtReastegy进行搜索/筛选?您不能。达米安:不是这个或那个。您可以使用RPT进行过滤(它做得最好),使用PVS进行排序或提升相关性(它做得最好)。a、 罗德:我不知道为什么PVS不适合你;提供Lucene java用户列表或堆栈溢出问题的更多详细信息。SpatialSample.java链接已失效。这里有一个新的: