关于Geode的Apache Lucene LatLonPoint查询

时间:2017-09-26 12:20:08

标签: java lucene geolocation geospatial geode

我正在尝试索引在Geode区域上创建的Lucene索引上的一些地理空间数据,并使用Lucene's LatLonPoint类查询方法(如newDistanceQuery或{运行对这些数据的查询{1}}方法)。运行应用程序一次返回正确的结果,但是当我第二次运行代码时,我得到以下异常:

newPolygonQuery

以下是课程:

Server.java

org.apache.lucene.index.IndexNotFoundException: 
no segments* file found in RegionDirectory@4218500f lockFactory=
org.apache.lucene.store.SingleInstanceLockFactory@4bff64c2: files: []

Client.java

public class Server {
final static Logger _logger = LoggerFactory.getLogger(Server.class);

public static void main(String[] args) throws InterruptedException {
    startServer();
}

/** Start a Geode Cache Server with a locator */
public static void startServer() throws InterruptedException {
    ServerLauncher serverLauncher = new ServerLauncher.Builder()
            .setMemberName("server1")
            .setServerPort(40404)
            .set("start-locator", "127.0.0.1[10334]")
            .set("jmx-manager", "true")
            .set("jmx-manager-start", "true")
            .build();

    ServerLauncher.ServerState state = serverLauncher.start();
    _logger.info(state.toString());

    Cache cache = new CacheFactory().create();
    createLuceneIndex(cache);
    cache.createRegionFactory(RegionShortcut.PARTITION).create("locationsRegion");
}

/** Create a Lucene Index with given cache */
public static void createLuceneIndex(Cache cache) throws InterruptedException {
    LuceneService luceneService = LuceneServiceProvider.get(cache);
    luceneService.createIndexFactory()
            .addField("NAME")
            .addField("LOCATION")
            .addField("COORDINATES")
            .create("locationsIndex", "locationsRegion");
}
}

RawLucene.java

public class Client {
private static ClientCache cache;
private static Region<Integer, Document> region;

public static void main(String[] args) throws LuceneQueryException, InterruptedException, IOException {
    init();
    indexFiles();
    search();
}

/** Initialize the client cache and region */
private static void init() {
    cache = new ClientCacheFactory()
            .addPoolLocator("localhost", 10334)
            .create();

    if (cache != null) {
        region = cache.<Integer, Document>createClientRegionFactory(
                ClientRegionShortcut.CACHING_PROXY).create("locationsRegion");
    } else {
        throw new NullPointerException("Client cache is null");
    }
}

/** Add documents to the Lucene index */
private static void indexFiles() {
    // Dummy data
    List<Document> locations = Arrays.asList(
            DocumentBuilder.newSampleDocument("Exastax", 40.984929, 29.133506),
            DocumentBuilder.newSampleDocument("Galata Tower", 41.025826, 28.974378),
            DocumentBuilder.newSampleDocument("St. Peter and St. Paul Church", 41.024757, 28.972950));

    // Standart IndexWriter initialization.
    Analyzer analyzer = new StandardAnalyzer();
    // Create a directory from geode region
    Directory directory = RawLucene.returnRegionDirectory(cache, region, "locationsIndex");
    IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
    IndexWriter indexWriter;
    try {
        indexWriter = new IndexWriter(directory, indexWriterConfig);
        indexWriter.addDocuments(locations);
        indexWriter.commit();
        indexWriter.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

/** Search in the Lucene index */
private static void search() {
    try {
        DirectoryReader reader = DirectoryReader.open(RawLucene.returnRegionDirectory(cache, region, "locationsIndex"));
        IndexSearcher indexSearcher = new IndexSearcher(reader);

        Query query = LatLonPoint.newDistanceQuery("COORDINATES", 41.024873, 28.974346, 500);
        ScoreDoc[] scoreDocs = indexSearcher.search(query, 10).scoreDocs;
        for (int i = 0; i < scoreDocs.length; i++) {
            Document doc = indexSearcher.doc(scoreDocs[i].doc);
            System.out.println(doc.get("NAME") + " --- " + doc.get("LOCATION"));
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

DocumentBuilder.java

public class RawLucene {
public static Directory returnRegionDirectory(ClientCache cache, Region region, String indexName) {
    return new RegionDirectory(region,new FileSystemStats(cache.getDistributedSystem(), indexName));
}
}

这是我启动应用的方式:

  1. 运行Server类
  2. 使用所有三种方法运行Client类(初始运行。正常工作并返回正确的结果)
  3. 运行Client类而不调用public class DocumentBuilder { public static Document newSampleDocument(String name, Double lat, Double lon) { Document document = new Document(); document.add(new StoredField("NAME", name)); document.add(new StoredField("LOCATION", lat + " " + lon)); document.add(new LatLonPoint("COORDINATES", lat, lon)); return document; } } 方法。 (第二次运行。这是我得到例外的地方)
  4. 为什么代码第一次正常运行并在第二次运行时抛出异常?

1 个答案:

答案 0 :(得分:1)

看起来您正在使用geode的公共API以及内部类RegionDirectory。公共API仅支持通过将对象直接添加到区域来添加文档,以及使用LuceneService.createQueryFactory()进行查询。

geode-lucene模块在内部使用RegionDirectory,但它使用它的方式与你使用它有点不同 - 它不是从客户端包装整个区域,而是在服务器端包装各个桶。

我认为这里发生的事情是RegionDirectory和底层的FileSystem类正在使用一些geode API,当你在客户端上调用它们时,它们的行为会有所不同。特别是,我认为当FileSystem类正在查找文件时,它正在使用Region.keySet,它与您的缓存客户端将返回客户端缓存的文件列表。我认为这解释了为什么你得到关于没有文件的错误。

RegionDirectory不是公共API并且不支持您尝试使用它的方式太糟糕了,因为这看起来是一个很好的用例。