Ickle Queries with Embedded Infinispan

What You Will Learn

How to annotate entities for indexing, configure an indexed cache, and execute Ickle queries to search for data in an embedded Infinispan cache.

Prerequisites

  • Java 17+

Step 1: Add Dependencies

Add the Infinispan core, API, and query modules to your pom.xml:

<dependency>
   <groupId>org.infinispan</groupId>
   <artifactId>infinispan-api</artifactId>
</dependency>
<dependency>
   <groupId>org.infinispan</groupId>
   <artifactId>infinispan-core</artifactId>
</dependency>
<dependency>
   <groupId>org.infinispan</groupId>
   <artifactId>infinispan-query</artifactId>
</dependency>

Step 2: Define an Indexed Entity

Annotate your entity class with @Indexed and mark searchable fields with @Basic:

@Indexed
public class Person {
   @Basic
   String name;

   @Basic
   String surname;

   public Person(String name, String surname) {
      this.name = name;
      this.surname = surname;
   }

Step 3: Configure an Indexed Cache

Enable indexing on the cache configuration and register the indexed entity:

      // Create cache config
      ConfigurationBuilder builder = new ConfigurationBuilder();
      builder.indexing()
              .enable()
              .storage(IndexStorage.LOCAL_HEAP)
              .addIndexedEntity(Person.class);

      // Obtain the cache
      cache = cacheManager.administration()
              .withFlags(CacheContainerAdmin.AdminFlag.VOLATILE)
              .getOrCreateCache("cache", builder.build());

Step 4: Run an Ickle Query

Store some entries and query them using the Ickle query language:

      // Store some entries
      cache.put("person1", new Person("William", "Shakespeare"));
      cache.put("person2", new Person("William", "Wordsworth"));
      cache.put("person3", new Person("John", "Milton"));
      // Construct a query
      Query<Person> query = cache.query("from org.infinispan.tutorial.simple.query.Person where name = 'William'");
      // Execute the query
      return query.execute().list();

Step 5: Run the Tutorial

mvn package exec:java

The output shows the two persons named William (Shakespeare and Wordsworth).

What’s Next