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