Magnolia 5.7 reached extended end of life on May 31, 2022. Support for this branch is limited, see End-of-life policy. Please note that to cover the extra maintenance effort, this EEoL period is a paid extension in the life of the branch. Customers who opt for the extended maintenance will need a new license key to run future versions of Magnolia 5.7. If you have any questions or to subscribe to the extended maintenance, please get in touch with your local contact at Magnolia.

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

queryString

required

The search term such as "beach".

startPath

required

Absolute path in the website workspace such as "/" or "/travel".

limit

optional

The maximal number of items to be returned. Use together with offset.

offset

optional

The offset ("starting point") of the complete result set. Use together with limit.

Returns

Collection<ContentMap>

Collection of page nodes as ContentMap items.

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

workspace

required

The name of the workspace to search in.

queryString

required

The search term such as "travel".

startPath

required

Absolute path in the specified workspace such as "/" or "/home/about/".

itemType

required

The node type.

limit

optional

The maximal number of items to be returned. Use together with offset .

offset

optional

The offset ("starting point") of the complete result set. Use together with limit .

Returns

Collection<ContentMap>

Collection of nodes as ContentMap items.

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]
[#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]