search.jsp revision 144
72d0e1cb2facaa4b8ba2f15e311d6bb9491badb7Stéphane GraberCDDL HEADER START
72d0e1cb2facaa4b8ba2f15e311d6bb9491badb7Stéphane GraberThe contents of this file are subject to the terms of the
72d0e1cb2facaa4b8ba2f15e311d6bb9491badb7Stéphane GraberCommon Development and Distribution License (the "License").
72d0e1cb2facaa4b8ba2f15e311d6bb9491badb7Stéphane GraberYou may not use this file except in compliance with the License.
72d0e1cb2facaa4b8ba2f15e311d6bb9491badb7Stéphane GraberSee LICENSE.txt included in this distribution for the specific
72d0e1cb2facaa4b8ba2f15e311d6bb9491badb7Stéphane Graberlanguage governing permissions and limitations under the License.
72d0e1cb2facaa4b8ba2f15e311d6bb9491badb7Stéphane GraberWhen distributing Covered Code, include this CDDL HEADER in each
72d0e1cb2facaa4b8ba2f15e311d6bb9491badb7Stéphane Graberfile and include the License file at LICENSE.txt.
72d0e1cb2facaa4b8ba2f15e311d6bb9491badb7Stéphane GraberIf applicable, add the following below this CDDL HEADER, with the
72d0e1cb2facaa4b8ba2f15e311d6bb9491badb7Stéphane Graberfields enclosed by brackets "[]" replaced with your own identifying
72d0e1cb2facaa4b8ba2f15e311d6bb9491badb7Stéphane Graberinformation: Portions Copyright [yyyy] [name of copyright owner]
72d0e1cb2facaa4b8ba2f15e311d6bb9491badb7Stéphane GraberCDDL HEADER END
72d0e1cb2facaa4b8ba2f15e311d6bb9491badb7Stéphane GraberCopyright 2005 Sun Microsystems, Inc. All rights reserved.
72d0e1cb2facaa4b8ba2f15e311d6bb9491badb7Stéphane GraberUse is subject to license terms.
72d0e1cb2facaa4b8ba2f15e311d6bb9491badb7Stéphane Graberident "%Z%%M% %I% %E% SMI"
72d0e1cb2facaa4b8ba2f15e311d6bb9491badb7Stéphane Graber--%><%@ page import = "javax.servlet.*,
72d0e1cb2facaa4b8ba2f15e311d6bb9491badb7Stéphane Graberorg.apache.lucene.search.spell.LuceneDictionary,
72d0e1cb2facaa4b8ba2f15e311d6bb9491badb7Stéphane Graber%><%@ page session="false" %><%@ page errorPage="error.jsp" %><%
72d0e1cb2facaa4b8ba2f15e311d6bb9491badb7Stéphane GraberDate starttime = new Date();
72d0e1cb2facaa4b8ba2f15e311d6bb9491badb7Stéphane GraberString defs = request.getParameter("defs");
72d0e1cb2facaa4b8ba2f15e311d6bb9491badb7Stéphane GraberString refs = request.getParameter("refs");
72d0e1cb2facaa4b8ba2f15e311d6bb9491badb7Stéphane GraberString hist = request.getParameter("hist");
72d0e1cb2facaa4b8ba2f15e311d6bb9491badb7Stéphane GraberString path = request.getParameter("path");
72d0e1cb2facaa4b8ba2f15e311d6bb9491badb7Stéphane Graber<%@ include file="projects.jspf" %>
72d0e1cb2facaa4b8ba2f15e311d6bb9491badb7Stéphane GraberString sort = null;
72d0e1cb2facaa4b8ba2f15e311d6bb9491badb7Stéphane Graberfinal String LASTMODTIME = "lastmodtime";
72d0e1cb2facaa4b8ba2f15e311d6bb9491badb7Stéphane Graberfinal String RELEVANCY = "relevancy";
72d0e1cb2facaa4b8ba2f15e311d6bb9491badb7Stéphane GraberCookie[] cookies = request.getCookies();
72d0e1cb2facaa4b8ba2f15e311d6bb9491badb7Stéphane Graberif (cookies != null) {
72d0e1cb2facaa4b8ba2f15e311d6bb9491badb7Stéphane Graber for (Cookie cookie : cookies) {
72d0e1cb2facaa4b8ba2f15e311d6bb9491badb7Stéphane Graber if (cookie.getName().equals("OpenGrok/sorting")) {
72d0e1cb2facaa4b8ba2f15e311d6bb9491badb7Stéphane Graber if (!LASTMODTIME.equals(sort) && !RELEVANCY.equals(sort)) {
72d0e1cb2facaa4b8ba2f15e311d6bb9491badb7Stéphane GraberString sortParam = request.getParameter("sort");
72d0e1cb2facaa4b8ba2f15e311d6bb9491badb7Stéphane Graberif (sortParam != null) {
72d0e1cb2facaa4b8ba2f15e311d6bb9491badb7Stéphane Graber if (LASTMODTIME.equals(sortParam)) {
72d0e1cb2facaa4b8ba2f15e311d6bb9491badb7Stéphane Graber sort = LASTMODTIME;
72d0e1cb2facaa4b8ba2f15e311d6bb9491badb7Stéphane Graber } else if (RELEVANCY.equals(sortParam)) {
72d0e1cb2facaa4b8ba2f15e311d6bb9491badb7Stéphane Graber sort = RELEVANCY;
72d0e1cb2facaa4b8ba2f15e311d6bb9491badb7Stéphane Graber if (sort != null) {
72d0e1cb2facaa4b8ba2f15e311d6bb9491badb7Stéphane Graber Cookie cookie = new Cookie("OpenGrok/sorting", sort);
72d0e1cb2facaa4b8ba2f15e311d6bb9491badb7Stéphane GraberHits hits = null;
72d0e1cb2facaa4b8ba2f15e311d6bb9491badb7Stéphane GraberString errorMsg = null;
if( q!= null && q.equals("")) q = null;
if( defs != null && defs.equals("")) defs = null;
if( refs != null && refs.equals("")) refs = null;
if( hist != null && hist.equals("")) hist = null;
if( path != null && path.equals("")) path = null;
if (project != null && project.equals("")) project = null;
Searcher searcher = null; //the searcher used to open/search the index
IndexReader ireader = null; //the reader used to open/search the index
int thispage = 0; //used for the for/next either max or
String DATA_ROOT = env.getDataRootPath();
if(DATA_ROOT.equals("")) {
throw new Exception("DATA_ROOT parameter is not configured in web.xml!");
if(!data_root.isDirectory()) {
throw new Exception("DATA_ROOT parameter in web.xml does not exist or is not a directory!");
ireader = IndexReader.open(DATA_ROOT + "/index");
//String date = request.getParameter("date");
sb.append(q);
sb.append(" defs:(");
sb.append(defs);
sb.append(")");
sb.append(" refs:(");
sb.append(refs);
sb.append(")");
sb.append(" path:(");
sb.append(path);
sb.append(")");
sb.append(" hist:(");
sb.append(hist);
sb.append(")");
sb.append(" (");
for (String s : project.split(" ")) {
sb.append(" OR ");
sb.append("project:(");
sb.append(s);
sb.append(")");
sb.append(")");
qstr = sb.toString();
query = qparser.parse(qstr); //parse the
hits = searcher.search(query, new Sort("date", true));
hits = searcher.search(query);
} catch (BooleanQuery.TooManyClauses e) {
if (hits != null && hits.length() == 1 && request.getServletPath().equals("/s") && (query != null && query instanceof TermQuery)) {
%><%@ include file="httpheader.jspf" %>
<div id="header"><%@ include file="pageheader.jspf" %></div>
<table border="0" width="100%"><tr><td><a href="<%=context%>" id="home">Home</a></td><td align="right"><%
StringBuffer url = request.getRequestURL();
url.append('?');
String querys = request.getQueryString();
int idx = querys.indexOf("sort=");
url.append(querys);
url.append('&');
url.append("sort=");
if (sort == null || RELEVANCY.equals(sort)) {
url.append(LASTMODTIME);
%><b>Sort by relevance</b> <a href="<%=url.toString()%>">Sort by last modified time</a><%
url.append(RELEVANCY);
%><a href="<%=url.toString()%>">Sort by relevance</a> <b>Sort by last modified time</b><%
<%@ include file="menu.jspf"%>
} else if (hits.length() == 0) {
File spellIndex = new File(env.getDataRootPath(), "spellIndex");
if (spellIndex.exists()) {
FSDirectory spellDirectory = FSDirectory.getDirectory(spellIndex);
toks = q.split("[\t ]+");
for(int j=0; j<toks.length; j++) {
String[] ret = checker.suggestSimilar(toks[j].toLowerCase(), 3);
for(int i = 0;i < ret.length; i++) {
toks = refs.split("[\t ]+");
for(int j=0; j<toks.length; j++) {
String[] ret = checker.suggestSimilar(toks[j].toLowerCase(), 3);
for(int i = 0;i < ret.length; i++) {
toks = defs.split("[\t ]+");
for(int j=0; j<toks.length; j++) {
String[] ret = checker.suggestSimilar(toks[j].toLowerCase(), 3);
for(int i = 0;i < ret.length; i++) {
%><p> Your search <b><%=query.toString()%></b> did not match any files.
if ( max < hits.length()) {
if((start + max) < hits.length()) {
thispage = hits.length() - start;
String url = (q == null ? "" : "&q=" + Util.URIEncode(q) ) +
(defs == null ? "" : "&defs=" + Util.URIEncode(defs)) +
(refs == null ? "" : "&refs=" + Util.URIEncode(refs)) +
(path == null ? "" : "&path=" + Util.URIEncode(path)) +
(hist == null ? "" : "&hist=" + Util.URIEncode(hist)) +
(sort == null ? "" : "&sort=" + Util.URIEncode(sort));
labelStart = sstart/max + 1;
for(int i=sstart; i<hits.length() && label <= labelEnd; i+= max) {
slider.append("<span class=\"sel\">" + label + "</span>");
} else if(label == labelEnd && i < hits.length()) {
arr = label < 10 ? " " + label : String.valueOf(label);
slider.append("<a class=\"more\" href=\"search?n=" + max + "&start=" + i + url + "\">"+
thispage = hits.length() - start; // set the max index to max or last
%> Searched <b><%=query.toString()%></b> (Results <b><%=start+1%> -
<%=thispage+start%></b> of <b><%=hits.length()%></b>) sorted by <%=sort%> <p><%=slider != null ?
slider.toString(): ""%></p>
Results.prettyPrintHTML(hits, start, start+thispage,
ef.close();
<b> Completed in <%=(new Date()).getTime() - starttime.getTime()%> milliseconds </b> <br/>
%><br/></div><%@include file="foot.jspf"%><%