Skip to content

Instantly share code, notes, and snippets.

@topolik
Created October 16, 2012 09:21
Show Gist options
  • Save topolik/3898265 to your computer and use it in GitHub Desktop.
Save topolik/3898265 to your computer and use it in GitHub Desktop.
Redirecting to the only one search result in wiki/search portlet
<%
// ... OLD CONTENT
searchContext.setEnd(searchContainer.getEnd());
searchContext.setKeywords(keywords);
searchContext.setNodeIds(nodeIds);
searchContext.setStart(searchContainer.getStart());
Hits results = indexer.search(searchContext);
int total = results.getLength();
// !!!!!!!!!!!!!!!!! MY NEW CONTENT !!!!!!!!!!!
if(total == 1){
PortletURL myURL = renderResponse.createRenderURL();
myURL.setParameter("struts_action", "/wiki/view");
myURL.setParameter("nodeId", results.doc(0).get("nodeId"));
%>
<script type="text/javascript">window.location.href="<%= HtmlUtil.escapeJS(myURL.toString()) %>";</script>
<%
}
// OLD CONTENT CONTINUES ...
searchContainer.setTotal(total);
List resultRows = searchContainer.getResultRows();
for (int i = 0; i < results.getDocs().length; i++) {
Document doc = results.doc(i);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment