search.jsp revision a66e7a81c079afbe13c5a6c0b5bd2c0a776b83f6
5887N/A<%--
5887N/ACDDL HEADER START
5887N/A
5887N/AThe contents of this file are subject to the terms of the
5887N/ACommon Development and Distribution License (the "License").
5887N/AYou may not use this file except in compliance with the License.
5887N/A
5887N/ASee LICENSE.txt included in this distribution for the specific
5887N/Alanguage governing permissions and limitations under the License.
5887N/A
5887N/AWhen distributing Covered Code, include this CDDL HEADER in each
5887N/Afile and include the License file at LICENSE.txt.
5887N/AIf applicable, add the following below this CDDL HEADER, with the
5887N/Afields enclosed by brackets "[]" replaced with your own identifying
5887N/Ainformation: Portions Copyright [yyyy] [name of copyright owner]
5887N/A
5887N/ACDDL HEADER END
5887N/A
5887N/ACopyright 2005 Sun Microsystems, Inc. All rights reserved.
5887N/AUse is subject to license terms.
5887N/A
5887N/Aident "%Z%%M% %I% %E% SMI"
5887N/A
5887N/A--%><%@ page import = "javax.servlet.*,
5887N/Ajava.lang.Integer,
5887N/Ajavax.servlet.http.*,
5887N/Ajava.util.Hashtable,
5887N/Ajava.util.Vector,
5887N/Ajava.util.Date,
5887N/Ajava.util.ArrayList,
5887N/Ajava.util.List,
5887N/Ajava.lang.*,
5887N/Ajava.io.*,
5887N/Ajava.io.StringReader,
5887N/Aorg.opensolaris.opengrok.analysis.*,
5887N/Aorg.opensolaris.opengrok.search.*,
5887N/Aorg.opensolaris.opengrok.web.*,
5887N/Aorg.opensolaris.opengrok.web.*,
5887N/Aorg.opensolaris.opengrok.search.context.*,
5887N/Aorg.opensolaris.opengrok.configuration.*,
5887N/Aorg.apache.lucene.spell.*,
5887N/Aorg.apache.lucene.analysis.*,
5887N/Aorg.apache.lucene.document.*,
5887N/Aorg.apache.lucene.index.*,
5887N/Aorg.apache.lucene.search.*,
5887N/Aorg.apache.lucene.queryParser.*"
5887N/A%><%@ page session="false" %><%@ page errorPage="error.jsp" %><%
5887N/ADate starttime = new Date();
5887N/AString q = request.getParameter("q");
5887N/AString defs = request.getParameter("defs");
5887N/AString refs = request.getParameter("refs");
5887N/AString hist = request.getParameter("hist");
5887N/AString path = request.getParameter("path");
5887N/AString sort = request.getParameter("sort");
5887N/A
5887N/A%>
5887N/A<%@ include file="projects.jspf" %>
5887N/A<%
5887N/A
5887N/AHits hits = null;
5887N/AString errorMsg = null;
5887N/AString context = request.getContextPath();
5887N/A
5887N/Aif( q!= null && q.equals("")) q = null;
5887N/Aif( defs != null && defs.equals("")) defs = null;
5887N/Aif( refs != null && refs.equals("")) refs = null;
5887N/Aif( hist != null && hist.equals("")) hist = null;
5887N/Aif( path != null && path.equals("")) path = null;
5887N/Aif( sort != null && sort.equals("")) sort = null;
5887N/Aif (project != null && project.equals("")) project = null;
5887N/A
5887N/Aif (q != null || defs != null || refs != null || hist != null || path != null) {
5887N/A Searcher searcher = null; //the searcher used to open/search the index
5887N/A IndexReader ireader = null; //the reader used to open/search the index
5887N/A Query query = null, defQuery = null; //the Query created by the QueryParser
5887N/A
5887N/A int start = 0; //the first index displayed on this page
5887N/A int max = 25; //the maximum items displayed on this page
5887N/A int thispage = 0; //used for the for/next either max or
5887N/A String moreUrl = null;
5887N/A CompatibleAnalyser analyzer = new CompatibleAnalyser();
5887N/A String qstr = "";
5887N/A String result = "";
5887N/A try {
5887N/A String DATA_ROOT = env.getDataRootPath();
5887N/A if(DATA_ROOT.equals("")) {
5887N/A throw new Exception("DATA_ROOT parameter is not configured in web.xml!");
5887N/A }
5887N/A File data_root = new File(DATA_ROOT);
5887N/A if(!data_root.isDirectory()) {
5887N/A throw new Exception("DATA_ROOT parameter in web.xml does not exist or is not a directory!");
5887N/A }
5887N/A ireader = IndexReader.open(DATA_ROOT + "/index");
5887N/A searcher = new IndexSearcher(ireader);
5887N/A //String date = request.getParameter("date");
5887N/A try {
5887N/A start = Integer.parseInt(request.getParameter("start")); //parse the max results first
5887N/A max = Integer.parseInt(request.getParameter("n")); //then the start index
5887N/A if(max < 0 || (max % 10 != 0) || max > 50) max = 25;
5887N/A if(start < 0 ) start = 0;
5887N/A } catch (Exception e) { }
5887N/A
5887N/A StringBuilder sb = new StringBuilder();
5887N/A if (q != null) {
5887N/A sb.append(q);
5887N/A }
5887N/A
5887N/A if (defs != null) {
5887N/A sb.append(" defs:(");
5887N/A sb.append(defs);
5887N/A sb.append(")");
5887N/A }
5887N/A
5887N/A if (refs != null) {
5887N/A sb.append(" refs:(");
5887N/A sb.append(refs);
5887N/A sb.append(")");
5887N/A }
5887N/A
5887N/A if (path != null) {
5887N/A sb.append(" path:(");
5887N/A sb.append(path);
5887N/A sb.append(")");
5887N/A }
5887N/A
5887N/A if (hist != null) {
5887N/A sb.append(" hist:(");
5887N/A sb.append(hist);
5887N/A sb.append(")");
5887N/A }
5887N/A
5887N/A if (project != null) {
5887N/A sb.append(" (");
5887N/A
5887N/A boolean first = true;
5887N/A for (String s : project.split(" ")) {
5887N/A if (first) {
5887N/A first = false;
5887N/A } else {
5887N/A sb.append(" OR ");
5887N/A }
5887N/A sb.append("project:(");
5887N/A sb.append(s);
5887N/A sb.append(")");
5887N/A }
5887N/A
5887N/A sb.append(")");
5887N/A }
5887N/A
5887N/A qstr = sb.toString();
5887N/A
5887N/A QueryParser qparser = new QueryParser("full", analyzer);
5887N/A qparser.setOperator(QueryParser.DEFAULT_OPERATOR_AND);
5887N/A query = qparser.parse(qstr); //parse the
5887N/A if (sort == null || sort.equals("relevancy")) {
5887N/A hits = searcher.search(query);
5887N/A }
5887N/A else if (sort.equals("lastmodtime")) {
5887N/A hits = searcher.search(query, new Sort("date", true));
5887N/A }
5887N/A else {
5887N/A hits = searcher.search(query);
5887N/A }
5887N/A thispage = max;
5887N/A } catch (BooleanQuery.TooManyClauses e) {
5887N/A errorMsg = "<b>Error:</b> Too many results for wildcard!";
5887N/A } catch (ParseException e) {
5887N/A errorMsg = "<b>Error:</b><br/>" + Util.Htmlize(qstr) + "<br/>" + Util.Htmlize(e.getMessage());
5887N/A } catch (FileNotFoundException e) {
5887N/A errorMsg = "<b>Error:</b> Index database not found";
5887N/A } catch (Exception e) {
5887N/A errorMsg = "<b>Error:</b> " + Util.Htmlize(e.getMessage());
5887N/A }
5887N/A if (hits != null && hits.length() == 1 && request.getServletPath().equals("/s")) {
5887N/A if (query != null && query instanceof TermQuery) {
5887N/A response.sendRedirect(context + "/xref" + hits.doc(0).get("path")
5887N/A + "#" + ((TermQuery)query).getTerm().text());
5887N/A } else {
5887N/A response.sendRedirect(context + "/xref" + hits.doc(0).get("path"));
5887N/A }
5887N/A } else {
5887N/A %><?xml version="1.0" encoding="iso-8859-1"?>
5887N/A<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
5887N/A<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
5887N/A<head>
5887N/A <meta name="robots" content="noindex,nofollow">
5887N/A <link rel="icon" href="img/icon.png" type="image/png"/>
5887N/A <link rel="stylesheet" type="text/css" href="style.css"/>
5887N/A <link rel="stylesheet" type="text/css" href="print.css" media="print" />
5887N/A <link rel="alternate stylesheet" type="text/css" media="all" title="Paper White" href="print.css"/>
5887N/A <title>Search</title>
5887N/A</head>
5887N/A<body>
5887N/A<div id="page">
5887N/A <div id="header">
5887N/A <%= getServletContext().getInitParameter("HEADER") %>
5887N/A </div>
5887N/A<div id="Masthead"></div>
5887N/A<div id="bar">
5887N/A <%@ include file="menu.jspf"%>
5887N/A<!-- table cellpadding="0" cellspacing="0" border="0" width="100%">
5887N/A <tr>
5887N/A <td valign="top"><br /> &nbsp;</td>
5887N/A <td align="left" valign="middle">
5887N/A <br/><form action="search" name="sbox">
5887N/A <table cellpadding="2" border="0" cellspacing="0">
<tr valign="top">
<td>
<table cellpadding="2" border="0" cellspacing="0">
<tr><td align="right"> Full&nbsp;Search </td><td><input class="q" name="q" size="45" style="width: 300px" value="<%=Util.formQuoteEscape(q)%>"/></td></tr>
<tr><td align="right"> Definition </td><td><input class="q" name="defs" size="25" style="width: 300px" value="<%=Util.formQuoteEscape(defs)%>"/></td></tr>
<tr><td align="right"> Symbol </td><td><input class="q" name="refs" size="25" style="width: 300px" value="<%=Util.formQuoteEscape(refs)%>"/></td></tr>
<tr><td align="right"> File&nbsp;Path </td><td><input class="q" name="path" size="25" style="width: 300px" value="<%=Util.formQuoteEscape(path)%>"/></td></tr>
<tr><td align="right"> History </td><td><input class="q" name="hist" size="25" style="width: 300px" value="<%=Util.formQuoteEscape(hist)%>"/></td></tr>
<% if (sort == null || sort.equals("relevancy")) { %>
<tr><td align="right"> sort by </td><td><select class="q" name="sort"><option value="relevancy" selected>relevancy</option><option value="lastmodtime">last modified time</option></select></td></tr>
<%
}
else if (sort.equals("lastmodtime")) { %>
<tr><td align="right"> sort by </td><td><select class="q" name="sort"><option value="relevancy">relevancy</option><option value="lastmodtime" selected>last modified time</option></select></td></tr>
<%
}
else { %>
<tr><td align="right"> sort by </td><td><select class="q" name="sort"><option value="relevancy" selected>relevancy</option><option value="lastmodtime">last modified time</option></select></td></tr>
<%
}
%>
</table>
</td>
<% if (hasProjects) { %>
<td>
Project:<br>
<select class="q" name="project" size="6" multiple="true" style="width: 300px">
<% for (Project p : env.getProjects()) {
%><option value="<%=Util.formQuoteEscape(p.getPath())%>"<%=p.getPath().equals(project) ? " selected" : ""%>><%=Util.formQuoteEscape(p.getDescription())%></option><%
} %>
</select>
</td>
<% } %>
</tr>
<tr><td></td><td> &nbsp; <input class="submit" type="submit" value="Search"/> | <input class="submit"
onClick="document.sbox.q.value='';document.sbox.defs.value='';document.sbox.refs.value='';document.sbox.path.value='';document.sbox.hist.value='';" type="button" value=" Clear "
/> | <a href="help.html">Help</a></td></tr>
</table></form>
</td>
<td valign="top" align="right"></td></tr>
</table -->
</div>
<div id="results">
<%
if( hits == null || errorMsg != null) {
%><%=errorMsg%><%
} else if (hits.length() == 0) {
String ngramIndex = env.getDataRootPath() + "/spellIndex";
if (ngramIndex != null && (new
File(ngramIndex+"/segments")).exists()) {
Date sstart = new Date();
IndexReader spellReader = IndexReader.open(ngramIndex);
IndexSearcher spellSearcher = new IndexSearcher(spellReader);
%><p><font color="#cc0000">Did you mean</font>:<%
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 = NGramSpeller.suggestUsingNGrams(spellSearcher,toks[j].toLowerCase(), 3, 4, 3, 5, 3, 2, 0, null, false);
for(int i = 0;i < ret.length; i++) {
%> <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 = NGramSpeller.suggestUsingNGrams(spellSearcher,toks[j].toLowerCase(),3, 4, 3, 5, 3, 2, 0, null, false);
for(int i = 0;i < ret.length; i++) {
%> <a href=search?q=<%=ret[i]%>><%=ret[i]%></a> &nbsp; <%
}
}
}
}
}
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 = NGramSpeller.suggestUsingNGrams(spellSearcher,toks[j].toLowerCase(),3, 4, 3, 5, 3, 2, 0, null, false);
for(int i = 0;i < ret.length; i++) {
%> <a href=search?q=<%=ret[i]%>><%=ret[i]%></a> &nbsp; <%
}
}
}
}
}
spellSearcher.close();
spellReader.close();
%></p><%
}
%><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 < hits.length()) {
if((start + max) < hits.length()) {
thispage = max;
} else {
thispage = hits.length() - start;
}
String 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)) +
(sort == null ? "" : "&amp;sort=" + Util.URIEncode(sort));
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<hits.length() && 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 < hits.length()) {
arr = "&gt;&gt;";
} else {
arr = label < 10 ? " " + label : String.valueOf(label);
}
slider.append("<a class=\"more\" href=\"search?n=" + max + "&amp;start=" + i + url + "\">"+
arr + "</a>");
}
label++;
}
} else {
thispage = hits.length() - 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><%=hits.length()%></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) {
}
Results.prettyPrintHTML(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");
}
%>