search.jsp revision 58
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 2005 Sun Microsystems, Inc. All rights reserved.
0N/AUse is subject to license terms.
0N/A
0N/Aident "%Z%%M% %I% %E% SMI"
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.*,
0N/Ajava.io.*,
0N/Ajava.io.StringReader,
0N/Aorg.opensolaris.opengrok.analysis.*,
0N/Aorg.opensolaris.opengrok.search.*,
0N/Aorg.opensolaris.opengrok.web.*,
0N/Aorg.opensolaris.opengrok.web.*,
0N/Aorg.opensolaris.opengrok.search.context.*,
0N/Aorg.opensolaris.opengrok.configuration.*,
0N/Aorg.apache.lucene.spell.*,
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");
0N/AString hist = request.getParameter("hist");
0N/AString path = request.getParameter("path");
0N/A
0N/A%>
0N/A<%@ include file="projects.jspf" %>
0N/A<%
0N/A
0N/AHits hits = null;
0N/AString errorMsg = null;
0N/AString context = request.getContextPath();
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;
0N/Aif (project == null) project = null;
0N/A
0N/Aif (q != null || defs != null || refs != null || hist != null || path != null) {
0N/A Searcher searcher = null; //the searcher used to open/search the index
0N/A IndexReader ireader = null; //the reader used to open/search the index
0N/A Query query = null, defQuery = null; //the Query created by the QueryParser
0N/A
0N/A int start = 0; //the first index displayed on this page
0N/A int max = 25; //the maximum items displayed on this page
0N/A int thispage = 0; //used for the for/next either max or
0N/A String moreUrl = null;
0N/A CompatibleAnalyser analyzer = new CompatibleAnalyser();
0N/A String qstr = "";
0N/A String result = "";
0N/A try {
0N/A String DATA_ROOT = env.getDataRootPath();
0N/A if(DATA_ROOT.equals("")) {
0N/A throw new Exception("DATA_ROOT parameter is not configured in web.xml!");
0N/A }
0N/A File data_root = new File(DATA_ROOT);
0N/A if(!data_root.isDirectory()) {
0N/A throw new Exception("DATA_ROOT parameter in web.xml does not exist or is not a directory!");
0N/A }
0N/A ireader = IndexReader.open(DATA_ROOT + "/index");
0N/A searcher = new IndexSearcher(ireader);
0N/A //String date = request.getParameter("date");
0N/A try {
0N/A start = Integer.parseInt(request.getParameter("start")); //parse the max results first
0N/A max = Integer.parseInt(request.getParameter("n")); //then the start index
0N/A if(max < 0 || (max % 10 != 0) || max > 50) max = 25;
0N/A if(start < 0 ) start = 0;
0N/A } catch (Exception e) { }
0N/A
0N/A StringBuilder sb = new StringBuilder();
0N/A if (q != null) {
0N/A sb.append(q);
0N/A }
0N/A
0N/A if (defs != null) {
0N/A sb.append(" defs:(");
0N/A sb.append(defs);
0N/A sb.append(")");
0N/A }
0N/A
0N/A if (refs != null) {
0N/A sb.append(" refs:(");
0N/A sb.append(refs);
0N/A sb.append(")");
0N/A }
0N/A
0N/A if (path != null) {
0N/A sb.append(" path:(");
0N/A sb.append(path);
0N/A sb.append(")");
0N/A }
0N/A
0N/A if (hist != null) {
0N/A sb.append(" hist:(");
0N/A sb.append(hist);
0N/A sb.append(")");
0N/A }
0N/A
0N/A if (project != null) {
0N/A sb.append(" (");
0N/A
0N/A boolean first = true;
0N/A for (String s : project.split(" ")) {
0N/A if (first) {
0N/A first = false;
0N/A } else {
0N/A sb.append(" OR ");
0N/A }
0N/A sb.append("project:(");
0N/A sb.append(s);
0N/A sb.append(")");
0N/A }
0N/A
0N/A sb.append(")");
0N/A }
0N/A
0N/A qstr = sb.toString();
0N/A
0N/A QueryParser qparser = new QueryParser("full", analyzer);
0N/A qparser.setOperator(QueryParser.DEFAULT_OPERATOR_AND);
0N/A query = qparser.parse(qstr); //parse the
0N/A hits = searcher.search(query);
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) {
0N/A errorMsg = "<b>Error:</b><br/>" + Util.Htmlize(qstr) + "<br/>" + Util.Htmlize(e.getMessage());
0N/A } catch (FileNotFoundException e) {
0N/A errorMsg = "<b>Error:</b> Index database not found";
0N/A } catch (Exception e) {
0N/A errorMsg = "<b>Error:</b> " + Util.Htmlize(e.getMessage());
0N/A }
0N/A if (hits != null && hits.length() == 1 && request.getServletPath().equals("/s")) {
0N/A if (query != null && query instanceof TermQuery) {
0N/A response.sendRedirect(context + "/xref" + hits.doc(0).get("path")
0N/A + "#" + ((TermQuery)query).getTerm().text());
0N/A } else {
0N/A response.sendRedirect(context + "/xref" + hits.doc(0).get("path"));
0N/A }
0N/A } else {
0N/A %><?xml version="1.0" encoding="iso-8859-1"?>
0N/A<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
0N/A<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
0N/A<head>
0N/A <meta name="robots" content="noindex,nofollow">
0N/A <link rel="icon" href="img/icon.png" type="image/png"/>
0N/A <link rel="stylesheet" type="text/css" href="style.css"/>
0N/A <link rel="stylesheet" type="text/css" href="print.css" media="print" />
0N/A <link rel="alternate stylesheet" type="text/css" media="all" title="Paper White" href="print.css"/>
0N/A <title>Search</title>
0N/A</head>
0N/A<body>
0N/A<div id="page">
0N/A <div id="header">
0N/A <%= getServletContext().getInitParameter("HEADER") %>
0N/A </div>
0N/A<div id="Masthead"></div>
0N/A<div id="bar">
0N/A <%@ include file="menu.jspf"%>
0N/A<!-- table cellpadding="0" cellspacing="0" border="0" width="100%">
0N/A <tr>
0N/A <td valign="top"><br /> &nbsp;</td>
0N/A <td align="left" valign="middle">
0N/A <br/><form action="search" name="sbox">
0N/A <table cellpadding="2" border="0" cellspacing="0">
0N/A <tr valign="top">
0N/A <td>
0N/A <table cellpadding="2" border="0" cellspacing="0">
0N/A <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>
0N/A <tr><td align="right"> Definition </td><td><input class="q" name="defs" size="25" style="width: 300px" value="<%=Util.formQuoteEscape(defs)%>"/></td></tr>
0N/A <tr><td align="right"> Symbol </td><td><input class="q" name="refs" size="25" style="width: 300px" value="<%=Util.formQuoteEscape(refs)%>"/></td></tr>
0N/A <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>
0N/A <tr><td align="right"> History </td><td><input class="q" name="hist" size="25" style="width: 300px" value="<%=Util.formQuoteEscape(hist)%>"/></td></tr>
0N/A </table>
0N/A </td>
0N/A <% if (hasProjects) { %>
0N/A <td>
0N/A Project:<br>
0N/A <select class="q" name="project" size="6" multiple="true" style="width: 300px">
0N/A <% for (Project p : env.getProjects()) {
0N/A %><option value="<%=Util.formQuoteEscape(p.getPath())%>"<%=p.getPath().equals(project) ? " selected" : ""%>><%=Util.formQuoteEscape(p.getDescription())%></option><%
0N/A } %>
0N/A </select>
0N/A </td>
0N/A <% } %>
0N/A </tr>
0N/A <tr><td></td><td> &nbsp; <input class="submit" type="submit" value="Search"/> | <input class="submit"
0N/A 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 "
0N/A /> | <a href="help.html">Help</a></td></tr>
0N/A </table></form>
0N/A </td>
0N/A <td valign="top" align="right"></td></tr>
0N/A</table -->
0N/A</div>
0N/A<div id="results">
0N/A<%
0N/Aif( hits == null || errorMsg != null) {
0N/A %><%=errorMsg%><%
0N/A } else if (hits.length() == 0) {
0N/A
0N/A String ngramIndex = env.getDataRootPath() + "/spellIndex";
0N/A if (ngramIndex != null && (new
0N/A File(ngramIndex+"/segments")).exists()) {
0N/A Date sstart = new Date();
0N/A IndexReader spellReader = IndexReader.open(ngramIndex);
0N/A IndexSearcher spellSearcher = new IndexSearcher(spellReader);
0N/A
0N/A %><p><font color="#cc0000">Did you mean</font>:<%
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 = NGramSpeller.suggestUsingNGrams(spellSearcher,toks[j].toLowerCase(), 3, 4, 3, 5, 3, 2, 0, null, false);
0N/A for(int i = 0;i < ret.length; i++) {
0N/A %> <a href=search?q=<%=ret[i]%>><%=ret[i]%></a> &nbsp; <%
0N/A }
0N/A }
0N/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) {
0N/A String[] ret = NGramSpeller.suggestUsingNGrams(spellSearcher,toks[j].toLowerCase(),3, 4, 3, 5, 3, 2, 0, null, false);
0N/A for(int i = 0;i < ret.length; i++) {
0N/A %> <a href=search?q=<%=ret[i]%>><%=ret[i]%></a> &nbsp; <%
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A if(defs != null) {
0N/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 = NGramSpeller.suggestUsingNGrams(spellSearcher,toks[j].toLowerCase(),3, 4, 3, 5, 3, 2, 0, null, false);
0N/A for(int i = 0;i < ret.length; i++) {
0N/A %> <a href=search?q=<%=ret[i]%>><%=ret[i]%></a> &nbsp; <%
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A spellSearcher.close();
0N/A spellReader.close();
0N/A %></p><%
0N/A }
0N/A %><p> Your search <b><%=query.toString()%></b> did not match any files.
0N/A <br />
0N/A Suggestions:<br/><blockquote>- Make sure all terms are spelled correctly.<br/>
0N/A - Try different keywords.<br/>
0N/A - Try more general keywords.<br/>
0N/A - Use 'wil*' cards if you are looking for partial match.
0N/A </blockquote>
0N/A </p><%
0N/A } else { // We have a lots of results to show
0N/A StringBuilder slider = null;
0N/A if ( max < hits.length()) {
0N/A if((start + max) < hits.length()) {
0N/A thispage = max;
0N/A } else {
0N/A thispage = hits.length() - start;
0N/A }
0N/A String 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
0N/A slider = new StringBuilder();
0N/A int labelStart =1;
0N/A int sstart = start - max* (start / max % 10 + 1) ;
0N/A if(sstart < 0) {
0N/A sstart = 0;
0N/A labelStart = 1;
0N/A } else {
0N/A labelStart = sstart/max + 1;
0N/A }
0N/A int label = labelStart;
0N/A int labelEnd = label + 11;
0N/A String arr;
0N/A for(int i=sstart; i<hits.length() && label <= labelEnd; i+= max) {
0N/A if (i <= start && start < i+ max) {
0N/A slider.append("<span class=\"sel\">" + label + "</span>");
0N/A } else {
0N/A if(label == labelStart && label != 1) {
0N/A arr = "&lt;&lt";
0N/A } else if(label == labelEnd && i < hits.length()) {
0N/A arr = "&gt;&gt;";
0N/A } else {
0N/A arr = label < 10 ? " " + label : String.valueOf(label);
0N/A }
0N/A slider.append("<a class=\"more\" href=\"search?n=" + max + "&amp;start=" + i + url + "\">"+
0N/A arr + "</a>");
0N/A }
0N/A label++;
0N/A }
0N/A } else {
0N/A thispage = hits.length() - start; // set the max index to max or last
0N/A }
0N/A %>&nbsp; &nbsp; Searched <b><%=query.toString()%></b> (Results <b><%=start+1%> -
0N/A <%=thispage+start%></b> of <b><%=hits.length()%></b>) <p><%=slider != null ?
0N/A slider.toString(): ""%></p>
0N/A <table width="100%" cellpadding="3" cellspacing="0" border="0"><%
0N/A
0N/A Context sourceContext = null;
0N/A Summarizer summer = null;
0N/A if (query != null) {
0N/A try{
0N/A sourceContext = new Context(query);
0N/A if(sourceContext != null)
0N/A summer = new Summarizer(query, analyzer);
0N/A } catch (Exception e) {
0N/A
0N/A }
0N/A }
0N/A
0N/A HistoryContext historyContext = null;
0N/A try {
0N/A historyContext = new HistoryContext(query);
0N/A } catch (Exception e) {
0N/A }
0N/A EftarFileReader ef = null;
0N/A try{
0N/A ef = new EftarFileReader(env.getDataRootPath() + "/index/dtags.eftar");
0N/A } catch (Exception e) {
0N/A }
0N/A Results.prettyPrintHTML(hits, start, start+thispage,
0N/A out,
0N/A sourceContext, historyContext, summer,
0N/A context + "/xref",
0N/A context + "/more",
0N/A env.getSourceRootPath(),
0N/A env.getDataRootPath(),
0N/A ef);
0N/A if(ef != null) {
0N/A try{
0N/A ef.close();
0N/A } catch (IOException e) {
0N/A }
0N/A }
0N/A %></table><br/>
0N/A <b> Completed in <%=(new Date()).getTime() - starttime.getTime()%> milliseconds </b> <br/>
0N/A <%=slider != null ? "<p>" + slider + "</p>" : ""%>
0N/A <%
0N/A }
0N/A %><br/></div><%@include file="foot.jsp"%><%
0N/A }
0N/A if (ireader != null)
0N/A ireader.close();
0N/A} else { // Entry page show the map
0N/A response.sendRedirect(context + "/index.jsp");
0N/A}
0N/A%>
0N/A