-
Language:
English
-
Language:
English
Chapter 6. The Infinispan Query DSL
The Infinispan Query DSL provides a simplified way of writing queries, and is agnostic of the underlying query mechanisms. This provides an alternative query engine to Lucene, while allowing use of the same query language or API.
JBoss Data Grid also includes indexless queries, which do not require a configured index and make everything easily searchable. However, indexless queries are only available with the new Query DSL for both the remote and embedded DSL-based queries (see Section 6.2, “Enabling Infinispan Query DSL-based Queries”). The Hibernate Search/Lucene-based API cannot use indexless queries.
Warning
The Infinispan Query DSL and Remote Querying described in this chapter are Technology Preview and not supported in JBoss Data Grid.
6.1. Creating Queries with Infinispan Query DSL
The new query API is located in the org.infinispan.query.dsl package. A query is created with the assistance of the
QueryFactory
instance, which is obtained using Search.getQueryFactory()
. Each QueryFactory
instance is bound to the one cache instance, and is a stateless and thread-safe object that can be used for creating multiple parallel queries.
The Infinispan Query DSL uses the following steps to perform a query.
- A query is created by invocating the
from(Class entityType)
method, which returns aQueryBuilder
object that is responsible for creating queries for the specified entity class from the given cache. - The
QueryBuilder
accumulates search criteria and configuration specified through invoking its DSL methods, and is used to build aQuery
object by invoking theQueryBuilder.build()
method, which completes the construction. TheQueryBuilder
object cannot be used for constructing multiple queries at the same time except for nested queries, however it can be reused afterwards. - Invoke the
list()
method of theQuery
object to execute the query and fetch the results. Once executed, theQuery
object is not reusable. If new results must be fetched, a new instance must be obtained by callingQueryBuilder.build()
.
Important
A query targets a single entity type and is evaluated over the contents of a single cache. Running a query over multiple caches, or creating queries targeting several entity types is not supported.