search.jsp revision b645988bdc1cf4f2f82b8c00ed041ddddd822c24
2b80aa00cbee106b9ec709660eadb6f59f4ca121vboxsyncCDDL HEADER START
2b80aa00cbee106b9ec709660eadb6f59f4ca121vboxsyncThe contents of this file are subject to the terms of the
2b80aa00cbee106b9ec709660eadb6f59f4ca121vboxsyncCommon Development and Distribution License (the "License").
2b80aa00cbee106b9ec709660eadb6f59f4ca121vboxsyncYou may not use this file except in compliance with the License.
2b80aa00cbee106b9ec709660eadb6f59f4ca121vboxsyncSee LICENSE.txt included in this distribution for the specific
2b80aa00cbee106b9ec709660eadb6f59f4ca121vboxsynclanguage governing permissions and limitations under the License.
2b80aa00cbee106b9ec709660eadb6f59f4ca121vboxsyncWhen distributing Covered Code, include this CDDL HEADER in each
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsyncfile and include the License file at LICENSE.txt.
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsyncIf applicable, add the following below this CDDL HEADER, with the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsyncfields enclosed by brackets "[]" replaced with your own identifying
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsyncinformation: Portions Copyright [yyyy] [name of copyright owner]
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsyncCDDL HEADER END
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsyncCopyright 2010 Sun Microsystems, Inc. All rights reserved.
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsyncUse is subject to license terms.
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync--%><%@ page import = "javax.servlet.*,
2b80aa00cbee106b9ec709660eadb6f59f4ca121vboxsync%><%@ page session="false" %><%@ page errorPage="error.jsp" %><%
2b80aa00cbee106b9ec709660eadb6f59f4ca121vboxsyncDate starttime = new Date();
a4d7fc6f54717c342281099fe14f5666be9b7921vboxsyncString q = request.getParameter("q");
2b80aa00cbee106b9ec709660eadb6f59f4ca121vboxsyncString defs = request.getParameter("defs");
5eda82e218d35ae0691febd531e1bfc0324cc4a6vboxsyncString refs = request.getParameter("refs");
aa4bcf0a4b2db3ac352b56a291d49cb8d4b66d32vboxsyncString hist = request.getParameter("hist");
2b80aa00cbee106b9ec709660eadb6f59f4ca121vboxsyncString path = request.getParameter("path");
10f0e3bfdb61e7bae30a1a1d9c17659a908c3d37vboxsync%><%@ include file="projects.jspf" %><%
10f0e3bfdb61e7bae30a1a1d9c17659a908c3d37vboxsyncString sort = null;
10f0e3bfdb61e7bae30a1a1d9c17659a908c3d37vboxsyncfinal String LASTMODTIME = "lastmodtime";
10f0e3bfdb61e7bae30a1a1d9c17659a908c3d37vboxsyncfinal String RELEVANCY = "relevancy";
10f0e3bfdb61e7bae30a1a1d9c17659a908c3d37vboxsyncfinal String BY_PATH = "fullpath";
10f0e3bfdb61e7bae30a1a1d9c17659a908c3d37vboxsyncfinal SortField S_BY_PATH = new SortField(BY_PATH,SortField.STRING);
10f0e3bfdb61e7bae30a1a1d9c17659a908c3d37vboxsyncCookie[] cookies = request.getCookies();
10f0e3bfdb61e7bae30a1a1d9c17659a908c3d37vboxsyncif (cookies != null) {
d8bda85819de53cb1ad1a0074093743ae7455861vboxsync for (Cookie cookie : cookies) {
43a42eb4892f9a9dc5fd7f5f68d24129bc21a81bvboxsync if (!LASTMODTIME.equals(sort) && !RELEVANCY.equals(sort) && !BY_PATH.equals(sort)) {
43a42eb4892f9a9dc5fd7f5f68d24129bc21a81bvboxsync sort = RELEVANCY;
10f0e3bfdb61e7bae30a1a1d9c17659a908c3d37vboxsyncString sortParam = request.getParameter("sort");
10f0e3bfdb61e7bae30a1a1d9c17659a908c3d37vboxsyncif (sortParam != null) {
10f0e3bfdb61e7bae30a1a1d9c17659a908c3d37vboxsync if (LASTMODTIME.equals(sortParam)) {
10f0e3bfdb61e7bae30a1a1d9c17659a908c3d37vboxsync sort = LASTMODTIME;
10f0e3bfdb61e7bae30a1a1d9c17659a908c3d37vboxsync } else if (RELEVANCY.equals(sortParam)) {
10f0e3bfdb61e7bae30a1a1d9c17659a908c3d37vboxsync sort = RELEVANCY;
10f0e3bfdb61e7bae30a1a1d9c17659a908c3d37vboxsync } else if (BY_PATH.equals(sortParam)) {
10f0e3bfdb61e7bae30a1a1d9c17659a908c3d37vboxsync sort = BY_PATH;
10f0e3bfdb61e7bae30a1a1d9c17659a908c3d37vboxsync if (sort != null) {
10f0e3bfdb61e7bae30a1a1d9c17659a908c3d37vboxsync Cookie cookie = new Cookie("OpenGrok/sorting", sort);
10f0e3bfdb61e7bae30a1a1d9c17659a908c3d37vboxsync} else { sort = RELEVANCY; }
10f0e3bfdb61e7bae30a1a1d9c17659a908c3d37vboxsync//List<org.apache.lucene.document.Document> docs=new ArrayList<org.apache.lucene.document.Document>();
10f0e3bfdb61e7bae30a1a1d9c17659a908c3d37vboxsyncString errorMsg = null;
5eda82e218d35ae0691febd531e1bfc0324cc4a6vboxsyncif( q!= null && q.equals("")) q = null;
2b80aa00cbee106b9ec709660eadb6f59f4ca121vboxsyncif( defs != null && defs.equals("")) defs = null;
1d78e6358c47c7fe42af63c999c6308b11325a1avboxsyncif( refs != null && refs.equals("")) refs = null;
10f0e3bfdb61e7bae30a1a1d9c17659a908c3d37vboxsyncif( hist != null && hist.equals("")) hist = null;
10f0e3bfdb61e7bae30a1a1d9c17659a908c3d37vboxsyncif( path != null && path.equals("")) path = null;
10f0e3bfdb61e7bae30a1a1d9c17659a908c3d37vboxsyncif (project != null && project.size()<1) project = null;
10f0e3bfdb61e7bae30a1a1d9c17659a908c3d37vboxsyncif (q != null || defs != null || refs != null || hist != null || path != null) {
2b80aa00cbee106b9ec709660eadb6f59f4ca121vboxsync Searcher searcher = null; //the searcher used to open/search the index
2b80aa00cbee106b9ec709660eadb6f59f4ca121vboxsync TopScoreDocCollector collector=null; // the collector used
2b80aa00cbee106b9ec709660eadb6f59f4ca121vboxsync ScoreDoc[] hits = null; // list of documents which result from the query
2b80aa00cbee106b9ec709660eadb6f59f4ca121vboxsync IndexReader ireader = null; //the reader used to open/search the index
5eda82e218d35ae0691febd531e1bfc0324cc4a6vboxsync Query query = null, defQuery = null; //the Query created by the QueryParser
2b80aa00cbee106b9ec709660eadb6f59f4ca121vboxsync boolean allCollected=false;
10f0e3bfdb61e7bae30a1a1d9c17659a908c3d37vboxsync int totalHits=0;
10f0e3bfdb61e7bae30a1a1d9c17659a908c3d37vboxsync int start = 0; //the first index displayed on this page
10f0e3bfdb61e7bae30a1a1d9c17659a908c3d37vboxsync //TODO deprecate max this and merge with paging and param n - TEST needed
10f0e3bfdb61e7bae30a1a1d9c17659a908c3d37vboxsync //int max = 25; //the maximum items displayed on this page
10f0e3bfdb61e7bae30a1a1d9c17659a908c3d37vboxsync int max=RuntimeEnvironment.getInstance().getHitsPerPage();
2b80aa00cbee106b9ec709660eadb6f59f4ca121vboxsync int hitsPerPage = RuntimeEnvironment.getInstance().getHitsPerPage();
aa4bcf0a4b2db3ac352b56a291d49cb8d4b66d32vboxsync int cachePages= RuntimeEnvironment.getInstance().getCachePages();
2b80aa00cbee106b9ec709660eadb6f59f4ca121vboxsync final boolean docsScoredInOrder=false;
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!");
//String date = request.getParameter("date");
//TODO merge paging hitsPerPage with parameter n (has to reflect the search if changed so proper number is cached first time)
qstr = Util.buildQueryString(q, defs, refs, path, hist);
QueryParser qparser = new QueryParser(Version.LUCENE_CURRENT,"full", analyzer);
query = qparser.parse(qstr); //parse the
File root = new File(RuntimeEnvironment.getInstance().getDataRootFile(),
if (RuntimeEnvironment.getInstance().hasProjects()) {
if (project.size() > 1) { //more projects
IndexSearcher[] searchables = new IndexSearcher[project.size()];
File droot = new File(RuntimeEnvironment.getInstance().getDataRootFile(), "index");
//TODO might need to rewrite to Project instead of String , need changes in projects.jspf too
if (Runtime.getRuntime().availableProcessors() > 1) {
root = new File(root, project.get(0));
//TODO check if below is somehow reusing sessions so we don't requery again and again, I guess 2min timeout sessions could be usefull, since you click on the next page within 2mins, if not, then wait ;)
collector = TopScoreDocCollector.create(hitsPerPage*cachePages,docsScoredInOrder);
if (LASTMODTIME.equals(sort)) {
Sort sortf = new Sort(new SortField("date",SortField.STRING,true));
TopFieldDocs fdocs=searcher.search(query, null,hitsPerPage*cachePages, sortf);
totalHits=fdocs.totalHits;
fdocs=searcher.search(query, null, totalHits, sortf);
hits = fdocs.scoreDocs;
} else if (BY_PATH.equals(sort)) {
TopFieldDocs fdocs=searcher.search(query, null,hitsPerPage*cachePages, sortf);
totalHits=fdocs.totalHits;
fdocs=searcher.search(query, null,totalHits, sortf);
hits = fdocs.scoreDocs;
searcher.search(query,collector);
totalHits=collector.getTotalHits();
collector = TopScoreDocCollector.create(totalHits,docsScoredInOrder);
searcher.search(query,collector);
hits=collector.topDocs().scoreDocs;
// for (int i = 0; i < hits.length; i++) {
// Document d = searcher.doc(docId);
// docs.add(d);
} catch (BooleanQuery.TooManyClauses e) {
errorMsg = "<b>Error parsing your query:</b><br/>" + Util.htmlize(qstr) +
"\"</a><p/> or read the <a href=\"help.jsp\">Help</a> on query language(eventually <a href=\"help.jsp#escaping\">escape special characters</a> with <b>\\</b>)<p/>" +
if (hits != null && hits.length == 1 && request.getServletPath().equals("/s") && (query != null && query instanceof TermQuery)) {
String preFragmentPath = Util.URIEncodePath(context + "/xref" + searcher.doc(hits[0].doc).get("path"));
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"><%
url = 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));
if (sort == null || RELEVANCY.equals(sort)) {
%><b>relevance</b> | <a href="<%=url+LASTMODTIME%>">last modified time</a> | <a href="<%=url+BY_PATH%>">path</a><%
} else if (LASTMODTIME.equals(sort)) {
%><a href="<%=url+RELEVANCY%>">relevance</a> | <b>last modified time</b> | <a href="<%=url+BY_PATH%>">path</a><%
} else if (BY_PATH.equals(sort)) {
%><a href="<%=url+RELEVANCY%>">relevance</a> | <a href="<%=url+LASTMODTIME%>">last modified time</a> | <b>path</b><%
%><a href="<%=url+RELEVANCY%>">relevance</a> | <a href="<%=url+LASTMODTIME%>">last modified time</a> | <a href="<%=url+BY_PATH%>">path</a><%
<%@ include file="menu.jspf"%>
//TODO spellchecking cycle below is not that great and we only create suggest links for every token in query, not for a query as whole
} else if (hits.length == 0) {
File spellIndex = new File(env.getDataRootPath(), "spellIndex");
if (RuntimeEnvironment.getInstance().hasProjects()) {
if (project.size() > 1) { //more projects
spellIndexes = new File[project.size()];
//TODO might need to rewrite to Project instead of String , need changes in projects.jspf too
spellIndex = new File(spellIndex, project.get(0));
if (spellIndexes!=null) {count=spellIndexes.length;}
if (spellIndex.exists()) {
FSDirectory spellDirectory = FSDirectory.open(spellIndex);
toks = q.split("[\t ]+");
for(int j=0; j<toks.length; j++) {
String[] ret = checker.suggestSimilar(toks[j].toLowerCase(), 5);
for(int i = 0;i < ret.length; i++) {
%><p><font color="#cc0000">Did you mean(for <%=spellIndex.getName()%>)</font>:<%
toks = refs.split("[\t ]+");
for(int j=0; j<toks.length; j++) {
String[] ret = checker.suggestSimilar(toks[j].toLowerCase(), 5);
for(int i = 0;i < ret.length; i++) {
%><p><font color="#cc0000">Did you mean(for <%=spellIndex.getName()%>)</font>:<%
//TODO it seems the only true spellchecker is for below field, see IndexDatabase createspellingsuggestions ...
toks = defs.split("[\t ]+");
for(int j=0; j<toks.length; j++) {
String[] ret = checker.suggestSimilar(toks[j].toLowerCase(), 5);
for(int i = 0;i < ret.length; i++) {
%><p><font color="#cc0000">Did you mean(for <%=spellIndex.getName()%>)</font>:<%
%><p> Your search <b><%=query.toString()%></b> did not match any files.
String urlp = (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;
slider.append("<span class=\"sel\">" + label + "</span>");
arr = label < 10 ? " " + label : String.valueOf(label);
slider.append("<a class=\"more\" href=\"s?n=" + max + "&start=" + i + urlp + "\">"+
%> Searched <b><%=query.toString()%></b> (Results <b><%=start+1%> -
slider.toString(): ""%></p>
//TODO also fix the way what and how it is passed to prettyprint, can improve performance! SearchEngine integration is really needed here.
Results.prettyPrintHTML(searcher,hits, start, start+thispage,
ef.close();
<b> Completed in <%=(new Date()).getTime() - starttime.getTime()%> milliseconds </b> <br/>
%><br/></div><%@include file="foot.jspf"%><%