Exclude specific attachment types in search results
In some cases it may be desirable to hide specific attachments from appearing in Scroll Viewport search results. This can be achieved by following the steps in this documentation.
Prerequisites
In order to prevent attachments from appearing in Scroll Viewport search results, you must first ensure that you have:
followed and implemented the Search Template Recipe
made attachments available in your search results
Implementation
Firstly, you need to replace the code of the search.vm with the following code:
#foreach($result in $searchResults.items)
#if($result.absoluteLink.contains("/files/"))
#if($result.absoluteLink.endsWith(".jpg"))
<section class="search-result">
<header>
<h2>
<a href="$result.link">$result.displayTitle</a>
</h2>
</header>
<div class="search-result-content">
<p class="search-result-link">$result.absoluteLink</p>
<p class="search-result-desc">$result.getDescription(280)</p>
</div>
<hr>
</section>
#end
#else
<section class="search-result">
<header>
<h2>
<a href="$result.link">$result.displayTitle</a>
</h2>
</header>
<div class="search-result-content">
<p class="search-result-link">$result.absoluteLink</p>
<p class="search-result-desc">$result.getDescription(280)</p>
</div>
<hr>
</section>
#end
#end
In the first #if-statement, the current search result is checked to see whether it is an attachment.
The second #if-statement then checks if the attachment ends with ".jpg" and gets displayed – all other attachments are ignored.
If you want to show all attachments except for ".jpg"-files, you just need to negate the #if-statement like this:
#foreach($result in $searchResults.items)
...
#if(!$result.absoluteLink.endsWith(".jpg"))
...
#end
Please note that the excluded attachments are still counted to the search results, which may affects the pager of Scroll Viewport.