solrfn
solrfn
templating
functions are used to construct search queries from FreeMarker templates
and trigger Solr server searches that will return results in form of a JSON string. Such results can be easily processed in FreeMarker templates.
These templating functions are provided by the magnolia-solr-templating
submodule of the
Solr module.
|
Basic templating
All of the solrfn
query methods are provided by the info.magnolia.search.solr.templating.SolrTemplatingFunction Java class and return a
info.magnolia.search.solr.templating.SolrQueryBuilder object.
Calling one of its methods returns the same object and so you can chain the methods using the same object.
Create a query
Create a Solr query.
Method signature
SolrQueryBuilder query(String searchTerm)
SolrQueryBuilder query(String searchTerm, boolean escape)
Arguments
Argument | Description |
---|---|
|
required A term to search for. Leaving the argument unspecified will search for everything, which is the same as using the wildcard ( |
|
optional Escapes the special characters in the search term if set to |
Trigger a search
To trigger a search after initializing the SolrQueryBuilder
by one of the query methods from the SolrTemplatingFunction
, call the search
method.
Usage
solrfn.query(searchTerm, escape).search()
[#assign results = solrfn.query("searchTerm").search()] ${results}
Creates a query for "searchTerm", triggers the search and prints the whole JSON string.
To convert a string to a JSON expression, use the FreeMarker
eval
function (eval_json
since freemarker 2.3.31), as in for example:
[#assign results = solrfn.query("searchTerm").search()?eval] Documents found: ${results.response.numFound} [#list results.response.docs as doc] <li>${doc.url!} [/#list]
Use a different Solr client
The Solr Search Provider module allows you to define more than one server (see
Configuring Solr clients).
To specify which Solr client should be leveraged, use the clientName
method in the SolrQueryBuilder
.
Filter results by type
Indexers and crawlers allow you to specify the type of content to be indexed, see the type
property in crawler and indexer configuration. You can use the equivalent solrfn type
method as a search filter.
Advanced templating
Common parameters
SolrQueryBuilder
provides several most commonly used Solr query parameters.
Method | Description | Equivalent Solr parameter Link to Solr documentation |
---|---|---|
|
Restrict query results. |
|
|
Sets an offset for a query result set and instructs Solr to display the results from this offset. |
|
|
Defines the maximum number of documents from a complete result set Solr should return. Default value is |
|
|
Limits the information included in a query response to a specified list of fields. |
|
|
Arranges the search results in either ascending (asc) or descending (desc) order. |
|
|
Specifies a default field, overriding the default field in the Schema. |
|
DisMax and eDisMax query parsers
By default, the standard query parser is used. To use the DisMax or the eDisMax filter instead, use the respective method.
Method | Description | Link to Solr documentation |
---|---|---|
|
Use the DisMax parser. |
|
|
Use the eDisMax parser. |
Parameters
Method | Description | Equivalent Solr parameter Link to Solr documentation |
---|---|---|
|
Introduces a list of fields, each of which is assigned a boost factor to increase or decrease that particular field’s importance in the query. |
|
|
By default, all words or phrases specified in the query are treated as "optional" clauses. The mm parameter makes it possible to say that a certain minimum number of those clauses must match. |
|
|
To "boost" the score of documents in cases where all of the terms in the query appear in close proximity. |
|
|
Specifies an additional, optional, query clause that will be added to the user’s main query to influence the score. |
|
Specifying a requestHandler
A request handler
processes incoming Solr requests. Multiple requestHandlers can
be defined in the solrConfig.xml
.
Higlighting
Highlighting allows you to include with the query response fragments of documents that match the user’s query.
Fragments (also referred to as snippets or passages) are a portion of a document field that contains matches from the query. |
The fragments are included in the highlighting section, a special section of the response, and the client uses the formatting clues also included to determine how to present the fragments to users.
See also Common Highlighting Parameters in Apache Solr documentation.
Method | Description | Equivalent Solr parameter |
---|---|---|
|
Enables highlighting. |
|
|
Enables highlighting and specifies a list of fields to highlight. |
|
|
Specifies the maximum number of highlighted snippets to generate per field. |
|
|
Specifies a tag that will be used before and after a highlighted term. |
|
|
Specifies the approximate fragment size to consider for highlighting. The default size is 100 characters. |
|
Faceting
Faceting is the arrangement of search results into categories based on indexed terms. It makes it easy for users to explore search results, narrowing in on exactly the results they are looking for.
Searchers are presented with the indexed terms, along with numerical counts of how many matching documents were found for each term.
See also Field-Value Faceting Parameters in Apache Solr documentation.
Method | Description | Equivalent Solr parameter |
---|---|---|
|
Enables faceting. |
|
|
Identifies a field that should be treated as a facet. |
|
|
Limits the terms on which to facet to those starting with the given string prefix. |
|
|
For a specific field, limits the terms on which to facet to those starting with the given string prefix. |
|
|
Limits the terms on which to facet to those containing the given substring. |
|
|
For a specific field, limits the terms on which to facet to those containing the given substring. |
|
|
Limits the terms on which to facet to those containing the given substring and allows case to be ignored when matching the given substring against candidate facet terms. |
|
|
For specific field, limits the terms on which to facet to those containing the given substring and allows case to be ignored when matching the given substring against candidate facet terms. |
|
|
Specifies the maximum number of facets for a field that are returned. The default value is 100. |
|
|
For specific field, specifies the maximum number of facets for a field that are returned. |
|
|
If set to true, returns the constraints sorted in their index order. If set to false, returns the constraints sorted by count. The default is count if the facet limit is greater than 0, otherwise, the default is index. |
|
|
Same as the |
|
|
Specifies the minimum counts required for a facet field to be included in the response. The default value is 0. |
|
Range faceting
You can use range faceting on any date field or any numeric field that supports range queries.
See also Range Facet Parameters in Apache Solr documentation.
Method | Description | Equivalent Solr parameter |
---|---|---|
|
Defines the field for which Solr should create range facets, with parameters |
|
Other parameters
Solr provides many more options. If a function is missing in solrfn
, then use the methods below.
Method | Description | Equivalent Solr parameter |
---|---|---|
|
Any parameter supported by Solr. |
|
|
Any parameter supported by Solr on a per field basis. |
|