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

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

Argument Description

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]
[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]
Related topics
Feedback

DX Core

×

Location

This widget lets you know where you are on the docs site.

You are currently perusing through the DX Core docs.

Main doc sections

DX Core Headless PaaS Legacy Cloud Incubator modules