3

我找不到任何有关如何使用此 API 的完整示例。下面的代码没有给出任何结果。知道为什么吗?

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);
    }
}
4

2 回答 2

8

你看到javadocs了吗?这些文档又指向您正在寻找的SpatialExample.java 。我能做些什么让它们更明显?

如果您一心想要使用一对双打作为内部索引方法,那么请使用 PointVectorStrategy。但是,如果您改为使用 RecursivePrefixTreeStrategy,您将获得出色的过滤器性能。目前,PVS 在可扩展性方面做得更好。您可以将两者用于各自的好处。

只是快速查看您的示例,我发现您没有使用 SpatialStrategy.createIndexableFields()。目的是您使用它。

于 2012-11-29T17:24:11.313 回答
1

See the following link for example : http://mad4search.blogspot.in/2013/06/implementing-geospatial-search-using.html

于 2013-10-31T05:25:27.330 回答