view src/global/web/tools/Search.jtp @ 39:b5d56f522ea3

error handling
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 11 Jul 2020 21:39:52 -0600
parents 7ecd1a4ef557
children
line wrap: on
line source

<%
package global.web.tools;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.util.Version;
import nabble.view.lib.Jtp;
import global.Site;


public final class Search extends HttpServlet {
	private static final Logger logger = LoggerFactory.getLogger(Search.class);

	protected void service(HttpServletRequest request,HttpServletResponse response)
		throws IOException
	{
		PrintWriter out = response.getWriter();
		String query = request.getParameter("query");
		IndexSearcher searcher;
		Query q;
		TopDocs hits;
		try {
			searcher = new IndexSearcher(Site.dir());
			QueryParser parser = new QueryParser(Version.LUCENE_CURRENT,Site.SUBJECT_FLD,Site.analyzer);
			parser.setDefaultOperator(QueryParser.AND_OPERATOR);
			q = parser.parse(query);
			hits = searcher.search(q,100);
		} catch(IOException e) {
			throw new RuntimeException(e);
		} catch(ParseException e) {
			throw new RuntimeException(e);
		}
		try {
			%>
			<html>
				<head>
					<title>Nabble Search Result</title>
				</head>
				<body>
					<h1>Nabble Search for: <%=q%></h1>
					<h3>showing <%=hits.scoreDocs.length%> out of <%=hits.totalHits%> results</h3>
					<%
					for( ScoreDoc sd : hits.scoreDocs ) {
						Site site = new Site( searcher.doc(sd.doc) );
						%>
						<p><%=site.link()%></p>
						<%
					}
					%>
				</body>
			</html>
			<%
		} finally {
			searcher.close();
		}
	}

}
%>