Lucene创建索引入门案例
生活随笔
收集整理的這篇文章主要介紹了
Lucene创建索引入门案例
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
最近在學習lucene,參考網(wǎng)上的資料寫了一個簡單搜索demo;
項目jar包:
//索引關鍵類
<pre name="code" class="java">package com.lucene.index;import java.io.File; import java.io.IOException; import java.io.StringReader; import java.util.ArrayList; import java.util.List;import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.tokenattributes.CharTermAttribute; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.CorruptIndexException; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.queryParser.ParseException; import org.apache.lucene.queryParser.QueryParser; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.TopDocs; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.store.LockObtainFailedException; import org.apache.lucene.util.Version; import org.wltea.analyzer.lucene.IKAnalyzer;import com.lucene.vo.User;/*** ?* lucene 檢索內(nèi)存索引 非常簡單的例子 ?* ?* @author Administrator ?* ?*/ public class searchIndex {private String[] ids = { "1", "2", "3", "4", "5", "6" };private String[] emails = { "aa@itat.org", "bb@itat.org", "cc@cc.org", "dd@sina.org", "ee@zttc.edu", "ff@itat.org" }; // private String[] contents = { "welcome to visited the space,I like book", "hello boy, I like pingpeng ball", "my name is cc I like game", "I like football", // "I like football and I like basketball too", "I like movie and swim" };private String[] contents = { "創(chuàng)建一個內(nèi)存目錄對象,所以這里生成的索引會放在磁盤中,而不是在內(nèi)存中", "創(chuàng)建索引寫入對象,該對象既可以把索引寫入到磁盤中也可以寫入到內(nèi)存中", "分詞器,分詞器就是將檢索的關鍵字分割成一組組詞組, 它是lucene檢索查詢的一大特色之一", "這個是分詞器拆分最大長度,因為各種不同類型的分詞器拆分的字符顆粒細化程度不一樣,所以需要設置一個最長的拆分長度","文檔對象,在lucene中創(chuàng)建的索引可以看成數(shù)據(jù)庫中的一張表,表中也可以有字段,往里面添加內(nèi)容之后可以根據(jù)字段去匹配查詢", "I like movie and swim" };private String[] names = { "zhangsan", "lisi", "john", "jetty", "mike", "jake" };// 創(chuàng)建一個內(nèi)存目錄對象,所以這里生成的索引會放在磁盤中,而不是在內(nèi)存中。private Directory directory = null;//IK分詞器IKAnalyzer analyzer = null;public searchIndex() {try {directory = FSDirectory.open(new File("H:/lucene/index"));analyzer = new IKAnalyzer(true);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public void index() {/** 創(chuàng)建索引寫入對象,該對象既可以把索引寫入到磁盤中也可以寫入到內(nèi)存中。*/IndexWriter writer;try {writer = new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_36, analyzer));//創(chuàng)建之前先刪除writer.deleteAll();// 創(chuàng)建Document// 文檔對象,在lucene中創(chuàng)建的索引可以看成數(shù)據(jù)庫中的一張表,表中也可以有字段,往里面添加內(nèi)容之后可以根據(jù)字段去匹配查詢Document doc =null;for(int i=0;i<ids.length;i++){doc = new Document();doc.add(new Field("id", ids[i], Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));doc.add(new Field("email", emails[i], Field.Store.YES, Field.Index.NOT_ANALYZED));doc.add(new Field("content", contents[i], Field.Store.NO, Field.Index.ANALYZED));doc.add(new Field("name", names[i], Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));writer.addDocument(doc);}writer.close(); } catch (CorruptIndexException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (LockObtainFailedException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public List<User> search(String keyword) {long startTime = System.currentTimeMillis();System.out.println("*****************檢索開始**********************");List<User> userList = new ArrayList<User>();IndexReader reader;try {reader = IndexReader.open(directory);// 創(chuàng)建IndexSearcher 檢索索引的對象,里面要傳遞上面寫入的內(nèi)存目錄對象directoryIndexSearcher searcher = new IndexSearcher(reader);// 根據(jù)搜索關鍵字 封裝一個term組合對象,然后封裝成Query查詢對象QueryParser queryParser = new QueryParser(Version.LUCENE_36, "content", analyzer);Query query = queryParser.parse(keyword);// 去索引目錄中查詢,返回的是TopDocs對象,里面存放的就是上面放的document文檔對象TopDocs rs = searcher.search(query, null, 10);long endTime = System.currentTimeMillis();System.out.println("總共花費" + (endTime - startTime) + "毫秒,檢索到" + rs.totalHits + "條記錄。");User user = null;for (int i = 0; i < rs.scoreDocs.length; i++) {// rs.scoreDocs[i].doc 是獲取索引中的標志位id, 從0開始記錄Document firstHit = searcher.doc(rs.scoreDocs[i].doc);user = new User();user.setId(Long.parseLong(firstHit.get("id")));user.setName(firstHit.get("name"));user.setSex(firstHit.get("sex"));user.setDosomething(firstHit.get("dosometing"));user.setEmail(firstHit.get("email"));user.setContent(firstHit.get("content"));userList.add(user);// System.out.println("name:" + firstHit.get("name")); // System.out.println("sex:" + firstHit.get("sex")); // System.out.println("dosomething:" + firstHit.get("dosometing"));}reader.close();} catch (CorruptIndexException e1) {// TODO Auto-generated catch blocke1.printStackTrace();} catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace();} catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println("*****************檢索結束**********************");return userList;}}package com.lucene;import java.io.IOException; 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.lucene.index.searchIndex; import com.lucene.vo.User;/*** Servlet implementation class searchServlet*/ public class searchServlet extends HttpServlet {private static final long serialVersionUID = 1L;/*** Default constructor. */public searchServlet() {// TODO Auto-generated constructor stub}/*** @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)*/protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stub}/*** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)*/protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {request.setCharacterEncoding("UTF-8");String keyword = request.getParameter("keyword");if("".equals(keyword)){keyword="0";}searchIndex si = new searchIndex();si.index();List<User> userList = si.search(keyword);request.setAttribute("userList", userList);request.getRequestDispatcher("search.jsp").forward(request, response);}}
package com.lucene.vo;public class User {private Long id;private String name;private String sex;private String dosomething;private String email;private String content;public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public String getDosomething() {return dosomething;}public void setDosomething(String dosomething) {this.dosomething = dosomething;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public String getContent() {return content;}public void setContent(String content) {this.content = content;}}
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>lucene 全文檢索</title> </head> <body style="text-align: center;"><form action="searchServlet.do" method="post"><input type="text" name="keyword" /> <input type="submit" value="搜索" /></form><div style="height: 10px"></div><c:if test="${not empty userList}"><div>相關信息:</div><table border="1" align="center"><tr><td>ID</td><td>姓名</td><td>性別</td><td>郵箱</td><td>愛好</td><td>正文</td></tr><c:forEach items="${ userList}" var="user"><tr><td>${user.id }</td><td>${user.name }</td><td>${user.sex }</td><td>${user.email }</td><td>${user.dosomething }</td><td>${user.content }</td></tr></c:forEach></table></c:if> </body> </html>
代碼測試:
總結
以上是生活随笔為你收集整理的Lucene创建索引入门案例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第五人格调香师的技能(《第五人格》官方网
- 下一篇: 大屏幕拼接方案(大上海保卫战)