lucene 的一些整理

来源:互联网 发布:申请淘宝客服介入处理 编辑:程序博客网 时间:2024/04/29 12:19

lucene3.0学习笔记3-(IndexWriter的一些方法和属性)

文章分类:互联网

2.3 Basic index operations
2.3.1 Adding documents to an index
IndexWriter有两个方法可以加入Document的方法
Java代码 复制代码
  1. addDocument(Document)和addDocument(Document, Analyzer)  

第一个是加Document使用默认的分词器,第二个是加入的时候使用指定的分词器

2.3.2 Deleting documents from an index
IndexWriter提供四个方法删除Document
Java代码 复制代码
  1. deleteDocuments(Term);   
  2. deleteDocuments(Term[]);   
  3. deleteDocuments(Query);   
  4. deleteDocuments(Query[]);  


一般最好有个唯一索引,这样才好删,不然的话有可以会一删一大堆
如:
Java代码 复制代码
  1. writer.deleteDocument(new Term(“ID”, documentID));  

Java代码 复制代码
  1. package com.langhua;   
  2.   
  3. import java.io.File;   
  4. import java.io.IOException;   
  5.   
  6. import org.apache.lucene.analysis.standard.StandardAnalyzer;   
  7. import org.apache.lucene.index.CorruptIndexException;   
  8. import org.apache.lucene.index.IndexWriter;   
  9. import org.apache.lucene.index.Term;   
  10. import org.apache.lucene.store.Directory;   
  11. import org.apache.lucene.store.SimpleFSDirectory;   
  12. import org.apache.lucene.util.Version;   
  13. /**  
  14.  * Lucene 3.0+ 删除索引  
  15.  * @author Administrator  
  16.  *  
  17.  */  
  18.   
  19. public class DeleteIndex {   
  20.   
  21.     public static void main(String[] args) throws CorruptIndexException, IOException {   
  22.         //索引所放目录   
  23.         String indexDir = "F://indexDir";   
  24.         //创建Directory   
  25.         Directory dir =  new SimpleFSDirectory(new File(indexDir));        
  26.         IndexWriter indexWriter = new IndexWriter(dir,new StandardAnalyzer(Version.LUCENE_30),false,IndexWriter.MaxFieldLength.UNLIMITED);   
  27.         //删除filename为time.txt的Document   
  28.         indexWriter.deleteDocuments(new Term("filename","time.txt"));   
  29.         //优化   
  30.         indexWriter.optimize();   
  31.         //提交事务   
  32.         indexWriter.commit();   
  33.         System.out.println("是否有删除="+indexWriter.hasDeletions());   
  34.         //如果不indexWriter.optimize()以下两个会有区别   
  35.         System.out.println("一共有"+indexWriter.maxDoc()+"索引");   
  36.         System.out.println("还剩"+indexWriter.numDocs()+"索引");   
  37.         indexWriter.close();   
  38.     }   
  39.   
  40. }  


2.3.3 Updating documents in the index
更新索引也提供两个方法,其实Lucene是没有办法更新的,只有先删除了再更新,方法如下
Java代码 复制代码
  1. updateDocument(Term, Document)    
  2. //first deletes all documents containing the provided term and then adds the new document using the writer’s default analyzer.   
  3. updateDocument(Term, Document, Analyzer)    
  4. //does the same, but uses the provided analyzer instead of the writer’s default analyzer.  


可以这样使用
Java代码 复制代码
  1. writer.updateDocument(new Term(“ID”, documenteId), newDocument);  

例子如下:
Java代码 复制代码
  1. package com.langhua;   
  2.   
  3. import java.io.File;   
  4. import java.io.FileReader;   
  5. import java.io.IOException;   
  6.   
  7. import org.apache.lucene.analysis.standard.StandardAnalyzer;   
  8. import org.apache.lucene.document.Document;   
  9. import org.apache.lucene.document.Field;   
  10. import org.apache.lucene.index.IndexWriter;   
  11. import org.apache.lucene.index.Term;   
  12. import org.apache.lucene.store.Directory;   
  13. import org.apache.lucene.store.SimpleFSDirectory;   
  14. import org.apache.lucene.util.Version;   
  15.   
  16. /**  
  17.  * Lucene 3.0 更新索引  
  18.  *   
  19.  * @author Administrator  
  20.  *   
  21.  */  
  22. public class updateIndex {   
  23.     public static void main(String[] args) throws IOException {   
  24.         String indexDir = "F://indexDir";   
  25.         String dateDir = "F://dateDir";   
  26.         Directory dir = new SimpleFSDirectory(new File(indexDir));   
  27.         IndexWriter indexWriter = new IndexWriter(dir, new StandardAnalyzer(   
  28.                 Version.LUCENE_30), false, IndexWriter.MaxFieldLength.UNLIMITED);   
  29.         File[] files = new File(dateDir).listFiles();   
  30.         Document doc = new Document();   
  31.         for (int i = 0; i < files.length; i++) {   
  32.             if (files[i].getName().equals("time.txt")) {   
  33.                 doc.add(new Field("contents"new FileReader(files[i])));   
  34.                 doc.add(new Field("filename", files[i].getName(),   
  35.                         Field.Store.YES, Field.Index.NOT_ANALYZED));   
  36.             }   
  37.         }   
  38.         // 更新索引使用默认分词器   
  39.         indexWriter.updateDocument(new Term("filename""time.txt"), doc);   
  40.         indexWriter.close();   
  41.     }   
  42. }  



2.4 Field options
2.4.1 Field options for indexing

在创建Field的时候一般常用的要指定两个参数
Field.Index.*
需要分词,并建立索引
Index.ANALYZED – use the analyzer to break the Field’s value into a stream of separate tokens
and make each token searchable. This is useful for normal text fields (body, title, abstract, etc.).

不分词,直接建立索引
Index.NOT_ANALYZED – do index the field, but do not analyze the String.

不用建立索引
Index.NO – don’t make this field’s value available for searching at all.

后面两个没有看懂。。
Index.ANALYZED_NO_NORMS
Index.NOT_ANALYZED_NO_NORMS

2.4.2 Field options for storing fields
要指定的另一个参数是:Field.Store.*

要保存在Document里面
Store.YES — store the value.

不要保存到Document里面,一般用于建立索引
Store.NO – do not store the value.

2.4.3 Field options for term vectors
在建立Field还有一个不常用的参数TermVector
http://callan.javaeye.com/blog/155602参考一下
因为我也没有怎么看懂,书上说在后面有高亮作用。。。后面书上应该会介绍的

2.4.4 Other Field values
其它的创建Field方法
Java代码 复制代码
  1. //uses a Reader instead of a String to represent the value. In this    
  2. //case the value cannot be stored (hardwired to Store.NO)   
  3. //and is always analyzed and indexed (Index.ANALYZED).   
  4. //这个方法是用来分词的,不能保存   
  5. Field(String name, Reader value, TermVector vector)   
  6. //这个不懂   
  7. Field(String name, TokenStream tokenStream, TermVector TermVector)   
  8. //这个是图片的吧,只能保存,不能建立索引   
  9. //never indexed /no term vectors  /must be Store.YES   
  10. Field(String name, byte[] value, Store store)  


2.4.5 Field option combinations
最后给出了一张图,说明在什么情况下用什么



2.5 Multi-valued Fields
创建多个值,例子:
Java代码 复制代码
  1. Document doc = new Document();   
  2. for (int i = 0; i < authors.length; i++) {   
  3.     //多次加入同一个author   
  4.     doc.add(new Field("author", authors[i],   
  5.     Field.Store.YES,   
  6.     Field.Index.ANALYZED));   
  7. }  


2.6 Boosting Documents and Fields
设置Boosting值,Boosting值在0.1到1.5之间,越大就越排在前面(或者是说越重要,就先搜到他),如果不设的话就没有
如:
Java代码 复制代码
  1. Document doc = new Document();   
  2. doc..setBoost(0.1F); OR .setBoost(1.5);   
  3.   
  4. Field senderNameField = new Field("senderName", senderName,   
  5. Field.Store.YES,   
  6. Field.Index.ANALYZED);   
  7. Field subjectField = new Field("subject", subject,   
  8. Field.Store.YES,   
  9. Field.Index.ANALYZED);   
  10. subjectField.setBoost(1.2F);  


2.6.1 Norms
不明白,没看懂

2.7 Indexing dates & times
Lucene提供了一个工具类DateTools
可以这样来保存时间
Java代码 复制代码
  1. Document doc = new Document();   
  2. doc.add(new Field("indexDate",   
  3. DateTools.dateToString(new Date(), DateTools.Resolution.DAY),   
  4. Field.Store.YES,   
  5. Field.Index.NOT_ANALYZED);  


2.11 Optimizing an index
WriteIndex的优化索引方法
Java代码 复制代码
  1. //优化索引,使多个Segments变成一个Segments   
  2. optimize()   
  3. //指定最大Segments的数量   
  4. optimize(int maxNumSegments)   
  5. //前面的方面都是优化完成之后再返回,这个方法的参数如果是FALSE的话,就直接返回,再开一个线程来优化   
  6. optimize(boolean doWait)   
  7. //前面两个参数的组合哈   
  8. optimize(int maxNumSegments, boolean doWait)  

我对Segments还是不太明白,能不能有人出来解释一下啥

2.12 Other Directory Implementations
截了一张图