This followup will explain how the indexed data can be used in a search application in AEM. Although you can use every Elasticsearch client you want (e.g. the Java REST Client2), an easy way is the usage of one of the two Searches implemented in this package.
Both Search classes (
PageSearch.java4) require an instance of the
ElasticSearchService5. Usually you would inject it using the
@Reference Annotation6. Furthermore you have to choose, which index to use.
Generic searches on Elasticsearch Documents are implemented in the Search3 class.
The most important part of a request is the query7. Currently basic querying is supported8, the syntax for most queries is similar. Usually the base for a query is a Bool Query9 that supports the following types:
|must||query must appear in the matching documents|
|should||query is optional|
|must_not||query must not appear in the matching documents|
|filter||query must appear in the matching documents but does not contribute to the score, see Filter|
Filter10 are a special type of query as they also influence the results but do not change the score. The syntax is similar to a query:
Limit the results
The following example constructs a search, which will include the first 10 results which have at least a score of 2.0.
Another option is
from which can be used for paging.
If you want to sort11 the results by a field, you can use the
You can add one or more fields to the highlight12 section. This will allow you, to show excerpts from the results.
By default, all highlights are wrapped in
<em> tags you can replace:
As most searches require a similar base set, the PageSearch extends a Search with some AEM Page specific methods.