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

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


这是课程:

Server.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");
}
}


客户端.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();
    }
}
}


RawLucene.java

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


DocumentBuilder.java

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


这是我启动应用程序的方式:


运行服务器类
使用所有三种方法运行Client类(初始运行。工作正常,并返回正确的结果)
运行Client类而不调用indexFiles方法。 (第二次运行。这是我得到的例外)


为什么代码第一次运行良好,而第二次运行会引发异常?

最佳答案

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

geode-lucene模块确实在内部使用RegionDirectory,但是它的使用方式与您使用的方式有所不同-它不是在客户端包装整个区域,而是在服务器端包装了单独的存储桶。

我认为这里发生的是RegionDirectory和基础FileSystem类使用一些geode API,当您在客户端上调用它们时,它们的行为会有所不同。特别是,我认为当FileSystem类正在寻找文件时,它使用的是Region.keySet,它与您的缓存客户端一起将返回在客户端缓存的文件列表。我认为这可以解释为什么您收到有关无文件的错误的信息。

非常遗憾的是RegionDirectory不是公共API,并且不真正支持您尝试使用它的方式,因为这看起来是一个很好的用例。

10-05 18:51