1 HstQuery bootstrapping
On this page
Getting hold of a HstQueryManager
The HstQueryManager can be retrieved from the current request context.
-
In a HstComponent
HstComponent extending BaseHstComponent:
-
In a JAXRS Service Resource extending from org.hippoecm.hst.jaxrs.services.AbstractResource
ProductPlainResource extending AbstractResource:
In cases where you don't have a HstRequestContext (for example for during some background process or for external application integration that does not involve a http request), you can get hold of the HstQueryManager to create an HstQuery directly through the Spring Component Manager:
Creating a new HstQuery
Once you have the HstQueryManager, there are multiple ways to create a new HstQuery. Below, we explain the usual way to do this. Other ways are very similar, also see the Javadocs from org.hippoecm.hst.content.beans.query.HstQueryManager.
HstQueryManager#createQuery:
In the code above the createQuery method takes three arguments:
-
HippoBean scope : The scope to search below in the repository. A HippoBean is backed by a JCR Node, which represents a location in the content tree of the repository. The scope identifies the sub-tree in the repository for searching documents. Note that instead of argument HippoBean, there are also methods in the HstQueryManager that directly take the JCR Node as scope, for example HstQuery createQuery(Node node, Class<? extends HippoBean> filterBean, boolean includeSubTypes);
-
Class<? extends HippoBean> filterBean : This is a filter for the possible hits. Only hits of type filterBean will be part of the result. Depending on the last argument, includeSubTypes, also subtypes are inluded in the result.
-
includeSubTypes : when true, also subtypes of the filterBean can be part of the result.
Here's an example code snippet for creating a new HstQuery in an HstComponent extending from BaseHstComponent:
Setting a limit and offset
In general it is always best to set a limit. If you do not specify a limit, the HST by default will set a limit of 1000. The best way is to set the limit equal to the number of items you want to show, for example pageSize. If you combine this with offset, you can easily always fetch the correct items.
For example, if pageSize = 10, and you want to show the 3rd page ( currentPage = 3), use:
Now, there is one more catch. If you have set a limit of, say, 10, then, HstQueryResult#getSize() will return at most 10. So, what if you need to know how many pages there are? For this, you can use getTotalSize(). This method returns the total number of hits.
Always set a limit, preferably to just the number you need to show
If you don't set a limit, HST sets a limit of 1000 for safety. This is already large, and might result in slower searches. For optimal performance, set a limit (and optionally an offset).
Sorted results
The HstQuery can "order by" / "sort by" multiple properties, ascending or descending. The sorting is done according to the order in which the properties to sort on are added. Sorting can only be done on properties (meta-data) which are directly stored on the document. Properties which are part of a Compound in a document can not be used for sorting.
For example:
Sorting / Ordering:
Sorting can only be done on properties directly on the documents
Also see Sorting search results in a query can only be done on direct properties of Documents
Including or excluding extra scopes
An HstQuery can search in multiple scopes, and also, some scopes can be excluded. This can be done by addScope and excludeScope :
Including extra or excluding scopes:
Assuming that I want to search through the entire content of the current site and through the assets I can use something like: