search.jsp revision b645988bdc1cf4f2f82b8c00ed041ddddd822c24
883N/ASee LICENSE.txt included in this distribution for the specific
883N/Afile and include the License file at LICENSE.txt.
883N/A--%><%@ page import = "javax.servlet.*,
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%><%@ include file="projects.jspf" %><%
883N/Afinal SortField S_BY_PATH = new SortField(BY_PATH,SortField.STRING);
883N/ACookie[] cookies = request.getCookies();
883N/A sort = cookie.getValue();
883N/AString sortParam = request.getParameter("sort");
883N/A if (LASTMODTIME.equals(sortParam)) {
883N/A } else if (RELEVANCY.equals(sortParam)) {
883N/A } else if (BY_PATH.equals(sortParam)) {
883N/A Cookie cookie = new Cookie("OpenGrok/sorting", sort);
883N/A response.addCookie(cookie);
883N/A//List<org.apache.lucene.document.Document> docs=new ArrayList<org.apache.lucene.document.Document>();
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 Searcher searcher = null; //the searcher used to open/search the index
883N/A IndexReader ireader = null; //the reader used to open/search the index
883N/A int max=RuntimeEnvironment.getInstance().getHitsPerPage();
883N/A int hitsPerPage = RuntimeEnvironment.getInstance().getHitsPerPage();
883N/A int cachePages= RuntimeEnvironment.getInstance().getCachePages();
883N/A String DATA_ROOT = env.getDataRootPath();
883N/A if(DATA_ROOT.equals("")) {
883N/A if(!data_root.isDirectory()) {
883N/A //String date = request.getParameter("date");
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 qstr = Util.buildQueryString(q, defs, refs, path, hist);
883N/A QueryParser qparser = new QueryParser(Version.LUCENE_CURRENT,"full", analyzer);
883N/A query = qparser.parse(qstr); //parse the
883N/A File root = new File(RuntimeEnvironment.getInstance().getDataRootFile(),
883N/A if (RuntimeEnvironment.getInstance().hasProjects()) {
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 //TODO might need to rewrite to Project instead of String , need changes in projects.jspf too
883N/A if (Runtime.getRuntime().availableProcessors() > 1) {
883N/A root = new File(root, project.get(0));
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 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 hits = fdocs.scoreDocs;
883N/A } else if (BY_PATH.equals(sort)) {
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 hits = fdocs.scoreDocs;
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 hits=collector.topDocs().scoreDocs;
883N/A// for (int i = 0; i < hits.length; i++) {
883N/A// Document d = searcher.doc(docId);
883N/A } catch (BooleanQuery.TooManyClauses e) {
883N/A errorMsg = "<b>Error parsing your query:</b><br/>" + 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 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 url.append("#");
883N/A url.append(fragment);
883N/A RuntimeEnvironment environment = RuntimeEnvironment.getInstance();
883N/A %><%@ include file="httpheader.jspf" %>
883N/A <div id="header"><%@ include file="pageheader.jspf" %></div>
883N/A <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"%><%