Luncene 之二查询

新建一个search.jsp 页面

<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>搜索结果</title>

<mce:style type="text/css"><!--

body
{
margin-top:50px;
margin-left:50px;

}

.search
{
text-align:center;
border: 1px solid #3366CC;
width:{bfb};
height:40px;
margin-bottom:20px;


}

.input_text
{
width:500px;
height:20px;
margin:9px 0 0 0;
}

.title
{
font-size:25px;
color:#000;
font-weight:bold;
margin-bottom:20px;
text-decoration:underline;

}

.content
{
font-size:23px;
color:#999999;
margin-bottom:35px;

}
--></mce:style><style type="text/css" mce_bogus="1">
body
{
margin-top:50px;
margin-left:50px;

}

.search
{
text-align:center;
border: 1px solid #3366CC;
width:{bfb};
height:40px;
margin-bottom:20px;


}

.input_text
{
width:500px;
height:20px;
margin:9px 0 0 0;
}

.title
{
font-size:25px;
color:#000;
font-weight:bold;
margin-bottom:20px;
text-decoration:underline;

}

.content
{
font-size:23px;
color:#999999;
margin-bottom:35px;

}</style>
</head>

<body>

   <div class="search">
   
    <form action="searchServlet" method="post">
    
    
     <input type="text" name="keyword" class="input_text"/>
     <input type="submit" value=" 搜 索 " />
    </form>
   
   </div>
<hr/>
<c:forEach items="${list}" var="n">

<div class="title">
   ${n.title }
</div>
<div class="content">${n.content }</div>
</c:forEach>
</body>
</html>

再util 包下新建一个LunceneUtil文件

package com.lunceneTest.util;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import net.paoding.analysis.analyzer.PaodingAnalyzer;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.search.highlight.Fragmenter;
import org.apache.lucene.search.highlight.Highlighter;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.SimpleFragmenter;
import org.apache.lucene.search.highlight.SimpleHTMLFormatter;
import org.apache.lucene.store.FSDirectory;

import com.lunceneTest.Model.News;

public class LunceneUtil
{


public static List<News> search(String path,String keyword)
{
  
   List<News> list = new ArrayList<News>();
  
   IndexReader reader=null;
   try {
   
    FSDirectory directory = FSDirectory.getDirectory(path);
   
    reader = IndexReader.open(directory);
   
    Searcher searcher = new IndexSearcher(directory);
   
   
    //要查询的字段
    String [] fields = {News.TITLE,News.CONTENT};
    //要查询字段之间的关系(与或非)
   
    BooleanClause.Occur [] flags = new BooleanClause.Occur[]{
      BooleanClause.Occur.SHOULD,BooleanClause.Occur.SHOULD
     
    };
   
    // 分词时用的分词器
    Analyzer analyzer = new PaodingAnalyzer();
   
    Query query =MultiFieldQueryParser.parse(keyword, fields, flags, analyzer);
   
    // 由于我们目前使用的查询是多字段查询,需要匹配度的排序
    // QueryScorer内置计分器
   
    query.rewrite(reader);//用于重写query对象,目的能过让计费器识别当前的query;
   
    Hits hits = searcher.search(query);
   
    int length = hits.length();
   
    for(int i=0; i < length; i++)
    {
     Document doc = hits.doc(i);
    
     News n = new News();
    
     int id =Integer.valueOf(doc.get(News.ID));
     String title = doc.get(News.TITLE); //查询关键字高亮
     System.out.println("tt = " + title);
     String content = doc.get(News.CONTENT);
    
     SimpleHTMLFormatter format = new SimpleHTMLFormatter("<font color=red>","</font>");
    
     //高亮 ,计分器
     Highlighter highlighter = new Highlighter(format,new QueryScorer(query));
    
     //关键字附近字符串的截取,截取200个字符
    
     Fragmenter fragmenter = new SimpleFragmenter(150);
    
     highlighter.setTextFragmenter(fragmenter);
    
     //针对某个关键字的高亮以及截取
     /*TokenStream title_tokenStream = analyzer.tokenStream(News.TITLE, new StringReader(title));
     String h_title = highlighter.getBestFragment(title_tokenStream, title);*/
    
     TokenStream content_tokenStream = analyzer.tokenStream(News.CONTENT, new StringReader(content));
     content = highlighter.getBestFragment(content_tokenStream, content);
    
    
     n.setId(id);
     n.setTitle(title);
     n.setContent(content);
    
     list.add(n);
    
    
    }
   
   
   } catch (Exception e) {
    // TODO: handle exception
   }
   finally
   {
    try {
     reader.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
   return list;
  
}

}

新建一个用来查询的Servlet : searchServlet

package com.lunceneTest.servlet;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lunceneTest.Model.News;
import com.lunceneTest.util.LunceneUtil;

public class SearchServlet extends HttpServlet {

/**
*
*/
private static final long serialVersionUID = -7697674920907944387L;

public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

  
   request.setCharacterEncoding("utf-8");
  
   String keyword = request.getParameter("keyword");
  
   String path = URLDecoder.decode(SearchServlet.class.getResource("/data/index").toString(), "UTF-8").replace("file:/", "");
  
   List<News> list=LunceneUtil.search(path, keyword);
  
   request.setAttribute("list",list);
  
   request.getRequestDispatcher("/search.jsp").forward(request, response);
  
  
  
}

}

运行项目,在文本框中输入搜索关键词 ,例如“爱 情 输赢 慢慢”,运行结果如下:



郑重声明:资讯 【Luncene 之二查询】由 发布,版权归原作者及其所在单位,其原创性以及文中陈述文字和内容未经(企业库qiyeku.com)证实,请读者仅作参考,并请自行核实相关内容。若本文有侵犯到您的版权, 请你提供相关证明及申请并与我们联系(qiyeku # qq.com)或【在线投诉】,我们审核后将会尽快处理。
—— 相关资讯 ——