1

Created a Lucene index in Geode with the code provided in documentation. Then put a couple of objects in the region and queried the region with a Lucene query, which documentation also shows how. But the query result is always empty. Here is my code:

Starting a Geode server and creating a Lucene index in it:

    public static void startServerAndLocator() throws InterruptedException {
    ServerLauncher serverLauncher = new ServerLauncher.Builder()
            .setMemberName("server1")
            .setServerPort(40404)
            .set("start-locator", "127.0.0.1[10334]")
            .build();

    ServerLauncher.ServerState state = serverLauncher.start();
    _logger.info(state.toString());

    Cache cache = new CacheFactory().create();
    createLuceneIndex(cache);
    cache.createRegionFactory(RegionShortcut.PARTITION).create("test");
}

public static void createLuceneIndex(Cache cache) throws InterruptedException {
    LuceneService luceneService = LuceneServiceProvider.get(cache);
    luceneService.createIndexFactory()
            .addField("fullName")
            .addField("salary")
            .addField("phone")
            .create("employees", "/test");
}

Putting objects in region and querying:

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

Person is a basic POJO class with three fields (name, salary, phone). What am I doing wrong here? Why the query result is empty?

Fatih Barmanbay
  • 61
  • 3
  • 10

3 Answers3

1

If you do a query with just fullName, do you still get no results?

I think the issue is that salary and phone are getting stored as IntPoint. You could make them String fields in your Person class so they get stored as strings, or you could use an integer query, eg.

luceneService.createLuceneQueryFactory()
   .create("employees", "test", 
       index -> IntPoint.newExactQuery("salary", 30000))
Dan Smith
  • 481
  • 2
  • 3
0

The events are still in AsyncEventQueue and not flushed into index yet. (It might take 10+ milliseconds). The AsyncEventQueue's default flush interval is 10ms.

You need to add following code before doing query. luceneService.waitUntilFlushed("employees", "/test", 30000, TimeUnit.MILLISECONDS);

0

Another issue in the program is:

The salary field is a integer. But the query try to do a string query on the salary field and mixed with another string field.

To query on a integer field mixed with a string field, you need to create a LuceneQueryProvider to bind a StringQueryParser with a IntPoint.newExactQuery(or other IntPoint queries).

If you just want to try the basic functionality, you can only use String fields for the time being. (i.e. change the salary field to String)