search.jsp revision 158
291N/A<%--
291N/ACDDL HEADER START
291N/A
291N/AThe contents of this file are subject to the terms of the
291N/ACommon Development and Distribution License (the "License").
291N/AYou may not use this file except in compliance with the License.
291N/A
291N/ASee LICENSE.txt included in this distribution for the specific
291N/Alanguage governing permissions and limitations under the License.
291N/A
291N/AWhen distributing Covered Code, include this CDDL HEADER in each
291N/Afile and include the License file at LICENSE.txt.
291N/AIf applicable, add the following below this CDDL HEADER, with the
291N/Afields enclosed by brackets "[]" replaced with your own identifying
291N/Ainformation: Portions Copyright [yyyy] [name of copyright owner]
291N/A
6935N/ACDDL HEADER END
291N/A
291N/ACopyright 2005 Sun Microsystems, Inc. All rights reserved.
291N/AUse is subject to license terms.
291N/A
291N/Aident "%Z%%M% %I% %E% SMI"
291N/A
291N/A--%><%@ page import = "javax.servlet.*,
291N/Ajava.lang.Integer,
291N/Ajavax.servlet.http.*,
291N/Ajava.util.Hashtable,
291N/Ajava.util.Vector,
291N/Ajava.util.Date,
291N/Ajava.util.ArrayList,
291N/Ajava.util.List,
291N/Ajava.lang.*,
291N/Ajava.io.*,
291N/Ajava.io.StringReader,
291N/Aorg.opensolaris.opengrok.analysis.*,
291N/Aorg.opensolaris.opengrok.search.*,
291N/Aorg.opensolaris.opengrok.web.*,
291N/Aorg.opensolaris.opengrok.web.*,
6935N/Aorg.opensolaris.opengrok.search.context.*,
291N/Aorg.opensolaris.opengrok.configuration.*,
291N/Aorg.apache.lucene.search.spell.LuceneDictionary,
291N/Aorg.apache.lucene.search.spell.SpellChecker,
291N/Aorg.apache.lucene.store.FSDirectory,
291N/Aorg.apache.lucene.analysis.*,
291N/Aorg.apache.lucene.document.*,
291N/Aorg.apache.lucene.index.*,
291N/Aorg.apache.lucene.search.*,
291N/Aorg.apache.lucene.queryParser.*"
291N/A%><%@ page session="false" %><%@ page errorPage="error.jsp" %><%
291N/ADate starttime = new Date();
291N/AString q = request.getParameter("q");
291N/AString defs = request.getParameter("defs");
291N/AString refs = request.getParameter("refs");
291N/AString hist = request.getParameter("hist");
291N/AString path = request.getParameter("path");
291N/A
291N/A%>
291N/A<%@ include file="projects.jspf" %>
291N/A<%
291N/AString sort = null;
291N/A
291N/Afinal String LASTMODTIME = "lastmodtime";
291N/Afinal String RELEVANCY = "relevancy";
291N/A
291N/ACookie[] cookies = request.getCookies();
291N/Aif (cookies != null) {
291N/A for (Cookie cookie : cookies) {
291N/A if (cookie.getName().equals("OpenGrok/sorting")) {
291N/A sort = cookie.getValue();
291N/A if (!LASTMODTIME.equals(sort) && !RELEVANCY.equals(sort)) {
291N/A sort = null;
291N/A }
291N/A break;
291N/A }
291N/A }
291N/A}
291N/A
291N/AString sortParam = request.getParameter("sort");
291N/Aif (sortParam != null) {
291N/A if (LASTMODTIME.equals(sortParam)) {
291N/A sort = LASTMODTIME;
6935N/A } else if (RELEVANCY.equals(sortParam)) {
6935N/A sort = RELEVANCY;
6935N/A }
6935N/A if (sort != null) {
6935N/A Cookie cookie = new Cookie("OpenGrok/sorting", sort);
6935N/A response.addCookie(cookie);
6935N/A }
291N/A}
291N/A
291N/AHits hits = null;
291N/AString errorMsg = null;
291N/A
291N/Aif( q!= null && q.equals("")) q = null;
291N/Aif( defs != null && defs.equals("")) defs = null;
291N/Aif( refs != null && refs.equals("")) refs = null;
291N/Aif( hist != null && hist.equals("")) hist = null;
291N/Aif( path != null && path.equals("")) path = null;
291N/Aif (project != null && project.equals("")) project = null;
291N/A
291N/Aif (q != null || defs != null || refs != null || hist != null || path != null) {
291N/A Searcher searcher = null; //the searcher used to open/search the index
291N/A IndexReader ireader = null; //the reader used to open/search the index
291N/A Query query = null, defQuery = null; //the Query created by the QueryParser
291N/A
291N/A int start = 0; //the first index displayed on this page
291N/A int max = 25; //the maximum items displayed on this page
291N/A int thispage = 0; //used for the for/next either max or
291N/A String moreUrl = null;
291N/A CompatibleAnalyser analyzer = new CompatibleAnalyser();
291N/A String qstr = "";
291N/A String result = "";
291N/A try {
291N/A String DATA_ROOT = env.getDataRootPath();
291N/A if(DATA_ROOT.equals("")) {
291N/A throw new Exception("DATA_ROOT parameter is not configured in web.xml!");
291N/A }
291N/A File data_root = new File(DATA_ROOT);
6935N/A if(!data_root.isDirectory()) {
291N/A throw new Exception("DATA_ROOT parameter in web.xml does not exist or is not a directory!");
291N/A }
291N/A ireader = IndexReader.open(DATA_ROOT + "/index");
291N/A searcher = new IndexSearcher(ireader);
291N/A //String date = request.getParameter("date");
291N/A try {
291N/A start = Integer.parseInt(request.getParameter("start")); //parse the max results first
291N/A max = Integer.parseInt(request.getParameter("n")); //then the start index
291N/A if(max < 0 || (max % 10 != 0) || max > 50) max = 25;
291N/A if(start < 0 ) start = 0;
291N/A } catch (Exception e) { }
291N/A
291N/A StringBuilder sb = new StringBuilder();
291N/A if (q != null) {
291N/A sb.append(q);
291N/A }
291N/A
291N/A if (defs != null) {
291N/A sb.append(" defs:(");
291N/A sb.append(defs);
291N/A sb.append(")");
291N/A }
291N/A
291N/A if (refs != null) {
291N/A sb.append(" refs:(");
291N/A sb.append(refs);
291N/A sb.append(")");
291N/A }
6935N/A
291N/A if (path != null) {
291N/A sb.append(" path:(");
291N/A sb.append(path);
291N/A sb.append(")");
291N/A }
6935N/A
291N/A if (hist != null) {
291N/A sb.append(" hist:(");
291N/A sb.append(hist);
291N/A sb.append(")");
291N/A }
291N/A
291N/A if (project != null) {
291N/A sb.append(" (");
291N/A
291N/A boolean first = true;
291N/A for (String s : project.split(" ")) {
291N/A if (first) {
291N/A first = false;
291N/A } else {
291N/A sb.append(" OR ");
291N/A }
291N/A sb.append("project:(");
291N/A sb.append(s);
291N/A sb.append(")");
291N/A }
291N/A
291N/A sb.append(")");
6935N/A }
6935N/A
6935N/A qstr = sb.toString();
6935N/A
6935N/A QueryParser qparser = new QueryParser("full", analyzer);
6935N/A qparser.setDefaultOperator(QueryParser.AND_OPERATOR);
6935N/A qparser.setAllowLeadingWildcard(env.isAllowLeadingWildcard());
6935N/A
6935N/A query = qparser.parse(qstr); //parse the
6935N/A if ("lastmodtime".equals(sort)) {
6935N/A hits = searcher.search(query, new Sort("date", true));
6935N/A } else {
6935N/A hits = searcher.search(query);
6935N/A }
6935N/A thispage = max;
6935N/A } catch (BooleanQuery.TooManyClauses e) {
6935N/A errorMsg = "<b>Error:</b> Too many results for wildcard!";
6935N/A } catch (ParseException e) {
6935N/A errorMsg = "<b>Error:</b><br/>" + Util.Htmlize(qstr) + "<br/>" + Util.Htmlize(e.getMessage());
291N/A } catch (FileNotFoundException e) {
291N/A errorMsg = "<b>Error:</b> Index database not found";
291N/A } catch (Exception e) {
291N/A errorMsg = "<b>Error:</b> " + Util.Htmlize(e.getMessage());
291N/A }
291N/A // @TODO fix me. I should try to figure out where the exact hit is instead
291N/A // of returning a page with just _one_ entry in....
291N/A if (hits != null && hits.length() == 1 && request.getServletPath().equals("/s") && (query != null && query instanceof TermQuery)) {
291N/A String preFragmentPath = Util.URIEncodePath(context + "/xref" + hits.doc(0).get("path"));
291N/A String fragment = Util.URIEncode(((TermQuery)query).getTerm().text());
291N/A
291N/A StringBuilder url = new StringBuilder(preFragmentPath);
291N/A url.append("#");
291N/A url.append(fragment);
291N/A
291N/A response.sendRedirect(url.toString());
291N/A } else {
291N/A String pageTitle = "Search";
291N/A RuntimeEnvironment environment = RuntimeEnvironment.getInstance();
291N/A environment.register();
291N/A %><%@ include file="httpheader.jspf" %>
291N/A<body>
291N/A<div id="page">
291N/A <div id="header"><%@ include file="pageheader.jspf" %></div>
291N/A<div id="Masthead"></div>
291N/A<div id="bar">
291N/A <table border="0" width="100%"><tr><td><a href="<%=context%>" id="home">Home</a></td><td align="right"><%
291N/A {
291N/A StringBuffer url = request.getRequestURL();
291N/A url.append('?');
291N/A String querys = request.getQueryString();
291N/A if (querys != null) {
291N/A int idx = querys.indexOf("sort=");
291N/A if (idx == -1) {
291N/A url.append(querys);
291N/A url.append('&');
291N/A } else {
291N/A url.append(querys.substring(0, idx));
291N/A }
291N/A }
291N/A url.append("sort=");
291N/A
291N/A if (sort == null || RELEVANCY.equals(sort)) {
291N/A url.append(LASTMODTIME);
291N/A %><b>Sort by relevance</b> <a href="<%=url.toString()%>">Sort by last modified time</a><%
291N/A } else {
291N/A url.append(RELEVANCY);
291N/A %><a href="<%=url.toString()%>">Sort by relevance</a> <b>Sort by last modified time</b><%
291N/A }
291N/A } %></td></tr></table>
291N/A</div>
291N/A<div id="menu">
291N/A <%@ include file="menu.jspf"%>
291N/A</div>
291N/A<div id="results">
291N/A<%
291N/Aif( hits == null || errorMsg != null) {
291N/A %><%=errorMsg%><%
291N/A } else if (hits.length() == 0) {
291N/A File spellIndex = new File(env.getDataRootPath(), "spellIndex");
291N/A
291N/A if (spellIndex.exists()) {
291N/A FSDirectory spellDirectory = FSDirectory.getDirectory(spellIndex);
291N/A SpellChecker checker = new SpellChecker(spellDirectory);
291N/A
291N/A Date sstart = new Date();
291N/A
291N/A %><p><font color="#cc0000">Did you mean</font>:<%
291N/A String[] toks;
291N/A if(q != null) {
291N/A toks = q.split("[\t ]+");
291N/A if(toks != null){
291N/A for(int j=0; j<toks.length; j++) {
291N/A if(toks[j].length() > 3) {
291N/A String[] ret = checker.suggestSimilar(toks[j].toLowerCase(), 3);
291N/A for(int i = 0;i < ret.length; i++) {
291N/A %> <a href=search?q=<%=ret[i]%>><%=ret[i]%></a> &nbsp; <%
291N/A }
291N/A }
291N/A }
291N/A }
291N/A }
291N/A if(refs != null) {
291N/A toks = refs.split("[\t ]+");
291N/A if(toks != null){
291N/A for(int j=0; j<toks.length; j++) {
291N/A if(toks[j].length() > 3) {
291N/A String[] ret = checker.suggestSimilar(toks[j].toLowerCase(), 3);
291N/A for(int i = 0;i < ret.length; i++) {
291N/A %> <a href=search?q=<%=ret[i]%>><%=ret[i]%></a> &nbsp; <%
291N/A }
291N/A }
291N/A }
291N/A }
291N/A }
291N/A if(defs != null) {
291N/A toks = defs.split("[\t ]+");
291N/A if(toks != null){
291N/A for(int j=0; j<toks.length; j++) {
291N/A if(toks[j].length() > 3) {
291N/A String[] ret = checker.suggestSimilar(toks[j].toLowerCase(), 3);
291N/A for(int i = 0;i < ret.length; i++) {
291N/A %> <a href=search?q=<%=ret[i]%>><%=ret[i]%></a> &nbsp; <%
291N/A }
291N/A }
291N/A }
}
}
spellDirectory.close();
%></p><%
}
%><p> Your search <b><%=query.toString()%></b> did not match any files.
<br />
Suggestions:<br/><blockquote>- Make sure all terms are spelled correctly.<br/>
- Try different keywords.<br/>
- Try more general keywords.<br/>
- Use 'wil*' cards if you are looking for partial match.
</blockquote>
</p><%
} else { // We have a lots of results to show
StringBuilder slider = null;
if ( max < hits.length()) {
if((start + max) < hits.length()) {
thispage = max;
} else {
thispage = hits.length() - start;
}
String url = (q == null ? "" : "&amp;q=" + Util.URIEncode(q) ) +
(defs == null ? "" : "&amp;defs=" + Util.URIEncode(defs)) +
(refs == null ? "" : "&amp;refs=" + Util.URIEncode(refs)) +
(path == null ? "" : "&amp;path=" + Util.URIEncode(path)) +
(hist == null ? "" : "&amp;hist=" + Util.URIEncode(hist)) +
(sort == null ? "" : "&amp;sort=" + Util.URIEncode(sort));
slider = new StringBuilder();
int labelStart =1;
int sstart = start - max* (start / max % 10 + 1) ;
if(sstart < 0) {
sstart = 0;
labelStart = 1;
} else {
labelStart = sstart/max + 1;
}
int label = labelStart;
int labelEnd = label + 11;
String arr;
for(int i=sstart; i<hits.length() && label <= labelEnd; i+= max) {
if (i <= start && start < i+ max) {
slider.append("<span class=\"sel\">" + label + "</span>");
} else {
if(label == labelStart && label != 1) {
arr = "&lt;&lt";
} else if(label == labelEnd && i < hits.length()) {
arr = "&gt;&gt;";
} else {
arr = label < 10 ? " " + label : String.valueOf(label);
}
slider.append("<a class=\"more\" href=\"search?n=" + max + "&amp;start=" + i + url + "\">"+
arr + "</a>");
}
label++;
}
} else {
thispage = hits.length() - start; // set the max index to max or last
}
%>&nbsp; &nbsp; 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>
<table width="100%" cellpadding="3" cellspacing="0" border="0"><%
Context sourceContext = null;
Summarizer summer = null;
if (query != null) {
try{
sourceContext = new Context(query);
if(sourceContext != null)
summer = new Summarizer(query, analyzer);
} catch (Exception e) {
}
}
HistoryContext historyContext = null;
try {
historyContext = new HistoryContext(query);
} catch (Exception e) {
}
EftarFileReader ef = null;
try{
ef = new EftarFileReader(env.getDataRootPath() + "/index/dtags.eftar");
} catch (Exception e) {
}
Results.prettyPrintHTML(hits, start, start+thispage,
out,
sourceContext, historyContext, summer,
context + "/xref",
context + "/more",
env.getSourceRootPath(),
env.getDataRootPath(),
ef);
if(ef != null) {
try{
ef.close();
} catch (IOException e) {
}
}
%></table><br/>
<b> Completed in <%=(new Date()).getTime() - starttime.getTime()%> milliseconds </b> <br/>
<%=slider != null ? "<p>" + slider + "</p>" : ""%>
<%
}
%><br/></div><%@include file="foot.jspf"%><%
}
if (ireader != null)
ireader.close();
} else { // Entry page show the map
response.sendRedirect(context + "/index.jsp");
}
%>