Tom Donohue Tom Donohue

Infinispan Query DSL: quick example

How do you use the Query DSL in Infinispan? How do you search for objects in the in-memory database that have certain properties?

First add the infinispan-query Maven dependency:

<dependency>
    <groupId>org.infinispan</groupId>
    <artifactId>infinispan-query</artifactId>
    <version>${infinispan.version}</version>
</dependency>

Then you need to add annotations to the fields that you’d like to index on your object. You can add annotations on both fields and methods:

import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Indexed;

@Indexed
public class Worker {

    private Address workAddress;
    private Address homeAddress;

    @Field
    public String getHomeAddressLine1() {
        return homeAddress.getLine1();
    }

}

By default, this field will now be searchable using the property homeAddressLine1 (yes, getter methods are resolved to camelCase names):

QueryFactory qf = Search.getQueryFactory(cache.getCache("yourCacheName"));

Query q = qf.from(Worker.class)
    .having("homeAddressLine1").eq("1 High Street")
    .or(qf.having("contractType").eq("Full_time"))
    .build();

List<Worker> list = q.list();

Comments

What do you think? You can use Markdown in your comment.

To write code, indent each line with 4 spaces. Or, to paste a lot of code, you can put it in pastebin.com and share the link in your comment.