searchfn
searchfn templating functions allow you to search pages - or more
generally content - returning collections of
ContentMap
objects.
The results are sorted by jcr:score(), descending. A text excerpt may
be available in the returned ContentMap depending on how Jackrabbit
search is configured. Magnolia checks the current user’s permissions
before adding a result to the collection.
Searching pages
The searchPages function searches pages for a given query string and
start path. This function searches only nodes of type mgnl:page in the
website JCR workspace, not all types of content. The returned items of
the collection are of type ContentMap. Use this function with the
mandatory arguments if you want to render the results with pagination.
Method signature
Collection<``ContentMap> searchPages(String queryString, String startPath)
Collection<``ContentMap> searchPages(String queryString, String startPath, long limit, long offset)
Arguments
| Argument | Description |
|---|---|
|
required The search term such as |
|
required Absolute path in the website workspace such as |
|
optional The maximal number of items to be returned. Use together with |
|
optional The offset ( |
Usage
searchfn.searchPages('foo', '/somepath') or
searchfn.searchPages('foo', '/somepath', 10, 21)
[#assign searchResults = searchfn.searchPages('hello', '/home') /]
[#if searchResults?has_content]
[#list searchResults as item]
<a href="${cmsfn.link(item)}">
<h4>${item.title!}</h4>
<p>${item.excerpt!}</p>
</a>
[/#list]
[/#if]
Searching all content
The searchContent function searches all content for a given query
string, start path and node type in any workspace. If you don’t specify
a node type the function searches for the most basic type nt:base. The
returned items of the collection are of type ContentMap. Use this
function with the optional arguments (limit, offset) if you want to
render the results with pagination.
Method signature
Collection<``ContentMap> searchContent(String workspace, String queryString, String startPath, String returnItemType)
Collection<``ContentMap> searchContent(String workspace, String queryString, String startPath, String returnItemType, long limit, long offset)
Arguments
| Argument | Description |
|---|---|
|
required The name of the workspace to search in. |
|
required The search term such as |
|
required Absolute path in the specified workspace such as |
|
required The node type. |
|
optional The maximal number of items to be returned. Use together with |
|
optional The offset ( |
Usage
searchfn.searchContent("website", "Ansel Adams", "/home", "mgnl:component")
searchfn.searchContent("dam", "jpg", "/photos", "mgnl:asset", 10, 0)
[#assign searchResults = searchfn.searchContent("website", "Ansel Adams", "/home", "mgnl:component") /]
[#if searchResults?has_content]
[#list searchResults as item]
[#if item.quotation??]
<div class="found-component">
${item.quotation}
</div>
[/#if]
[/#list]
[/#if]
[source]
----
----
[#assign searchResults = searchfn.searchContent("dam", "Christoph Meier", "/photos", "") /]
[#if searchResults?has_content]
[#list searchResults as item]
[#if item.publisher??]
${item.publisher} -
[#assign myAsset = damfn.getAsset("jcr", cmsfn.asJCRNode(item).getPath())!]
[#assign url=damfn.getRendition(myAsset, "small-square").getLink()]
<img src="${url}"/>
[/#if]
[/#list]
[/#if]