此内容没有您所选择的语言版本。
Chapter 5. Querying
Infinispan Query can execute Lucene queries and retrieve domain objects from a Red Hat JBoss Data Grid cache.
Procedure 5.1. Prepare and Execute a Query
- Get
SearchManager
of an indexing enabled cache as follows:SearchManager manager = Search.getSearchManager(cache);
SearchManager manager = Search.getSearchManager(cache);
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Create a
QueryBuilder
to build queries forMyth.class
as follows:final org.hibernate.search.query.dsl.QueryBuilder queryBuilder = manager.buildQueryBuilderForClass(Myth.class).get();
final org.hibernate.search.query.dsl.QueryBuilder queryBuilder = manager.buildQueryBuilderForClass(Myth.class).get();
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Create an Apache Lucene query that queries the
Myth.class
class' atributes as follows:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
5.1. Building Queries 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
Query Module queries are built on Lucene queries, allowing users to use any Lucene query type. When the query is built, Infinispan Query uses org.infinispan.query.CacheQuery as the query manipulation API for further query processing.
With the Lucene API, use either the query parser (simple queries) or the Lucene programmatic API (complex queries). For details, see the online Lucene documentation or a copy of Lucene in Action or Hibernate Search in Action.
5.1.2. Building a Lucene Query 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
Using the Lucene programmatic API, it is possible to write full-text queries. However, when using Lucene programmatic API, the parameters must be converted to their string equivalent and must also apply the correct analyzer to the right field. A ngram analyzer for example uses several ngrams as the tokens for a given word and should be searched as such. It is recommended to use the
QueryBuilder
for this task.
The Lucene-based query API is fluent. This API has a following key characteristics:
- Method names are in English. As a result, API operations can be read and understood as a series of English phrases and instructions.
- It uses IDE autocompletion which helps possible completions for the current input prefix and allows the user to choose the right option.
- It often uses the chaining method pattern.
- It is easy to use and read the API operations.
To use the API, first create a query builder that is attached to a given indexed type. This
QueryBuilder
knows what analyzer to use and what field bridge to apply. Several QueryBuilder
s (one for each type involved in the root of your query) can be created. The QueryBuilder
is derived from the SearchFactory
.
Search.getSearchManager(cache).buildQueryBuilderForClass(Myth.class).get();
Search.getSearchManager(cache).buildQueryBuilderForClass(Myth.class).get();
The analyzer, used for a given field or fields can also be overridden.
QueryBuilder mythQB = searchFactory.buildQueryBuilder() .forEntity( Myth.class ) .overridesForField("history","stem_analyzer_definition") .get();
QueryBuilder mythQB = searchFactory.buildQueryBuilder()
.forEntity( Myth.class )
.overridesForField("history","stem_analyzer_definition")
.get();
The query builder is now used to build Lucene queries.
5.1.2.1. Keyword Queries 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
The following example shows how to search for a specific word:
Query luceneQuery = mythQB.keyword().onField("history").matching("storm").createQuery();
Query luceneQuery = mythQB.keyword().onField("history").matching("storm").createQuery();
Parameter | Description |
---|---|
keyword() | Use this parameter to find a specific word |
onField() | Use this parameter to specify in which lucene field to search the word |
matching() | use this parameter to specify the match for search string |
createQuery() | creates the Lucene query object |
- The value "storm" is passed through the
history
FieldBridge
. This is useful when numbers or dates are involved. - The field bridge value is then passed to the analyzer used to index the field
history
. This ensures that the query uses the same term transformation than the indexing (lower case, ngram, stemming and so on). If the analyzing process generates several terms for a given word, a boolean query is used with theSHOULD
logic (roughly anOR
logic).
To search a property that is not of type string.
Note
In plain Lucene, the
Date
object had to be converted to its string representation (in this case the year)
This conversion works for any object, provided that the
FieldBridge
has an objectToString
method (and all built-in FieldBridge
implementations do).
The next example searches a field that uses ngram analyzers. The ngram analyzers index succession of ngrams of words, which helps to avoid user typos. For example, the 3-grams of the word hibernate are hib, ibe, ber, rna, nat, ate.
The matching word "Sisiphus" will be lower-cased and then split into 3-grams: sis, isi, sip, phu, hus. Each of these ngram will be part of the query. The user is then able to find the Sysiphus myth (with a
y
). All that is transparently done for the user.
Note
If the user does not want a specific field to use the field bridge or the analyzer then the
ignoreAnalyzer()
or ignoreFieldBridge()
functions can be called.
To search for multiple possible words in the same field, add them all in the matching clause.
//search document with storm or lightning in their history Query luceneQuery = mythQB.keyword().onField("history").matching("storm lightning").createQuery();
//search document with storm or lightning in their history
Query luceneQuery =
mythQB.keyword().onField("history").matching("storm lightning").createQuery();
To search the same word on multiple fields, use the
onFields
method.
Query luceneQuery = mythQB .keyword() .onFields("history","description","name") .matching("storm") .createQuery();
Query luceneQuery = mythQB
.keyword()
.onFields("history","description","name")
.matching("storm")
.createQuery();
Sometimes, one field should be treated differently from another field even if searching the same term, use the
andField()
method for that.
In the previous example, only field name is boosted to 5.
5.1.2.2. Fuzzy Queries 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
To execute a fuzzy query (based on the Levenshtein distance algorithm), start like a
keyword
query and add the fuzzy flag.
The
threshold
is the limit above which two terms are considering matching. It is a decimal between 0 and 1 and the default value is 0.5. The prefixLength
is the length of the prefix ignored by the "fuzzyness". While the default value is 0, a non zero value is recommended for indexes containing a huge amount of distinct terms.
5.1.2.3. Wildcard Queries 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
Wildcard queries can also be executed (queries where some of parts of the word are unknown). The
?
represents a single character and *
represents any character sequence. Note that for performance purposes, it is recommended that the query does not start with either ?
or *
.
Note
Wildcard queries do not apply the analyzer on the matching terms. Otherwise the risk of
*
or ?
being mangled is too high.
5.1.2.4. Phrase Queries 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
So far we have been looking for words or sets of words, the user can also search exact or approximate sentences. Use
phrase()
to do so.
Query luceneQuery = mythQB .phrase() .onField("history") .sentence("Thou shalt not kill") .createQuery();
Query luceneQuery = mythQB
.phrase()
.onField("history")
.sentence("Thou shalt not kill")
.createQuery();
Approximate sentences can be searched by adding a slop factor. The slop factor represents the number of other words permitted in the sentence: this works like a within or near operator.
5.1.2.5. Range Queries 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
A range query searches for a value in between given boundaries (included or not) or for a value below or above a given boundary (included or not).
5.1.2.6. Combining Queries 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
Queries can be aggregated (combine) to create more complex queries. The following aggregation operators are available:
SHOULD
: the query should contain the matching elements of the subquery.MUST
: the query must contain the matching elements of the subquery.MUST NOT
: the query must not contain the matching elements of the subquery.
The subqueries can be any Lucene query including a boolean query itself. Following are some examples:
5.1.2.7. Query Options 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
The following is a summary of query options for query types and fields:
boostedTo
(on query type and on field) boosts the query or field to a provided factor.withConstantScore
(on query) returns all results that match the query and have a constant score equal to the boost.filteredBy(Filter)
(on query) filters query results using theFilter
instance.ignoreAnalyzer
(on field) ignores the analyzer when processing this field.ignoreFieldBridge
(on field) ignores the field bridge when processing this field.
The following example illustrates how to use these options:
5.1.3. Build a Query with Infinispan Query 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
5.1.3.1. Generality 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
After building the Lucene query, wrap it within a Infinispan CacheQuery. The query searches all indexed entities and returns all types of indexed classes unless explicitly configured not to do so.
Example 5.1. Wrapping a Lucene Query in an Infinispan CacheQuery
CacheQuery cacheQuery = Search.getSearchManager(cache).getQuery(luceneQuery);
CacheQuery cacheQuery = Search.getSearchManager(cache).getQuery(luceneQuery);
For improved performance, restrict the returned types as follows:
Example 5.2. Filtering the Search Result by Entity Type
The first part of the second example only returns the matching
Customer
s. The second part of the same example returns matching Actor
s and Item
s. The type restriction is polymorphic. As a result, if the two subclasses Salesman
and Customer
of the base class Person
return, specify Person.class
to filter based on result types.
5.1.3.2. Pagination 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
To avoid performance degradation, it is recommended to restrict the number of returned objects per query. A user navigating from one page to another page is a very common use case. The way to define pagination is similar to defining pagination in a plain HQL or Criteria query.
Example 5.3. Defining pagination for a search query
CacheQuery cacheQuery = Search.getSearchManager(cache).getQuery(luceneQuery, Customer.class); cacheQuery.firstResult(15); //start from the 15th element cacheQuery.maxResults(10); //return 10 elements
CacheQuery cacheQuery = Search.getSearchManager(cache).getQuery(luceneQuery, Customer.class);
cacheQuery.firstResult(15); //start from the 15th element
cacheQuery.maxResults(10); //return 10 elements
Note
The total number of matching elements, despite the pagination, is accessible via
cacheQuery.getResultSize()
.
5.1.3.3. Sorting 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
Apache Lucene contains a flexible and powerful result sorting mechanism. The default sorting is by relevance and is appropriate for a large variety of use cases. The sorting mechanism can be changed to sort by other properties using the Lucene Sort object to apply a Lucene sorting strategy.
Example 5.4. Specifying a Lucene Sort
Note
Fields used for sorting must not be tokenized. For more information about tokenizing, see Section 4.1.2, “@Field”.
5.1.3.4. Projection 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
In some cases, only a small subset of the properties is required. Use Infinispan Query to return a subset of properties as follows:
Example 5.5. Using Projection Instead of Returning the Full Domain Object
The Query Module extracts properties from the Lucene index and converts them to their object representation and returns a list of
Object[]
. Projections prevent a time consuming database round-trip. However, they have following constraints:
- The properties projected must be stored in the index (
@Field(store=Store.YES)
), which increases the index size. - The properties projected must use a
FieldBridge
implementingorg.infinispan.query.bridge.TwoWayFieldBridge
ororg.infinispan.query.bridge.TwoWayStringBridge
, the latter being the simpler version.Note
All Lucene-based Query API built-in types are two-way. - Only the simple properties of the indexed entity or its embedded associations can be projected. Therefore a whole embedded entity cannot be projected.
- Projection does not work on collections or maps which are indexed via
@IndexedEmbedded
Lucene provides metadata information about query results. Use projection constants to retrieve the metadata.
Example 5.6. Using Projection to Retrieve Metadata
Fields can be mixed with the following projection constants:
FullTextQuery.THIS
returns the initialized and managed entity as a non-projected query does.FullTextQuery.DOCUMENT
returns the Lucene Document related to the projected object.FullTextQuery.OBJECT_CLASS
returns the indexed entity's class.FullTextQuery.SCORE
returns the document score in the query. Use scores to compare one result against another for a given query. However, scores are not relevant to compare the results of two different queries.FullTextQuery.ID
is the ID property value of the projected object.FullTextQuery.DOCUMENT_ID
is the Lucene document ID. The Lucene document ID changes between two IndexReader openings.FullTextQuery.EXPLANATION
returns the Lucene Explanation object for the matching object/document in the query. This is not suitable for retrieving large amounts of data. RunningFullTextQuery.EXPLANATION
is as expensive as running a Lucene query for each matching element. As a result, projection is recommended.
5.1.3.5. Limiting the Time of a Query 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
Limit the time a query takes in Infinispan Query as follows:
- Raise an exception when arriving at the limit.
- Limit to the number of results retrieved when the time limit is raised.
5.1.3.6. Raise an Exception on Time Limit 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
If a query uses more than the defined amount of time, a custom exception might be defined to be thrown.
To define the limit when using the CacheQuery API, use the following approach:
Example 5.7. Defining a Timeout in Query Execution
The
getResultSize()
, iterate()
and scroll()
honor the timeout until the end of the method call. As a result, Iterable
or the ScrollableResults
ignore the timeout. Additionally, explain()
does not honor this timeout period. This method is used for debugging and to check the reasons for slow performance of a query.
Important
The example code does not guarantee that the query stops at the specified results amount.