Magnolia 5.4 reached end of life on November 15, 2018. This branch is no longer supported, see End-of-life policy.
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.
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.
Collection<ContentMap> searchPages(String queryString, String startPath)
Collection<ContentMap> searchPages(String queryString, String startPath, long limit, long offset)
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 | optional The offset ("starting point") of the complete result set. Use together with |
Collection<ContentMap>
Collection of page nodes as ContentMap
items.
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]
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.
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)
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 | optional The offset ("starting point") of the complete result set. Use together with |
Collection<ContentMap>
Collection of nodes as ContentMap
items.
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]