使用documentation中提供的代码在Geode中创建Lucene索引。然后在该区域中放置几个对象,并使用Lucene查询查询该区域,该文档还显示了该方法。但查询结果始终为空。这是我的代码:
启动Geode服务器并在其中创建Lucene索引:
public static void testGeodeServer() throws LuceneQueryException, InterruptedException {
ClientCache cache = new ClientCacheFactory()
.addPoolLocator("localhost", 10334)
.create();
Region<Integer, Person> region = cache
.<Integer, Person>createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY).create("test");
List<Person> persons = Arrays.asList(
new Person("John", 3000, 5556644),
new Person("Jane", 4000, 6664488),
new Person("Janet", 3500, 1112233));
for (int i = 0; i < persons.size(); i++) {
region.put(i, persons.get(i));
}
LuceneService luceneService = LuceneServiceProvider.get(cache);
LuceneQuery<Integer, Person> query = luceneService.createLuceneQueryFactory()
.setLimit(10)
.create("employees", "/test", "fullName:John AND salary:3000", "salary");
Collection<Person> values = query.findValues();
System.out.println("Query results:");
for (Person person : values) {
System.out.println(person);
}
cache.close();
}
将对象放入区域并查询:
<img />
Person是一个基本的POJO类,有三个字段(名称,工资,电话)。 我在这做错了什么?为什么查询结果为空?
答案 0 :(得分:1)
如果您只使用fullName进行查询,是否仍然没有结果?
我认为问题是工资和手机被存储为IntPoint。您可以在Person类中创建String字段,以便将它们存储为字符串,或者您可以使用整数查询,例如。
luceneService.createLuceneQueryFactory()
.create("employees", "test",
index -> IntPoint.newExactQuery("salary", 30000))
答案 1 :(得分:0)
事件仍在AsyncEventQueue中,尚未刷新到索引中。 (可能需要10+毫秒)。 AsyncEventQueue的默认刷新间隔为10毫秒。
在进行查询之前,您需要添加以下代码。 luceneService.waitUntilFlushed(“employees”,“/ test”,30000,TimeUnit.MILLISECONDS);
答案 2 :(得分:0)
该计划的另一个问题是:
薪水字段是整数。但是查询尝试在salary字段上执行字符串查询并与另一个字符串字段混合。
要查询与字符串字段混合的整数字段,您需要创建LuceneQueryProvider以将StringQueryParser与IntPoint.newExactQuery(或其他IntPoint查询)绑定。
如果您只想尝试基本功能,则暂时只能使用字符串字段。 (即将薪水字段更改为String)