1.

  1 package com.home.utils;
  2 
  3 import java.io.File;
  4 import java.util.ArrayList;
  5 import java.util.List;
  6 
  7 import org.apache.lucene.document.Document;
  8 import org.apache.lucene.index.IndexWriter;
  9 import org.apache.lucene.index.IndexWriter.MaxFieldLength;
 10 import org.apache.lucene.queryParser.MultiFieldQueryParser;
 11 import org.apache.lucene.queryParser.QueryParser;
 12 import org.apache.lucene.search.IndexSearcher;
 13 import org.apache.lucene.search.Query;
 14 import org.apache.lucene.search.ScoreDoc;
 15 import org.apache.lucene.search.TopDocs;
 16 import org.apache.lucene.store.Directory;
 17 import org.apache.lucene.store.FSDirectory;
 18 import org.apache.lucene.store.RAMDirectory;
 19 import org.apache.lucene.util.Version;
 20 import org.junit.Test;
 21 
 22 import com.sun.org.apache.xerces.internal.impl.dtd.models.DFAContentModel;
 23 
 24 /**
 25  * 1、能不能设置很多个索引库 可以设置很多个索引库 2、索引库能不能合并起来 如果是内存索引库 Directory ramDirectory = new
 26  * RamDirectory(Directory d); 这样就可以把一个索引库放入到内存索引库中
 27  * 利用IndexWriter.addIndexesNoOptimize方法可以把很多个索引库进行合并操作 3、应用程序能不能在内存中和索引库进行交互
 28  * 
 29  * @author Administrator
 30  * 
 31  */
 32 public class DirectoryTest {
 33 
 34     /**
 35      * 内存索引库 * 速度比较快 * 数据是暂时的 * 内存索引库和文件索引库的特点正好互补
 36      */
 37     @Test
 38     public void testRam() throws Exception {
 39 
 40         Directory directory = new RAMDirectory();
 41         IndexWriter indexWriter = new IndexWriter(directory,
 42                 LuceneUtils.analyzer, MaxFieldLength.LIMITED);
 43         Article article = new Article();
 44         article.setId(1L);
 45         article.setTitle("lucene可以做搜索引擎");
 46         article.setContent("baidu,google都是很好的搜索引擎");
 47         indexWriter.addDocument(DocumentUtils.article2Document(article));
 48         indexWriter.close();
 49         this.showData(directory);
 50     }
 51 
 52     private void showData(Directory directory) throws Exception {
 53 
 54         IndexSearcher indexSearcher = new IndexSearcher(directory);
 55         QueryParser parser = new MultiFieldQueryParser(Version.LUCENE_30,
 56                 new String[] { "title", "content" }, LuceneUtils.analyzer);
 57 
 58         Query query = parser.parse("lucene");
 59         TopDocs topDocs = indexSearcher.search(query, 10);
 60         ScoreDoc[] scoreDos = topDocs.scoreDocs;
 61         List<Article> articleList = new ArrayList<Article>();
 62         for (ScoreDoc scoreDoc : scoreDos) {
 63             int num = scoreDoc.doc;
 64             Document document = indexSearcher.doc(num);
 65             articleList.add(DocumentUtils.document2Article(document));
 66         }
 67 
 68         for (Article article : articleList) {
 69             System.out.println(article.getId());
 70             System.out.println(article.getTitle());
 71             System.out.println(article.getContent());
 72         }
 73     }
 74     
 75     
 76      /**
 77       * 文件索引库和内存索引库的结合
 78       */
 79     @Test
 80      public void testRamAndFile() throws Exception{
 81          /**
 82           * 1、当应用程序启动的时候,把文件索引库的内容复制到内存库中
 83           * 2、让内存索引库和应用程序交互
 84           * 3、把内存索引库的内容同步到文件索引库
 85           */
 86         Directory fileDirectory = FSDirectory.open(new File("E:\\s"));
 87         Directory ramDirectory = new RAMDirectory(fileDirectory);
 88         
 89         IndexWriter ramIndexDeWriter = new IndexWriter(ramDirectory,LuceneUtils.analyzer,MaxFieldLength.LIMITED);
 90         IndexWriter fileIndexWriter = new IndexWriter(fileDirectory,LuceneUtils.analyzer,true,MaxFieldLength.LIMITED);
 91          /**
 92           * 在内存索引库中根据关键词查询
 93           */
 94         this.showData(ramDirectory);
 95         
 96         System.out.println("上面的是从内存索引库中查询出来的");
 97         
 98         /**
 99           * 把一条信息插入到内存索引库
100           */
101           Article article = new Article();
102           article.setId(1L);
103           article.setTitle("lucene可以做搜索引擎");
104           article.setContent("baidu,google都是很好的搜索引擎");
105           ramIndexDeWriter.addDocument(DocumentUtils.article2Document(article));
106           ramIndexDeWriter.close();
107           
108           
109           
110           /*
111            * 把内存索引库中的内容同步到文件索引库中
112            */
113            fileIndexWriter.addIndexesNoOptimize(ramDirectory);
114            fileIndexWriter.close();
115            this.showData(fileDirectory);
116            System.out.println("上面的是从文件索引库中查询出来的");
117     }
118 }

 

posted on 2016-10-24 11:41  Sharpest  阅读(257)  评论(0)    收藏  举报