Direct Access to the Solr Query
This document provides information on the direct access to the solr queries.
Overview
The post-processors enhance the default converter classes with additional logic that depends on the particular configuration and implementation.
Search Query Post-Processors
The default implementation of the de.hybris.platform.solrfacetsearch.search.impl.DefaultFacetSearchService service uses a dedicated query converter.
public interface SolrQueryConverter {
/**
* Converts {@link SearchQuery} instance into valid {@link SolrQuery}.
*/
SolrQuery convertSolrQuery(SearchQuery searchQuery) throws FacetSearchException; }
}
The main purpose of the API is to provide translation of the search query to the Solr server query. By default, the convertSolrQuery method is called in the DefaultFacetSearchService for the de.hybris.platform.solrfacetsearch.search.impl.DefaultFacetSearchService.translateSearchQuery(SearchQuery).
The resulting query content comes from hybris to the Solr integration which is implemented in the solrfacetsearch extension. However, it is possible to use some other features of the Solr server, that are not yet supported directly by the solrfacetsearch extension.
This is the reason why the default implementation of the de.hybris.platform.solrfacetsearch.search.SolrQueryConverter.convertQuery(SearchQuery) method has been given given configurable query post-processing hooks that support customization of the default conversion process.
There is a configurable list of the query post-processors available. All the changes coming from these post-processors are applied in the same order as they appear in the configuration file. This is done at the end of the translation process.
Query converter is declared as a spring bean and used by the DefaultFacetSearchService service. Therefore all the post-processor hooks have to be declared at the converter level.
Custom Query Converter and Configuration
To declare a hook you need to implement the de.hybris.platform.solrfacetsearch.search.SolrQueryPostProcessor interface. The code sample below provides an example of such implementation.
public class SimpleSolrQueryPostProcessor implements SolrQueryPostProcessor
{
@Override
SolrQuery process(final SolrQuery query, final SearchQuery solrSearchQuery)
{
query.setSortField("id", ORDER.asc);
query.setStart(Integer.valueOf(0));
query.setRows(Integer.valueOf(10));
}
}
Spring Configuration
The defaultSolrQueryConverter holds the list referencing all defined query post-processors. The post-processors can be defined in a form of utility list instance. The default translation defined in the converter is performed at the beginning of the process. The post-processors are applied to the query one after another in the end of the conversion process.
<alias name="defaultSolrQueryConverter" alias="solrQueryConverter"/>
<bean id="defaultSolrQueryConverter" class="de.hybris.platform.solrfacetsearch.search.impl.DefaultSolrQueryConverter">
<property name="queryPostProcessors" ref="solrQueryPostProcessors"/>
<property name="solrFieldNameProvider" ref="solrFieldNameProvider"/>
<property name="defaultLimit" value="${facet.limit.default}"></property>
<property name="forbiddenChar" value="${solr.indexedproperty.forbidden.char}"></property>
</bean>
<alias name="defaultSolrQueryPostProcessors" alias="solrQueryPostProcessors"/>
<util:list id="defaultSolrQueryPostProcessors">
<ref bean="simpleSolrQueryPostProcessor "/>
</util:list>
<bean id="simpleSolrQueryPostProcessor" class="my.package.SimpleSolrQueryPostProcessor " />
Search Result Post-Processors
Similarly to the query post-processors that operate on the Solr query object, there are also result post-processors that can modify the search result object returned from the Solr index search.
The search result post-processors work on the de.hybris.platform.solrfacetsearch.search.impl.SolrSearchResult level. If you want to register a post-processor you need to provide a proper spring configuration and create your own implementation.
Spring Configuration
All post-processors are registered in the defaultFacetSearchService by default.
<alias name="defaultFacetSearchService" alias="facetSearchService"/>
<alias name="defaultFacetSearchService" alias="solrFacetSearchService"/>
<bean id="defaultFacetSearchService" class="de.hybris.platform.solrfacetsearch.search.impl.DefaultFacetSearchService">
<property name="userService" ref="userService" />
<property name="i18nService" ref="i18nService" />
<property name="solrService" ref="solrService"/>
<property name="catalogService" ref="catalogService"/>
<property name="fieldNameProvider" ref="solrFieldNameProvider"/>
<property name="solrQueryConverter" ref="solrQueryConverter"/>
<property name="resultPostProcessors" ref="solrSearchResultPostProcessors"/>
</bean>
<alias name="defaultSolrSearchResultPostProcessors" alias="solrSearchResultPostProcessors"/>
<util:list id="defaultSolrSearchResultPostProcessors">
<ref bean="idleSolrResultPostProcessor"/>
</util:list>
<bean id="idleSolrResultPostProcessor" class="my.package.IdleSolrResultPostProcessor" />
The search result post-processors must implement the de.hybris.platform.solrfacetsearch.search.SolrResultPostProcessor abstract class. The code sample contains the example.
public class IdleSolrResultPostProcessor implements SolrResultPostProcessor {
@Override
public SearchResult process(SearchResult solrSearchResult)
{
//do sth with thte search result : hide some result DTO's, sort facet values, manipulate with breadcrumbs....
}
}
Overview of the Solr Query and Search Result Post-Processing
Further Customization of the Query Converter
For flexibility reasons it is possible to set several options in the implementation of the Solr query converter. The table provides explanation for these options.
|
Configurable Attribute Name |
Description |
Possible Values |
Default Value |
|---|---|---|---|
|
fieldOperator |
Logical operator for the facet fields in the Solr query. The converter uses it when building the Solr query. |
AND / OR |
AND |
|
facetSort |
Sort option of the facet values in facets. |
COUNT/INDEX |
INDEX |
|
defaultFacetLimit |
Default limit of the facet values for a single facet. |
int |
project property = facet.limit.default |
|
forbiddenChar |
Char being the separator in the Solr fields names (for example, name_en_string) |
char |
project property = solr.indexedproperty.forbidden.char |
The default setup of the Solr query converter.
<alias name="defaultSolrQueryConverter" alias="solrQueryConverter"/>
<bean id="defaultSolrQueryConverter" class="de.hybris.platform.solrfacetsearch.search.impl.DefaultSolrQueryConverter">
<property name="queryPostProcessors" ref="solrQueryPostProcessors"/>
<property name="solrFieldNameProvider" ref="solrFieldNameProvider"/>
<property name="defaultLimit" value="${facet.limit.default}"></property>
<property name="forbiddenChar" value="${solr.indexedproperty.forbidden.char}"></property>
</bean>
The following code sample provides an example of the customization in spring.
<alias name="customSolrQueryConverter" alias="solrQueryConverter"/> <bean id="customSolrQueryConverter" parent="defaultSolrQueryConverter"> <property name="fieldOperator" value="OR"/> <property name="facetSort" value="COUNT"/> </bean>