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