Mercurial Hosting > nabble
diff src/global/web/tools/Search.jtp @ 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 diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/global/web/tools/Search.jtp Thu Mar 21 19:15:52 2019 -0600 @@ -0,0 +1,70 @@ +<% +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(); + } + } + +} +%>