HippoSolrClient and HippoQuery - BloomReach Experience - Open Source CMS

HippoSolrClient and HippoQuery

Through the HippoSolrClient you can get hold of a bootstrapped HippoQuery. The HippoQuery implementation is a small wrapper class around the org.apache.solr.client.solrj.SolrQuery, with a couple of facility methods, namely setLimit, setOffset, setIncludedClasses, setExcludedClasses, setScopes and setExcludedScopes. Note that all these setters delegate to the SolrQuery. Developers could just as well directly interact with the backing SolrQuery to, for example, mimic setExcludedClasses, however, these queries assume knowledge about how the beans are being indexed. Hence, this has been abstracted away to these facility method. Apart from these facility methods, developers will just interact with the backing SolrQuery. Thus, for example, adding a field to sort on is directly done through the SolrQuery see for example Sorting search results.

Bootstrapping a HippoQuery (SolrQuery)

HippoSolrClient query bootstrapping:

/**
 * <p>
 * Creates a new {@link HippoQuery} with an initial bootstrappped
 * {@link org.apache.solr.client.solrj.SolrQuery} which
 * has its {@link org.apache.solr.client.solrj.SolrQuery#setQuery(String)}
 * called with <code>query</code>.
 * </p>
 * <p>For <code>query</code> thus the general Solr syntax can be used,
 *    see http://wiki.apache.org/solr/SolrQuerySyntax. For example
 * <pre>
 *     <code>
 *        query = title:hippo +createdate:[1976-03-06T23:59:59.999Z TO *]
 *     </code>
 *
 * </pre>
 * </p>
 * @param query the <code>query</code> to bootstrap the
 * {@link org.apache.solr.client.solrj.SolrQuery} with.
 * @return a {@link HippoQuery}
 */
HippoSolrClient solrClient =
   HstServices.getComponentManager().getComponent(
                HippoSolrClient.class.getName(), "org.hippoecm.hst.solr");
// now, below create a bootstrapped HippoQuery, which, under the hood,
// also bootstraps the SolrQuery
HippoQuery hippoQuery = solrClient.createQuery(query);

Note, that query above in createQuery(query) can be of SolrQuerySyntax. If you do not want this, or want to parse the query first to strip illegal chars, you can use the HippoQueryParser :

Parsing the query first:

HippoSolrClient solrClient =
      HstServices.getComponentManager().getComponent(
                 HippoSolrClient.class.getName(), "org.hippoecm.hst.solr");
HippoQueryParser parser = solrClient.getQueryParser();
HippoQuery hippoQuery = solrClient.createQuery(parser.escape(query));

Once you have a bootstrapped HippoQuery, you can set some extra constraints (which will be delegated to the backing SolrQuery) and access the underlying SolrQuery

Setting limit and offset

// default limit is HippoQuery.DEFAULT_LIMIT = 10
HippoQuery hippoQuery = ....
hippoQuery.setLimit(100);
hippoQuery.setOffset(200);

Note that the same as above could be achieved directly through the SolrQuery :

HippoQuery hippoQuery  = solrClient.createQuery(query);
hippoQuery.getSolrQuery().setRows(100);
hippoQuery.getSolrQuery().setStart(200);

Setting scopes to search in and excluding scopes

By default, a HippoQuery (SolrQuery) searches in all scopes if you do not specify a specific one: Thus, in all indexed content, regardless to which (sub)site it belongs, and whether it are externally indexed sources or not. Once you specify scope(s) through setScopes, only below the specific scope will be search. When specify setExcludedScopes, those scopes will be excluded for search results.

This is different than the HstQuery (the other style query through the repository) : That one by default only searches in the scope of the (sub)site belonging to the request

What are the scopes of a IdentifiableContentBean? The scopes of an IdentifiableContentBean are all the partial paths that its getIdentifier() method returns. Thus, suppose an IdentifiableContentBean that returns for getIdentifier() the following value :

/content/documents/mysite/news/2011/06/12/mynews

Then, it's scopes are:

/content
/content/documents
/content/documents/mysite
/content/documents/mysite/news
/content/documents/mysite/news/2011
/content/documents/mysite/news/2011/06
/content/documents/mysite/news/2011/06/12
/content/documents/mysite/news/2011/06/12/mynews

For an IdentifiableContentBean that returns for getIdentifier()

http://www.example.org/news/2012/item

The scopes will be:

http:
http:/
http://
http://www.example.org
http://www.example.org/news
http://www.example.org/news/2012
http://www.example.org/news/2012/item

Now, setting the scopes to search in and to exclude from can be done for example by:

Only search below mysite and exclude agenda:

HippoQuery hippoQuery  = solrClient.createQuery(query);
hippoQuery.setScopes("/content/documents/mysite");
hippoQuery.setExcludedScopes("/content/documents/mysite/agenda");

Note that the setScopes and setExcludedScopes take a varargs String argument, so, for example if you'd only want to search in external (http and https) sources but not from onehippo.com and onehippo.org, you'd get:

Only search external http:

HippoQuery hippoQuery  = solrClient.createQuery(query);
hippoQuery.setScopes("http:", "https:");
hippoQuery.setExcludedScopes("http://www.onehippo.org",
                             "https://www.onehippo.com");

Specifying the type of classes the search result hits should be of

By default, a HippoQuery (SolrQuery) searches for all types of IdentifiableContentBean's, unless explicitly specified. When an IdentifiableContentBean gets indexed, the entire class hierarchy including interfaces gets indexed. Thus for example if you index a org.example.com.beans.NewsDocument bean, which extends from org.hippoecm.hst.content.beans.standard.HippoDocument, then, the indexed class hierarchy will be (note that HippoDocument extends some interfaces and base classes which in turn extend again interfaces)

org.example.com.beans.NewsDocument
org.hippoecm.hst.content.beans.standard.HippoDocument
org.hippoecm.hst.content.beans.standard.HippoDocumentBean
org.hippoecm.hst.content.beans.standard.HippoItem
org.hippoecm.hst.content.beans.standard.HippoBean
org.hippoecm.hst.content.beans.standard.IdentifiableContentBean
org.hippoecm.hst.content.beans.standard.ContentBean
org.hippoecm.hst.content.beans.NodeAware
org.hippoecm.hst.content.beans.manager.ObjectConverterAware
java.lang.Comparable

Note that java.lang.Object won't be part of the class hierarchy

Because the HST DocumentObjectBinder indexes the class hierarchy, you can specify to search for certain object types only. Thus, if you only want to search for NewsDocument's and AgendaDocument's or subtypes (boolean flag first argument) of these, you use:

HippoQuery hippoQuery  = solrClient.createQuery(query);
hippoQuery.setIncludedClasses(true, NewsDocument.class,
                                    AgendaDocument.class);

Excluding classes to search from, is similar. Suppose you do not want to search for any documents that are part of the JCR repository. These documents have normally beans that implement the NodeAware interface. Hence, the query below would exclude JCR documents:

HippoQuery hippoQuery  = solrClient.createQuery(query);
hippoQuery.setExcludedClasses(true, NodeAware.class);

Node that both setIncludedClasses and setIncludedClasses both accept first a boolean indicating including subtypes or not and then varargs Class<?> arguments.

Did you find this page helpful?
How could this documentation serve you better?
On this page
    Did you find this page helpful?
    How could this documentation serve you better?