6.3.3. Running Infinispan Query DSL-based Queries


Once Infinispan Query DSL-based queries have been enabled, obtain a QueryFactory from the SearchManager in order to run a DSL-based query.
A query can then be constructed as follows:
import org.infinispan.query.Search;
import org.infinispan.query.dsl.QueryFactory;
import org.infinispan.query.dsl.Query;
...
QueryFactory qf = Search.getSearchManager(cache).getQueryFactory();
Query q = qf.from(User.class)
    .having("name").eq("John")
    .toBuilder().build();
List list = q.list();
assertEquals(1, list.size());
assertEquals("John", list.get(0).getName());
assertEquals("Doe", list.get(0).getSurname());
It is also possible to combine multiple conditions with boolean operators, including sub-conditions. For example:
Query q = qf.from(User.class)
    .having("name").eq("John")
    .and().having("surname").eq("Doe")
    .and().not(qf.having("address.street").like("%Tanzania%").or().having("address.postCode").in("TZ13", "TZ22"))
    .toBuilder().build();
This query API simplifies the way queries are written by not exposing the user to the low level details of constructing Lucene query objects. It also has the benefit of being available to remote Hot Rod clients.
The following example shows how to write a query for the Book entity.

Example 6.3. Querying the Book Entity

import org.infinispan.query.dsl.*;
// get the search manager from the cache, as in previous examples:
SearchManager searchManager = org.infinispan.query.Search.getSearchManager(cache);
// get the DSL query factory, to be used for constructing the Query object:
QueryFactory qf = searchManager.getQueryFactory();
// create a query for all the books that have a title which contains the word "engine":
org.infinispan.query.dsl.Query query = qf.from(Book.class)
      .having("title").like("%engine%")
      .toBuilder().build();
// get the results:List<Book> list = query.list();
Red Hat logoGithubRedditYoutubeTwitter

Learn

Try, buy, & sell

Communities

About Red Hat Documentation

We help Red Hat users innovate and achieve their goals with our products and services with content they can trust.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

© 2024 Red Hat, Inc.