gpt4 book ai didi

java - Spatial lucene简单查询不起作用?

转载 作者:行者123 更新时间:2023-12-01 16:08:11 26 4
gpt4 key购买 nike

有人有使用lucene的空间搜索组件(lucene 3.0)的经验吗?

我尝试了一个非常简单的示例,但无法让搜索返回任何内容,请参阅下面的所有代码

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 = ctp.bestFit(maxMiles), endTier = ctp.bestFit(minMiles);

/**
* 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);
writer.addDocument(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);

writer.commit();
writer.close();

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(dq.getQuery(tq), 10);
for(int i =0; i<hits.totalHits; i++){
Document doc = searcher.doc(hits.scoreDocs[i].doc);
System.out.println(doc.get("name"));
}
}
}

任何帮助/评论将不胜感激。

最佳答案

您需要将层前缀添加到查询构建器:

    DistanceQueryBuilder dq = new DistanceQueryBuilder(coord.getLat(),
coord.getLon(), miles, Spatial.LAT_FIELD, Spatial.LON_FIELD,
Spatial.TIER_PREFIX_FIELD, false);

关于java - Spatial lucene简单查询不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2209089/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com