U E D R , A S I H C RSS

JSP/Search Agency

~java

<%@ page language="java" 

import="java.util.*, java.io.BufferedReader, java.io.InputStreamReader, java.io.FileReader,
		java.io.IOException, java.util.Date, 
		org.apache.lucene.analysis.Analyzer,
		org.apache.lucene.analysis.standard.StandardAnalyzer,
		org.apache.lucene.document.Document,
		org.apache.lucene.index.IndexReader,
		org.apache.lucene.index.FilterIndexReader,
		org.apache.lucene.search.Searcher,
		org.apache.lucene.search.IndexSearcher,
		org.apache.lucene.search.Query,
		org.apache.lucene.search.Hits,
		org.apache.lucene.queryParser.QueryParser
		"
pageEncoding="UTF-8"%>

<%

	out.write("<form method=post action=SearchAgency.jsp>");
	out.write("  검색할 키워드를 입력하세요 <br>");
	out.write("  <input type=text name='keyword'>");
	out.write("  </form>");
	class OneNormsReader extends FilterIndexReader {
    private String field;

    public OneNormsReader(IndexReader in, String field) {
      super(in);
      this.field = field;
    }

    public byte[] norms(String field) throws IOException {
      return in.norms(this.field);
    }
  }

//    String index = "/home/httpd/index";
    String index = "index";
    String field = "contents";
    String queries = null;
    int repeat = 0;
    boolean raw = false;
    String normsField = null;
 
    IndexReader reader = IndexReader.open(index);

    if (normsField != null)
      reader = new OneNormsReader(reader, normsField);

    Searcher searcher = new IndexSearcher(reader);
    Analyzer analyzer = new StandardAnalyzer();

    BufferedReader in = null;
    if (queries != null) {
      in = new BufferedReader(new FileReader(queries));
    } else {
      in = new BufferedReader(new InputStreamReader(System.in));
    }



      System.out.print("Query: ");
      request.setCharacterEncoding("UTF-8");
      String line = request.getParameter("keyword");
      if(line!=null)
      {
		  out.println(line);
	
	      Query query = QueryParser.parse(line+"*", field, analyzer);
		  out.println("Searching for: " + query.toString(field));
	
	      Hits hits = searcher.search(query);
	      
	      if (repeat > 0) {                           // repeat & time as benchmark
	        Date start = new Date();
	        for (int i = 0; i < repeat; i++) {
	          hits = searcher.search(query);
	        }
	        Date end = new Date();
	        out.println("Time: "+(end.getTime()-start.getTime())+"ms");
	      }
	
	      out.println(hits.length() + " total matching documents");
	
	      final int HITS_PER_PAGE = 10;
	      for (int start = 0; start < hits.length(); start += HITS_PER_PAGE) {
	        int end = Math.min(hits.length(), start + HITS_PER_PAGE);
	        for (int i = start; i < end; i++) {
	
	          if (raw) {                              // output raw format
	            out.println("doc="+hits.id(i)+" score="+hits.score(i));
	            continue;
	          }
	
	          Document doc = hits.doc(i);
	          String path = doc.get("path");
	          if (path != null) {
	            out.println((i+1) + ". " + path);
	            String title = doc.get("title");
	            if (title != null) {
	              out.println("   Title: " + doc.get("title"));
	            }
	          } else {
	            out.println((i+1) + ". " + "No path for this document");
	          }
	        }
	
	        if (queries != null)                      // non-interactive
	          break;
	        
	        if (hits.length() > end) {
	          out.print("more (y/n) ? ");
	          line = in.readLine();
	          if (line.length() == 0 || line.charAt(0) == 'n')
	            break;
	        }
	      }
	    reader.close();
	}

String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'SearchAgency.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  
  <body>
    This is my JSP page.aaa <br>
  </body>
</html>
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:28
Processing time 0.0072 sec