Mercurial Hosting > nabble
view src/global/web/tools/Search.java @ 0:7ecd1a4ef557
add content
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 21 Mar 2019 19:15:52 -0600 |
parents | |
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 { out.print( "\r\n<html>\r\n <head>\r\n <title>Nabble Search Result</title>\r\n </head>\r\n <body>\r\n <h1>Nabble Search for: " ); out.print( (q) ); out.print( "</h1>\r\n <h3>showing " ); out.print( (hits.scoreDocs.length) ); out.print( " out of " ); out.print( (hits.totalHits) ); out.print( " results</h3>\r\n " ); for( ScoreDoc sd : hits.scoreDocs ) { Site site = new Site( searcher.doc(sd.doc) ); out.print( "\r\n<p>" ); out.print( (site.link()) ); out.print( "</p>\r\n" ); } out.print( "\r\n</body>\r\n</html>\r\n" ); } finally { searcher.close(); } } }