lucene实例练习

package cn.itcast.hello;

public class Aritcle {
 private Integer id;
 private String title;
 private String content;
 public Integer getId() {
  return id;
 }
 public void setId(Integer id) {
  this.id = id;
 }
 public String getTitle() {
  return title;
 }
 public void setTitle(String title) {
  this.title = title;
 }
 public String getContent() {
  return content;
 }
 public void setContent(String content) {
  this.content = content;
 }

 
}
-----------------------------------

package cn.itcast.hello;

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

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test;

public class HelloWorld {
 
 //建立索引
 @Test
 public void testCreateIndex() throws Exception{
  //1,模拟一条已保存的信息
  Aritcle artcle = new Aritcle();
  artcle.setId(1);
  artcle.setTitle("luncene工具说明。。。");
  artcle.setContent("内容备注,内容备注,内容备注。。。。。");
  
  //为文章建立索引库
  
  //5.创建文档目录
  Directory directory = FSDirectory.open(new File("./indexDri/"));
  Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
  
  //3索引库需要添加文档,所以先将aritcle转换成文档,再添加文档到索引库中
  Document document = new Document();
  document.add(new Field("id", artcle.getId().toString(), Store.YES, Index.NOT_ANALYZED));
  document.add(new Field("title", artcle.getTitle(), Store.YES, Index.ANALYZED));
  document.add(new Field("content", artcle.getContent(), Store.YES, Index.ANALYZED));
  
  
  //4.索引库需要文档目录
  IndexWriter indexWriter = new IndexWriter(directory, analyzer, new MaxFieldLength(10000));
  indexWriter.addDocument(document);
  indexWriter.close();
  
 }
 

 //搜索
 @Test
 public void testSearcher() throws IOException, ParseException{
  //2,搜索条件
  //String queryString ="luncene";
  String queryString ="hibernate";

  
  Directory directory = FSDirectory.open(new File("./indexDri/"));
  Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
  
  //把查询字符串转换为query对象
  QueryParser queryParser = new QueryParser(Version.LUCENE_30, "title", analyzer);
  org.apache.lucene.search.Query query = queryParser.parse(queryString);
  
  //建立搜索查询,执行查询结果
  IndexSearcher insh = new IndexSearcher(directory);
  TopDocs topDocs = insh.search(query, 100);//需要query对象查询
  
  //匹配到结果数量,结果信息
  int count = topDocs.totalHits;
  ScoreDoc [] scoreDocs = topDocs.scoreDocs;
  
  //处理结果
  List<Aritcle> list = new ArrayList<Aritcle>();
  for(int i=0;i<scoreDocs.length;i++){
   ScoreDoc sd = scoreDocs[i];
   float score = sd.score;//争对相关度搜索条件得分
   int docId = sd.doc;
   Document doc = insh.doc(docId);
   Aritcle aritcle = new Aritcle();
   aritcle.setId(Integer.parseInt(doc.get("id")));
   aritcle.setTitle(doc.get("title"));
   aritcle.setContent(doc.get("content"));
   list.add(aritcle);
  }
  insh.close();
  System.out.println("搜索结果记录数= "+list.size());
  for(Aritcle aritcle:list){
   System.out.println("id:"+aritcle.getId());
   System.out.println("title:"+aritcle.getTitle());
   System.out.println("content:"+aritcle.getContent());
  }
  
 }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值