Last updated by
2 years ago
Page: Searchable Plugin - SearchableController and view, Version:10
Searchable Plugin - Controller and view
Searchable Plugin comes with a controllerSearchableController and view searchable/index.gsp . Try these with your application (like this) - they can help to test queries and you can probably copy some of the code.You could use theSearchableControlleras it comes, but you will probably want a different URL and HTML results page.To change the URL add entries to yourgrails-app/conf/UrlMappings.groovy.To change the view, you can simply keep the plugin's controller and copymyapp/plugins/searchable-x.x/grails-app/views/searchable/index.gsptomyapp/grails-app/views/searchable/index.gspwhere it will override the plugin's version.Or of course you can create your own dedicated search controller and view.
Under the covers
Here's the implementation of the search action fromSearchableController :import org.compass.core.engine.SearchEngineQueryParseException// ...class SearchableController { def searchableService /** * Index page with search form and results */ def index = { if (!params.q?.trim()) { return [:] } try { return [searchResult: searchableService.search(params.q, params)] } catch (SearchEngineQueryParseException ex) { return [parseException: true] } } // … }
params.q is the search query string and params is also given to the search method as the second argument. This is a Map of options and are things like page size, start result number, etc.Any String arguments are parsed, so you can use request parameters directly, even when they have string values. For example, params may be [escape: "true", offset: "20", q: "toast"] , but you won't get a ClassCastException.Pagination of search results in the view
Search results can be paginated using Grails' standard<g:paginate /> tag.Here it is in action in the Searchable Plugin's own search results page, grails-app/views/searchable/index.gsp . The searchResult is an object returned by either SearchableService#search or DomainClass#search :<g:if test="${haveResults}"> <!-- or you could use test="${searchResult?.results}" --> Page: <g:set var="totalPages" value="${Math.ceil(searchResult.total / searchResult.max)}" /> <g:if test="${totalPages == 1}"> <span class="currentStep">1</span> </g:if> <g:else> <g:paginate controller="searchable" action="index" params="[q: params.q]" total="${searchResult.total}" prev="< previous" next="next >"/> </g:else> </g:if>
.paging a.step {
padding: 0 .3em;
}.paging span.currentStep {
font-weight: bold;
}Suggested queries
You can easily highlight the difference between the original and a suggested query with the following in a GSP:<%@ page import="org.codehaus.groovy.grails.plugins.searchable.util.StringQueryUtils" %><p>Did you mean <g:link controller="searchable" action="index" params="[q: searchResult.suggestedQuery]"> ${StringQueryUtils.highlightTermDiffs(params.q.trim(), searchResult.suggestedQuery)} </g:link>? </p>