Java 基于Geode的Apache-Lucene-LatLonPoint查询
我试图在一个在测地区域上创建的Lucene索引上索引一些地理空间数据,并使用Lucene的LatLonPoint类查询方法(如newDistanceQuery或newPolygonQuery方法)对这些数据运行查询。运行应用程序一次返回正确的结果,但当我第二次运行代码时,会出现以下异常:Java 基于Geode的Apache-Lucene-LatLonPoint查询,java,lucene,geolocation,geospatial,geode,Java,Lucene,Geolocation,Geospatial,Geode,我试图在一个在测地区域上创建的Lucene索引上索引一些地理空间数据,并使用Lucene的LatLonPoint类查询方法(如newDistanceQuery或newPolygonQuery方法)对这些数据运行查询。运行应用程序一次返回正确的结果,但当我第二次运行代码时,会出现以下异常: org.apache.lucene.index.IndexNotFoundException: no segments* file found in RegionDirectory@4218500f lock
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");
}
}
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();
}
}
}
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;
}
}
以下是我启动应用程序的方式:
运行服务器类
在初始运行时使用所有三个方法运行客户机类。工作正常并返回正确的结果
运行客户机类而不调用indexFiles方法。第二轮。这就是我得到例外的地方
为什么代码第一次运行正常,第二次运行时抛出异常?看起来您将geode的公共API与内部类RegionDirectory混合使用。公共API只支持通过直接向区域添加对象和使用LuceneService.createQueryFactory进行查询来添加文档 geode-lucene模块确实在内部使用RegionDirectory,但它的使用方式与您使用的略有不同——它不是从客户端包装整个区域,而是在服务器端包装各个存储桶 我认为这里发生的事情是RegionDirectory和底层文件系统类正在使用一些GeodeAPI,当您在客户机上调用它们时,它们的行为会有所不同。特别是,我认为当FileSystem类查找文件时,它使用Region.keySet,它与缓存客户端一起将返回客户端缓存的文件列表。我想这就解释了为什么你会得到关于没有文件的错误 遗憾的是RegionDirectory不是一个公共API,并且不支持您尝试使用它的方式,因为这看起来是一个很好的用例
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;
}
}