search.jsp revision 158
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob AusteinCDDL HEADER START
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob AusteinThe contents of this file are subject to the terms of the
5347c0fcb04eaea19d9f39795646239f487c6207Tinderbox UserCommon Development and Distribution License (the "License").
5347c0fcb04eaea19d9f39795646239f487c6207Tinderbox UserYou may not use this file except in compliance with the License.
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob AusteinSee LICENSE.txt included in this distribution for the specific
d6fa26d0adaec6c910115be34fe7a5a5f402c14fMark Andrewslanguage governing permissions and limitations under the License.
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob AusteinWhen distributing Covered Code, include this CDDL HEADER in each
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austeinfile and include the License file at LICENSE.txt.
fd2597f75693a2279fdf588bd40dfe2407c42028Tinderbox UserIf applicable, add the following below this CDDL HEADER, with the
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Huntfields enclosed by brackets "[]" replaced with your own identifying
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austeininformation: Portions Copyright [yyyy] [name of copyright owner]
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob AusteinCDDL HEADER END
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob AusteinCopyright 2005 Sun Microsystems, Inc. All rights reserved.
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob AusteinUse is subject to license terms.
71c66a876ecca77923638d3f94cc0783152b2f03Mark Andrewsident "%Z%%M% %I% %E% SMI"
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein--%><%@ page import = "javax.servlet.*,
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein%><%@ page session="false" %><%@ page errorPage="error.jsp" %><%
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob AusteinDate starttime = new Date();
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob AusteinString defs = request.getParameter("defs");
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob AusteinString refs = request.getParameter("refs");
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob AusteinString hist = request.getParameter("hist");
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob AusteinString path = request.getParameter("path");
fd2597f75693a2279fdf588bd40dfe2407c42028Tinderbox User<%@ include file="projects.jspf" %>
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob AusteinString sort = null;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austeinfinal String LASTMODTIME = "lastmodtime";
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austeinfinal String RELEVANCY = "relevancy";
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob AusteinCookie[] cookies = request.getCookies();
fd2597f75693a2279fdf588bd40dfe2407c42028Tinderbox Userif (cookies != null) {
fd2597f75693a2279fdf588bd40dfe2407c42028Tinderbox User for (Cookie cookie : cookies) {
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein if (cookie.getName().equals("OpenGrok/sorting")) {
fd2597f75693a2279fdf588bd40dfe2407c42028Tinderbox User if (!LASTMODTIME.equals(sort) && !RELEVANCY.equals(sort)) {
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein sort = null;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob AusteinString sortParam = request.getParameter("sort");
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austeinif (sortParam != null) {
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein if (LASTMODTIME.equals(sortParam)) {
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein sort = LASTMODTIME;
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt } else if (RELEVANCY.equals(sortParam)) {
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein sort = RELEVANCY;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein if (sort != null) {
fd2597f75693a2279fdf588bd40dfe2407c42028Tinderbox User Cookie cookie = new Cookie("OpenGrok/sorting", sort);
fd2597f75693a2279fdf588bd40dfe2407c42028Tinderbox UserHits hits = null;
14a656f94b1fd0ababd84a772228dfa52276ba15Evan HuntString errorMsg = null;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austeinif( q!= null && q.equals("")) q = null;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austeinif( defs != null && defs.equals("")) defs = null;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austeinif( refs != null && refs.equals("")) refs = null;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austeinif( hist != null && hist.equals("")) hist = null;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austeinif( path != null && path.equals("")) path = null;
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Huntif (project != null && project.equals("")) project = null;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austeinif (q != null || defs != null || refs != null || hist != null || path != null) {
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein Searcher searcher = null; //the searcher used to open/search the index
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein IndexReader ireader = null; //the reader used to open/search the index
fd2597f75693a2279fdf588bd40dfe2407c42028Tinderbox User Query query = null, defQuery = null; //the Query created by the QueryParser
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein int start = 0; //the first index displayed on this page
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein int max = 25; //the maximum items displayed on this page
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein int thispage = 0; //used for the for/next either max or
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein String moreUrl = null;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein CompatibleAnalyser analyzer = new CompatibleAnalyser();
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein String qstr = "";
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt String result = "";
fd2597f75693a2279fdf588bd40dfe2407c42028Tinderbox User String DATA_ROOT = env.getDataRootPath();
fc74b733bf679e1b3fb1599e32d445dffe325208Tinderbox User throw new Exception("DATA_ROOT parameter is not configured in web.xml!");
fc74b733bf679e1b3fb1599e32d445dffe325208Tinderbox User File data_root = new File(DATA_ROOT);
fd2597f75693a2279fdf588bd40dfe2407c42028Tinderbox User throw new Exception("DATA_ROOT parameter in web.xml does not exist or is not a directory!");
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein ireader = IndexReader.open(DATA_ROOT + "/index");
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein searcher = new IndexSearcher(ireader);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein //String date = request.getParameter("date");
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein start = Integer.parseInt(request.getParameter("start")); //parse the max results first
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein max = Integer.parseInt(request.getParameter("n")); //then the start index
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein if(max < 0 || (max % 10 != 0) || max > 50) max = 25;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein if(start < 0 ) start = 0;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein } catch (Exception e) { }
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein StringBuilder sb = new StringBuilder();
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein if (q != null) {
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein if (defs != null) {
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)) {
String fragment = Util.URIEncode(((TermQuery)query).getTerm().text());
url.append("#");
url.append(fragment);
RuntimeEnvironment environment = RuntimeEnvironment.getInstance();
%><%@ 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"%><%