gpt4 book ai didi

java - Geode 上的 Apache Lucene LatLonPoint 查询

转载 作者:行者123 更新时间:2023-12-02 12:12:36 26 4
gpt4 key购买 nike

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

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

这是类(class):

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

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

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

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

最佳答案

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

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

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

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

关于java - Geode 上的 Apache Lucene LatLonPoint 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46426405/

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