2.lucene入门-保持数据库与索引库的同步


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

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

1.

 1 package com.home.utils;
 2 
 3 import org.apache.lucene.document.Document;
 4 import org.apache.lucene.document.Field;
 5 import org.apache.lucene.document.Field.Index;
 6 import org.apache.lucene.document.Field.Store;
 7 import org.w3c.dom.ranges.DocumentRange;
 8 
 9 public class DocumentUtils {
10 
11     public static Document article2Document(Article article) {
12 
13         Document document = new Document();
14         Field idField = new Field("id", article.getId().toString(), Store.YES,
15                 Index.NOT_ANALYZED);
16         Field titleField = new Field("title", article.getTitle(), Store.YES,
17                 Index.ANALYZED);
18         Field contentField = new Field("content", article.getContent(),
19                 Store.YES, Index.ANALYZED);
20         document.add(idField);
21         document.add(titleField);
22         document.add(contentField);
23 
24         return document;
25     }
26 
27     public static Article document2Article(Document document) {
28         Article article = new Article();
29         article.setId(Long.parseLong(document.get("id")));
30         article.setTitle(document.get("title"));
31         article.setContent(document.get("content"));
32         return article;
33     }
34     
35     
36 }

 

2.

 1 package com.home.utils;
 2 
 3 import java.io.File;
 4 
 5 import org.apache.lucene.analysis.Analyzer;
 6 import org.apache.lucene.analysis.standard.StandardAnalyzer;
 7 import org.apache.lucene.store.Directory;
 8 import org.apache.lucene.store.FSDirectory;
 9 import org.apache.lucene.util.Version;
10 
11 public class LuceneUtils {
12 
13     public static Directory directory;
14     public static Analyzer analyzer;
15 
16     static {
17         try {
18             directory = FSDirectory.open(new File("E:\\s"));
19             analyzer = new StandardAnalyzer(Version.LUCENE_30);
20 
21         } catch (Exception e) {
22             e.printStackTrace();
23         }
24     }
25     
26     
27 }

 

3.

  1 package com.home.utils;
  2 
  3 import java.io.IOException;
  4 import java.util.ArrayList;
  5 import java.util.List;
  6 
  7 import org.apache.lucene.document.Document;
  8 import org.apache.lucene.index.CorruptIndexException;
  9 import org.apache.lucene.index.IndexReader;
 10 import org.apache.lucene.index.IndexWriter;
 11 import org.apache.lucene.index.IndexWriter.MaxFieldLength;
 12 import org.apache.lucene.index.Term;
 13 import org.apache.lucene.queryParser.MultiFieldQueryParser;
 14 import org.apache.lucene.queryParser.QueryParser;
 15 import org.apache.lucene.search.IndexSearcher;
 16 import org.apache.lucene.search.Query;
 17 import org.apache.lucene.search.ScoreDoc;
 18 import org.apache.lucene.search.TopDocs;
 19 import org.apache.lucene.search.TopScoreDocCollector;
 20 import org.apache.lucene.store.LockObtainFailedException;
 21 import org.apache.lucene.util.Version;
 22 import org.junit.Test;
 23 
 24 import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;
 25 
 26 public class ArticleIndex {
 27 
 28     @Test
 29     public void testCreateIndex() throws Exception {
 30         Article article = new Article();
 31         article.setId(1L);
 32         article.setTitle("lucene可以做搜索引擎");
 33         article.setContent("baidu,google都是很好的搜索引擎");
 34 
 35         IndexWriter indexWriter = new IndexWriter(LuceneUtils.directory,
 36                 LuceneUtils.analyzer, MaxFieldLength.LIMITED);
 37         indexWriter.addDocument(DocumentUtils.article2Document(article));
 38         indexWriter.close();
 39 
 40     }
 41 
 42     @Test
 43     public void testSearchIndex() throws Exception {
 44 
 45         IndexSearcher indexSearcher = new IndexSearcher(LuceneUtils.directory);
 46         // 使用MultiFieldQueryParser进行多字段搜索
 47         QueryParser parser = new MultiFieldQueryParser(Version.LUCENE_30,
 48                 new String[] { "id", "content" }, LuceneUtils.analyzer);
 49         Query query = parser.parse("baidu");
 50         TopDocs topDocs = indexSearcher.search(query, 2);
 51         ScoreDoc[] scDocs = topDocs.scoreDocs;
 52         List<Article> articleList = new ArrayList<Article>();
 53         for (ScoreDoc doc : scDocs) {
 54             Document document = indexSearcher.doc(doc.doc);
 55             Article article = DocumentUtils.document2Article(document);
 56             articleList.add(article);
 57         }
 58         
 59 
 60         for (Article article : articleList) {
 61             System.out.println(article.getId());
 62             System.out.println(article.getTitle());
 63             System.out.println(article.getContent());
 64         }
 65 
 66     }
 67 
 68     /**
 69      * 一般情况下索引库的删除用关键词
 70      * 
 71      * @throws Exception
 72      */
 73     @Test
 74     public void testDeleteIndex() throws Exception {
 75         IndexWriter indexWriter = new IndexWriter(LuceneUtils.directory,LuceneUtils.analyzer,MaxFieldLength.LIMITED);
 76         //indexWriter.deleteAll()删除所有的索引值
 77           /**
 78           * term就为关键词对象
 79           */
 80         Term term = new Term("title", "lucene");
 81         indexWriter.deleteDocuments(term);
 82         indexWriter.close();
 83     }
 84     
 85     /**
 86       * 修改
 87       *    先删除后增加
 88       */
 89      @Test
 90      public void testUpdateIndex() throws Exception{
 91       IndexWriter indexWriter = new IndexWriter(LuceneUtils.directory,LuceneUtils.analyzer,MaxFieldLength.LIMITED);
 92       Term term = new Term("title", "lucene");
 93       Article article = new  Article();
 94       article.setId(1L);
 95       article.setTitle("lucene可以做搜索引擎");
 96       article.setContent("修改后的内容");
 97       /**
 98       * term是用删除的
 99       * document是用于增加的
100       */
101       indexWriter.updateDocument(term, DocumentUtils.article2Document(article));
102       indexWriter.close();
103      
104     }
105      
106      
107      public void create () throws CorruptIndexException, LockObtainFailedException, IOException{
108           Article article = new  Article();
109           article.setId(1L);
110           article.setTitle("lucene可以做搜索引擎");
111           article.setContent("baidu,google都是很好的搜索引擎");
112           IndexWriter indexWriter=new IndexWriter(LuceneUtils.directory, LuceneUtils.analyzer, MaxFieldLength.LIMITED);
113           indexWriter.addDocument(DocumentUtils.article2Document(article));
114           indexWriter.close();
115          }
116 
117          
118 
119         @Test
120          public void Query() throws CorruptIndexException, IOException,
121            ParseException, org.apache.lucene.queryParser.ParseException {
122           IndexSearcher indexSearcher = new IndexSearcher(LuceneUtils.directory);
123           QueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_30,
124             new String[] { "title", "content" }, LuceneUtils.analyzer);
125 
126           TopDocs topDocs = indexSearcher.search(queryParser.parse("lucene"), 2);
127           ScoreDoc[] scoreDocs = topDocs.scoreDocs;
128           List<Article> articleList = new ArrayList<Article>();
129           for (ScoreDoc scoreDoc : scoreDocs) {
130            System.err.println("相关度为" + scoreDoc.score);
131            Document document = indexSearcher.doc(scoreDoc.doc);
132            Article article = DocumentUtils.document2Article(document);
133            articleList.add(article);
134           }
135           for (Article article : articleList) {
136            System.out.println(article.getId());
137            System.out.println(article.getTitle());
138            System.out.println(article.getContent());
139           }
140          }
141 
142          
143 
144         @Test
145          public void delete() throws CorruptIndexException, LockObtainFailedException, IOException{
146           IndexWriter indexWriter=new IndexWriter(LuceneUtils.directory, LuceneUtils.analyzer, MaxFieldLength.LIMITED);
147           // indexWriter.deleteAll()删除所有的索引值
148             /**
149             * term就为关键词对象
150             */
151           indexWriter.deleteDocuments(new Term("title","lucene"));
152           indexWriter.close();
153          
154          }
155 
156          
157 
158         /**
159           *
160           * 修改 先删除后增加
161           * @throws IOException
162           * @throws CorruptIndexException
163           *
164           */
165          @Test
166          public void update() throws CorruptIndexException, IOException {
167           IndexWriter indexWriter = new IndexWriter(LuceneUtils.directory,
168             LuceneUtils.analyzer, MaxFieldLength.LIMITED);
169           Term term = new Term("title", "lucene");
170           Article article = new Article();
171           article.setId(1L);
172           article.setTitle("lucene可以做搜索引擎");
173           article.setContent("修改后的内容");
174           /**
175           * term是用删除的 document是用于增加的
176           */
177           indexWriter.updateDocument(term,
178             DocumentUtils.article2Document(article));
179           indexWriter.close();
180          }
181 
182          
183 }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM