你好,游客 登录 注册 搜索
背景:
阅读新闻

Lucene入门基础教程

保持数据库与索引库的同步

[日期:2014-06-08] 来源:Linux社区  作者:xinxinqiu [字体:大 中 小]

保持数据库与索引库的同步

说明:在一个系统中,如果索引功能存在,那么数据库和索引库应该是同时存在的。这个时候需要保证索引库的数据和数据库中的数据保持一致性。可以在对数据库进行增、删、改操作的同时对索引库也进行相应的操作。这样就可以保证数据库与索引库的一致性。

工具类DocumentUtils

public class DocumentUtils {
 public static Document article2Document(Article article){
  Document document = new Document();
  Field idField = new Field("id",article.getId().toString(),Store.YES,Index.NOT_ANALYZED);
  Field titleField = new Field("title",article.getTitle(),Store.YES,Index.ANALYZED);
  Field contentField = new Field("content",article.getContent(),Store.YES,Index.ANALYZED);
  document.add(idField);
  document.add(titleField);
  document.add(contentField);
  return document;
 }
 
 public static Article document2Article(Document document){
  Article article = new Article();
  article.setId(Long.parseLong(document.get("id")));
  article.setTitle(document.get("title"));
  article.setContent(document.get("content"));
  return article;
 }
}

 

public class LuceneUtils {
 public static Directory directory;
 public static Analyzer analyzer;
 static{
  try{
   directory = FSDirectory.open(new File("./indexDir"));
   analyzer = new StandardAnalyzer(Version.LUCENE_30);
  }catch(Exception e){
   e.printStackTrace();
  }
 }
}

public class ArticleIndex {
 @Test
 public void testCreateIndex() throws Exception{
  Article article = new  Article();
  article.setId(1L);
  article.setTitle("lucene可以做搜索引擎");
  article.setContent("baidu,google都是很好的搜索引擎");
 
  IndexWriter indexWriter = new IndexWriter(LuceneUtils.directory,LuceneUtils.analyzer,MaxFieldLength.LIMITED);
  indexWriter.addDocument(DocumentUtils.article2Document(article));
  indexWriter.close();
 }
 
 @Test
 public void testSearchIndex() throws Exception{
  IndexSearcher indexSearcher = new IndexSearcher(LuceneUtils.directory);
  QueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_30, new String[]{"title","content"}, LuceneUtils.analyzer);
  Query query = queryParser.parse("baidu");
  TopDocs topDocs = indexSearcher.search(query, 2);
  ScoreDoc[] scoreDocs = topDocs.scoreDocs;
  List<Article> articleList = new ArrayList<Article>();
  for(ScoreDoc scoreDoc:scoreDocs){
   Document document =  indexSearcher.doc(scoreDoc.doc);
   Article article = DocumentUtils.document2Article(document);
   articleList.add(article);
  }
 
  for(Article article:articleList){
   System.out.println(article.getId());
   System.out.println(article.getTitle());
   System.out.println(article.getContent());
  }
 }
 
 /**
  * 一般情况下索引库的删除用关键词
  * @throws Exception
  */
 @Test
 public void testDeleteIndex() throws Exception{
  IndexWriter indexWriter = new IndexWriter(LuceneUtils.directory,LuceneUtils.analyzer,MaxFieldLength.LIMITED);
  //indexWriter.deleteAll()删除所有的索引值
  /**
  * term就为关键词对象
  */
  Term term = new Term("title", "lucene");
  indexWriter.deleteDocuments(term);
  indexWriter.close();
 }
 
 /**
  * 修改
  *    先删除后增加
  */
 @Test
 public void testUpdateIndex() throws Exception{
  IndexWriter indexWriter = new IndexWriter(LuceneUtils.directory,LuceneUtils.analyzer,MaxFieldLength.LIMITED);
  Term term = new Term("title", "lucene");
  Article article = new  Article();
  article.setId(1L);
  article.setTitle("lucene可以做搜索引擎");
  article.setContent("修改后的内容");
  /**
  * term是用删除的
  * document是用于增加的
  */
  indexWriter.updateDocument(term, DocumentUtils.article2Document(article));
  indexWriter.close();
 }
}

 public void create () throws CorruptIndexException, LockObtainFailedException, IOException{
  Article article = new  Article();
  article.setId(1L);
  article.setTitle("lucene可以做搜索引擎");
  article.setContent("baidu,google都是很好的搜索引擎");
  IndexWriter indexWriter=new IndexWriter(LuceneUtils.directory, LuceneUtils.analyzer, MaxFieldLength.LIMITED);
  indexWriter.addDocument(DocumentUtils.article2Document(article));
  indexWriter.close();
 }

 

@Test
 public void Query() throws CorruptIndexException, IOException,
   ParseException {
  IndexSearcher indexSearcher = new IndexSearcher(LuceneUtils.directory);
  QueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_30,
    new String[] { "title", "content" }, LuceneUtils.analyzer);

  TopDocs topDocs = indexSearcher.search(queryParser.parse("lucene"), 2);
  ScoreDoc[] scoreDocs = topDocs.scoreDocs;
  List<Article> articleList = new ArrayList<Article>();
  for (ScoreDoc scoreDoc : scoreDocs) {
   System.err.println("相关度为" + scoreDoc.score);
   Document document = indexSearcher.doc(scoreDoc.doc);
   Article article = DocumentUtils.document2Article(document);
   articleList.add(article);
  }
  for (Article article : articleList) {
   System.out.println(article.getId());
   System.out.println(article.getTitle());
   System.out.println(article.getContent());
  }
 }

 

@Test
 public void delete() throws CorruptIndexException, LockObtainFailedException, IOException{
  IndexWriter indexWriter=new IndexWriter(LuceneUtils.directory, LuceneUtils.analyzer, MaxFieldLength.LIMITED);
  // indexWriter.deleteAll()删除所有的索引值
    /**
    * term就为关键词对象
    */
  indexWriter.deleteDocuments(new Term("title","lucene"));
  indexWriter.close();
 
 }

 

/**
  *
  * 修改 先删除后增加
  * @throws IOException
  * @throws CorruptIndexException
  *
  */
 @Test
 public void update() throws CorruptIndexException, IOException {
  IndexWriter indexWriter = new IndexWriter(LuceneUtils.directory,
    LuceneUtils.analyzer, MaxFieldLength.LIMITED);
  Term term = new Term("title", "lucene");
  Article article = new Article();
  article.setId(1L);
  article.setTitle("lucene可以做搜索引擎");
  article.setContent("修改后的内容");
  /**
  * term是用删除的 document是用于增加的
  */
  indexWriter.updateDocument(term,
    DocumentUtils.article2Document(article));
  indexWriter.close();
 }

 

 

 

 

linux
【内容导航】
第1页:简介 第2页:准备Lucene的开发环境
第3页:保持数据库与索引库的同步 第4页:indexWriter
第5页:与索引库的交互 第6页:分词器
第7页:分页 第8页:搜索方式
第9页:排序+高亮 第10页:常用API
相关资讯       Lucene  Lucene教程  Lucene简介 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款