【问题标题】:Apache Lucene LatLonPoint Query on GeodeGeode上的Apache Lucene LatLonPoint查询
【发布时间】:2017-09-26 12:20:07
【问题描述】:

我正在尝试对在 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: []

这里是类:

服务器.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");
}
}

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

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

  1. 运行服务器类
  2. 使用所有三种方法运行 Client 类(初始运行。工作正常并返回正确结果)
  3. 在不调用indexFiles 方法的情况下运行客户端类。 (第二次运行。这是我得到异常的地方)

为什么代码第一次运行良好,第二次运行抛出异常?

【问题讨论】:

    标签: java lucene geolocation geospatial geode


    【解决方案1】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-08
      • 1970-01-01
      相关资源
      最近更新 更多