view src/global/web/tools/Search.java @ 40:f770c4076954

add install doc
author Vadim Filimonov <fffilimonov@yandex.ru>
date Tue, 14 Jul 2020 22:06:41 +0300
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 {
			
		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();
		}
	}

}