0
|
1
|
|
2 package global.web.tools;
|
|
3
|
|
4 import java.io.IOException;
|
|
5 import java.io.PrintWriter;
|
|
6 import javax.servlet.http.HttpServlet;
|
|
7 import javax.servlet.http.HttpServletRequest;
|
|
8 import javax.servlet.http.HttpServletResponse;
|
|
9 import org.slf4j.Logger;
|
|
10 import org.slf4j.LoggerFactory;
|
|
11 import org.apache.lucene.queryParser.QueryParser;
|
|
12 import org.apache.lucene.queryParser.ParseException;
|
|
13 import org.apache.lucene.search.IndexSearcher;
|
|
14 import org.apache.lucene.search.Query;
|
|
15 import org.apache.lucene.search.TopDocs;
|
|
16 import org.apache.lucene.search.ScoreDoc;
|
|
17 import org.apache.lucene.util.Version;
|
|
18 import nabble.view.lib.Jtp;
|
|
19 import global.Site;
|
|
20
|
|
21
|
|
22 public final class Search extends HttpServlet {
|
|
23 private static final Logger logger = LoggerFactory.getLogger(Search.class);
|
|
24
|
|
25 protected void service(HttpServletRequest request,HttpServletResponse response)
|
|
26 throws IOException
|
|
27 {
|
|
28 PrintWriter out = response.getWriter();
|
|
29 String query = request.getParameter("query");
|
|
30 IndexSearcher searcher;
|
|
31 Query q;
|
|
32 TopDocs hits;
|
|
33 try {
|
|
34 searcher = new IndexSearcher(Site.dir());
|
|
35 QueryParser parser = new QueryParser(Version.LUCENE_CURRENT,Site.SUBJECT_FLD,Site.analyzer);
|
|
36 parser.setDefaultOperator(QueryParser.AND_OPERATOR);
|
|
37 q = parser.parse(query);
|
|
38 hits = searcher.search(q,100);
|
|
39 } catch(IOException e) {
|
|
40 throw new RuntimeException(e);
|
|
41 } catch(ParseException e) {
|
|
42 throw new RuntimeException(e);
|
|
43 }
|
|
44 try {
|
|
45
|
|
46 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: " );
|
|
47 out.print( (q) );
|
|
48 out.print( "</h1>\r\n <h3>showing " );
|
|
49 out.print( (hits.scoreDocs.length) );
|
|
50 out.print( " out of " );
|
|
51 out.print( (hits.totalHits) );
|
|
52 out.print( " results</h3>\r\n " );
|
|
53
|
|
54 for( ScoreDoc sd : hits.scoreDocs ) {
|
|
55 Site site = new Site( searcher.doc(sd.doc) );
|
|
56
|
|
57 out.print( "\r\n<p>" );
|
|
58 out.print( (site.link()) );
|
|
59 out.print( "</p>\r\n" );
|
|
60
|
|
61 }
|
|
62
|
|
63 out.print( "\r\n</body>\r\n</html>\r\n" );
|
|
64
|
|
65 } finally {
|
|
66 searcher.close();
|
|
67 }
|
|
68 }
|
|
69
|
|
70 }
|
|
71
|