search.jsp revision 853
98N/A<%--
98N/ACDDL HEADER START
98N/A
98N/AThe contents of this file are subject to the terms of the
1088N/ACommon Development and Distribution License (the "License").
98N/AYou may not use this file except in compliance with the License.
98N/A
919N/ASee LICENSE.txt included in this distribution for the specific
919N/Alanguage governing permissions and limitations under the License.
919N/A
919N/AWhen distributing Covered Code, include this CDDL HEADER in each
919N/Afile and include the License file at LICENSE.txt.
919N/AIf applicable, add the following below this CDDL HEADER, with the
919N/Afields enclosed by brackets "[]" replaced with your own identifying
919N/Ainformation: Portions Copyright [yyyy] [name of copyright owner]
919N/A
919N/ACDDL HEADER END
919N/A
919N/ACopyright 2009 Sun Microsystems, Inc. All rights reserved.
919N/AUse is subject to license terms.
919N/A
919N/Aident "%Z%%M% %I% %E% SMI"
919N/A
919N/A--%><%@ page import = "javax.servlet.*,
98N/Ajava.lang.Integer,
98N/Ajavax.servlet.http.*,
98N/Ajava.util.Hashtable,
98N/Ajava.util.Vector,
98N/Ajava.util.Date,
98N/Ajava.util.ArrayList,
493N/Ajava.util.List,
493N/Ajava.lang.*,
98N/Ajava.io.*,
911N/Ajava.io.StringReader,
1179N/Aorg.opensolaris.opengrok.analysis.*,
1179N/Aorg.opensolaris.opengrok.index.IndexDatabase,
1179N/Aorg.opensolaris.opengrok.search.*,
911N/Aorg.opensolaris.opengrok.web.*,
98N/Aorg.opensolaris.opengrok.search.context.*,
235N/Aorg.opensolaris.opengrok.configuration.*,
493N/Aorg.apache.lucene.search.spell.LuceneDictionary,
98N/Aorg.apache.lucene.search.spell.SpellChecker,
916N/Aorg.apache.lucene.store.FSDirectory,
916N/Aorg.apache.lucene.analysis.*,
916N/Aorg.apache.lucene.document.*,
916N/Aorg.apache.lucene.index.*,
970N/Aorg.apache.lucene.search.*,
970N/Aorg.apache.lucene.queryParser.*"
970N/A%><%@ page session="false" %><%@ page errorPage="error.jsp" %><%
970N/ADate starttime = new Date();
970N/AString q = request.getParameter("q");
970N/AString defs = request.getParameter("defs");
970N/AString refs = request.getParameter("refs");
970N/AString hist = request.getParameter("hist");
970N/AString path = request.getParameter("path");
970N/A
970N/A%><%@ include file="projects.jspf" %><%
970N/AString sort = null;
970N/A
970N/Afinal String LASTMODTIME = "lastmodtime";
970N/Afinal String RELEVANCY = "relevancy";
970N/Afinal String BY_PATH = "fullpath";
970N/A
970N/ACookie[] cookies = request.getCookies();
970N/Aif (cookies != null) {
970N/A for (Cookie cookie : cookies) {
970N/A if (cookie.getName().equals("OpenGrok/sorting")) {
970N/A sort = cookie.getValue();
970N/A if (!LASTMODTIME.equals(sort) && !RELEVANCY.equals(sort) && !BY_PATH.equals(sort)) {
970N/A sort = RELEVANCY;
970N/A }
1029N/A break;
1029N/A }
1124N/A }
1124N/A}
1124N/A
1124N/AString sortParam = request.getParameter("sort");
1124N/Aif (sortParam != null) {
1124N/A if (LASTMODTIME.equals(sortParam)) {
970N/A sort = LASTMODTIME;
98N/A } else if (RELEVANCY.equals(sortParam)) {
98N/A sort = RELEVANCY;
1124N/A } else if (BY_PATH.equals(sortParam)) {
1124N/A sort = BY_PATH;
1124N/A }
1124N/A if (sort != null) {
1124N/A Cookie cookie = new Cookie("OpenGrok/sorting", sort);
1124N/A response.addCookie(cookie);
1124N/A }
1124N/A} else { sort = RELEVANCY; }
98N/A
493N/A//List<org.apache.lucene.document.Document> docs=new ArrayList<org.apache.lucene.document.Document>();
98N/AString errorMsg = null;
970N/A
970N/Aif( q!= null && q.equals("")) q = null;
970N/Aif( defs != null && defs.equals("")) defs = null;
1026N/Aif( refs != null && refs.equals("")) refs = null;
970N/Aif( hist != null && hist.equals("")) hist = null;
970N/Aif( path != null && path.equals("")) path = null;
1026N/Aif (project != null && project.size()<1) project = null;
1124N/A
1003N/Aif (q != null || defs != null || refs != null || hist != null || path != null) {
98N/A Searcher searcher = null; //the searcher used to open/search the index
98N/A TopDocCollector collector=null; // the collector used
98N/A ScoreDoc[] hits = null; // list of documents which result from the query
908N/A IndexReader ireader = null; //the reader used to open/search the index
591N/A Query query = null, defQuery = null; //the Query created by the QueryParser
493N/A boolean allCollected=false;
493N/A int totalHits=0;
1201N/A
1201N/A int start = 0; //the first index displayed on this page
1201N/A //TODO deprecate max this and merge with paging and param n - TEST needed
1201N/A //int max = 25; //the maximum items displayed on this page
1201N/A int max=RuntimeEnvironment.getInstance().getHitsPerPage();
1201N/A
1201N/A int hitsPerPage = RuntimeEnvironment.getInstance().getHitsPerPage();
1201N/A int cachePages= RuntimeEnvironment.getInstance().getCachePages();
1201N/A
1201N/A int thispage = 0; //used for the for/next either max or
493N/A String moreUrl = null;
493N/A CompatibleAnalyser analyzer = new CompatibleAnalyser();
493N/A String qstr = "";
493N/A String result = "";
493N/A try {
493N/A String DATA_ROOT = env.getDataRootPath();
1026N/A if(DATA_ROOT.equals("")) {
705N/A throw new Exception("DATA_ROOT parameter is not configured in web.xml!");
493N/A }
557N/A File data_root = new File(DATA_ROOT);
1176N/A if(!data_root.isDirectory()) {
493N/A throw new Exception("DATA_ROOT parameter in web.xml does not exist or is not a directory!");
965N/A }
965N/A //String date = request.getParameter("date");
965N/A try {
1196N/A //TODO merge paging hitsPerPage with parameter n (has to reflect the search if changed so proper number is cached first time)
1196N/A start = Integer.parseInt(request.getParameter("start")); //parse the max results first
1196N/A max = Integer.parseInt(request.getParameter("n")); //then the start index
1196N/A if(max < 0 || (max % 10 != 0) || max > 50) max = 25;
1196N/A if(start < 0 ) start = 0;
1196N/A } catch (Exception e) { }
1196N/A
1196N/A qstr = Util.buildQueryString(q, defs, refs, path, hist);
1196N/A
1196N/A QueryParser qparser = new QueryParser("full", analyzer);
1196N/A qparser.setDefaultOperator(QueryParser.AND_OPERATOR);
606N/A qparser.setAllowLeadingWildcard(env.isAllowLeadingWildcard());
606N/A
851N/A query = qparser.parse(qstr); //parse the
851N/A
851N/A File root = new File(RuntimeEnvironment.getInstance().getDataRootFile(),
851N/A "index");
851N/A
851N/A if (RuntimeEnvironment.getInstance().hasProjects()) {
851N/A if (project == null) {
98N/A errorMsg = "<b>Error:</b> You must select a project!";
1193N/A } else {
1193N/A if (project.size() > 1) { //more projects
1193N/A IndexSearcher[] searchables = new IndexSearcher[project.size()];
591N/A File droot = new File(RuntimeEnvironment.getInstance().getDataRootFile(), "index");
963N/A int ii = 0;
1201N/A //TODO might need to rewrite to Project instead of String , need changes in projects.jspf too
910N/A for (String proj : project) {
1097N/A ireader = (IndexReader.open(new File(droot, proj)));
910N/A searchables[ii++] = new IndexSearcher(ireader);
1097N/A }
910N/A if (Runtime.getRuntime().availableProcessors() > 1) {
98N/A searcher = new ParallelMultiSearcher(searchables);
98N/A } else {
910N/A searcher = new MultiSearcher(searchables);
910N/A }
970N/A } else { // just 1 project selected
1030N/A root = new File(root, project.get(0));
1030N/A ireader = IndexReader.open(root);
970N/A searcher = new IndexSearcher(ireader);
970N/A }
970N/A }
1097N/A } else { //no project setup
1097N/A ireader = IndexReader.open(root);
1030N/A searcher = new IndexSearcher(ireader);
1030N/A }
910N/A
98N/A //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 ;)
1193N/A if (errorMsg == null) {
1193N/A collector = new TopDocCollector(hitsPerPage*cachePages);
1193N/A if (LASTMODTIME.equals(sort)) {
1193N/A Sort sortf = new Sort("date", true);
1193N/A TopFieldDocs fdocs=searcher.search(query, null,hitsPerPage*cachePages, sortf);
1193N/A totalHits=fdocs.totalHits;
1193N/A if (start>=hitsPerPage*cachePages && !allCollected) { //fetch ALL results only if above cachePages
1193N/A fdocs=searcher.search(query, null, totalHits, sortf);
1193N/A allCollected=true;
1193N/A }
1193N/A hits = fdocs.scoreDocs;
606N/A } else if (BY_PATH.equals(sort)) {
98N/A Sort sortf = new Sort(BY_PATH);
606N/A TopFieldDocs fdocs=searcher.search(query, null,hitsPerPage*cachePages, sortf);
98N/A totalHits=fdocs.totalHits;
591N/A if (start>=hitsPerPage*cachePages && !allCollected) { //fetch ALL results only if above cachePages
851N/A fdocs=searcher.search(query, null,totalHits, sortf);
111N/A allCollected=true;
606N/A }
1088N/A hits = fdocs.scoreDocs;
1088N/A } else {
1088N/A searcher.search(query,collector);
733N/A totalHits=collector.getTotalHits();
970N/A if (start>=hitsPerPage*cachePages && !allCollected) { //fetch ALL results only if above cachePages
733N/A collector = new TopDocCollector(totalHits);
733N/A searcher.search(query,collector);
963N/A allCollected=true;
851N/A }
963N/A hits=collector.topDocs().scoreDocs;
1088N/A }
1088N/A
1088N/A //below will get all the documents
1088N/A// for (int i = 0; i < hits.length; i++) {
98N/A// int docId = hits[i].doc;
851N/A// Document d = searcher.doc(docId);
1097N/A// docs.add(d);
1097N/A// }
851N/A
935N/A }
851N/A thispage = max;
851N/A } catch (BooleanQuery.TooManyClauses e) {
935N/A errorMsg = "<b>Error:</b> Too many results for wildcard!";
606N/A } catch (ParseException e) {
935N/A errorMsg = "<b>Error parsing your query:</b><br/>" + Util.htmlize(qstr) +
935N/A "<p/>You might try to enclose your search term in quotes: <br/>" +
935N/A "<a href=search?q=\"" + Util.URIEncode(qstr) + "\">\"" + Util.htmlize(qstr) +
935N/A "\"</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/>" +
212N/A "Error message from parser:<br/>" + Util.htmlize(e.getMessage());
1183N/A } catch (FileNotFoundException e) {
1183N/A errorMsg = "<b>Error:</b> Index database not found";
1183N/A } catch (Exception e) {
1183N/A errorMsg = "<b>Error:</b> " + Util.htmlize(e.getMessage());
1196N/A }
1183N/A
1183N/A // @TODO fix me. I should try to figure out where the exact hit is instead
1183N/A // of returning a page with just _one_ entry in....
1233N/A if (hits != null && hits.length == 1 && request.getServletPath().equals("/s") && (query != null && query instanceof TermQuery)) {
1196N/A String preFragmentPath = Util.URIEncodePath(context + "/xref" + searcher.doc(hits[0].doc).get("path"));
1196N/A String fragment = Util.URIEncode(((TermQuery)query).getTerm().text());
1183N/A
98N/A StringBuilder url = new StringBuilder(preFragmentPath);
98N/A url.append("#");
1196N/A url.append(fragment);
1183N/A
1196N/A response.sendRedirect(url.toString());
908N/A } else {
963N/A String pageTitle = "Search";
963N/A RuntimeEnvironment environment = RuntimeEnvironment.getInstance();
963N/A environment.register();
963N/A %><%@ include file="httpheader.jspf" %>
963N/A<body>
963N/A<div id="page">
1030N/A <div id="header"><%@ include file="pageheader.jspf" %></div>
970N/A<div id="Masthead"></div>
1097N/A<div id="bar">
1097N/A <table border="0" width="100%"><tr><td><a href="<%=context%>" id="home">Home</a></td><td align="right"><%
970N/A {
970N/A String url = "search?";
970N/A url = url + (q == null ? "" : "&amp;q=" + Util.URIEncode(q)) +
970N/A (defs == null ? "" : "&amp;defs=" + Util.URIEncode(defs)) +
970N/A (refs == null ? "" : "&amp;refs=" + Util.URIEncode(refs)) +
970N/A (path == null ? "" : "&amp;path=" + Util.URIEncode(path)) +
1152N/A (hist == null ? "" : "&amp;hist=" + Util.URIEncode(hist));
970N/A if (hasProjects) {
970N/A url = url + "&amp;project=";
970N/A for (Iterator it = project.iterator(); it.hasNext();) {
970N/A url = url + (project == null ? "" : Util.URIEncode((String) it.next()) + ",");
1026N/A }
974N/A }
970N/A
970N/A %>Sort by: <%
1026N/A url=url+("&amp;sort=");
970N/A
970N/A if (sort == null || RELEVANCY.equals(sort)) {
974N/A %><b>relevance</b> | <a href="<%=url+LASTMODTIME%>">last modified time</a> | <a href="<%=url+BY_PATH%>">path</a><%
974N/A } else if (LASTMODTIME.equals(sort)) {
970N/A %><a href="<%=url+RELEVANCY%>">relevance</a> | <b>last modified time</b> | <a href="<%=url+BY_PATH%>">path</a><%
1026N/A } else if (BY_PATH.equals(sort)) {
970N/A %><a href="<%=url+RELEVANCY%>">relevance</a> | <a href="<%=url+LASTMODTIME%>">last modified time</a> | <b>path</b><%
970N/A } else {
970N/A %><a href="<%=url+RELEVANCY%>">relevance</a> | <a href="<%=url+LASTMODTIME%>">last modified time</a> | <a href="<%=url+BY_PATH%>">path</a><%
970N/A }
970N/A } %></td></tr></table>
970N/A</div>
970N/A<div id="menu">
970N/A <%@ include file="menu.jspf"%>
970N/A</div>
970N/A<div id="results">
970N/A<%
970N/A//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
970N/Aif( hits == null || errorMsg != null) {
970N/A %><%=errorMsg%><%
970N/A } else if (hits.length == 0) {
970N/A File spellIndex = new File(env.getDataRootPath(), "spellIndex");
1003N/A File[] spellIndexes=null;
1003N/A
1003N/A if (RuntimeEnvironment.getInstance().hasProjects()) {
1003N/A if (project.size() > 1) { //more projects
1003N/A spellIndexes = new File[project.size()];
1003N/A int ii = 0;
970N/A //TODO might need to rewrite to Project instead of String , need changes in projects.jspf too
970N/A for (String proj : project) {
970N/A spellIndexes[ii++] = new File(spellIndex,proj);
970N/A }
970N/A } else { // just 1 project selected
970N/A spellIndex = new File(spellIndex, project.get(0));
970N/A }
970N/A }
970N/A
970N/A int count=1;
970N/A if (spellIndexes!=null) {count=spellIndexes.length;}
970N/A
970N/A for (int idx = 0; idx < count; idx++) {
1030N/A
1030N/A if (spellIndexes!=null) spellIndex = spellIndexes[idx];
1030N/A
1030N/A if (spellIndex.exists()) {
1030N/A FSDirectory spellDirectory = FSDirectory.getDirectory(spellIndex);
1030N/A SpellChecker checker = new SpellChecker(spellDirectory);
1030N/A
1030N/A Date sstart = new Date();
1030N/A boolean printHeader = true;
1030N/A String[] toks;
1030N/A if(q != null) {
1030N/A toks = q.split("[\t ]+");
1030N/A if(toks != null){
1030N/A for(int j=0; j<toks.length; j++) {
1030N/A if(toks[j].length() > 3) {
1030N/A String[] ret = checker.suggestSimilar(toks[j].toLowerCase(), 5);
1030N/A for(int i = 0;i < ret.length; i++) {
1030N/A if (printHeader) {
1030N/A %><p><font color="#cc0000">Did you mean(for <%=spellIndex.getName()%>)</font>:<%
1030N/A printHeader = false;
1030N/A }
1030N/A %> <a href=search?q=<%=ret[i]%>><%=ret[i]%></a> &nbsp; <%
1030N/A }
1030N/A }
1192N/A }
1030N/A }
1030N/A }
1030N/A if(refs != null) {
1030N/A toks = refs.split("[\t ]+");
1030N/A if(toks != null){
1030N/A for(int j=0; j<toks.length; j++) {
1030N/A if(toks[j].length() > 3) {
1030N/A String[] ret = checker.suggestSimilar(toks[j].toLowerCase(), 5);
1030N/A for(int i = 0;i < ret.length; i++) {
1030N/A if (printHeader) {
1030N/A %><p><font color="#cc0000">Did you mean(for <%=spellIndex.getName()%>)</font>:<%
1030N/A printHeader = false;
1030N/A }
1030N/A %> <a href=search?refs=<%=ret[i]%>><%=ret[i]%></a> &nbsp; <%
1030N/A }
1030N/A }
1030N/A }
1030N/A }
1030N/A }
1030N/A //TODO it seems the only true spellchecker is for below field, see IndexDatabase createspellingsuggestions ...
1030N/A if(defs != null) {
1030N/A toks = defs.split("[\t ]+");
1030N/A if(toks != null){
1030N/A for(int j=0; j<toks.length; j++) {
1030N/A if(toks[j].length() > 3) {
1030N/A String[] ret = checker.suggestSimilar(toks[j].toLowerCase(), 5);
1201N/A for(int i = 0;i < ret.length; i++) {
1201N/A if (printHeader) {
1201N/A %><p><font color="#cc0000">Did you mean(for <%=spellIndex.getName()%>)</font>:<%
1201N/A printHeader = false;
1201N/A }
1201N/A %> <a href=search?defs=<%=ret[i]%>><%=ret[i]%></a> &nbsp; <%
}
}
}
}
}
if (printHeader) {
%></p><%
}
spellDirectory.close();
}
}
%><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 < totalHits) {
if((start + max) < totalHits) {
thispage = max;
} else {
thispage = totalHits - start;
}
String urlp = (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));
if (hasProjects) {
urlp = urlp + "&amp;project=";
for (Iterator it = project.iterator(); it.hasNext();) {
urlp = urlp + (project == null ? "" : Util.URIEncode((String) it.next()) + ",");
}
}
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<totalHits && 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 < totalHits) {
arr = "&gt;&gt;";
} else {
arr = label < 10 ? " " + label : String.valueOf(label);
}
slider.append("<a class=\"more\" href=\"s?n=" + max + "&amp;start=" + i + urlp + "\">"+
arr + "</a>");
}
label++;
}
} else {
thispage = totalHits - 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><%=totalHits%></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) {
}
//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,
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");
}
%>