lucene入门

转帖地址:http://cleaneyes.javaeye.com/blog/182907

索引3種方式:倒排、後綴數組和簽名文件.


一段讀寫文本文件的代碼:

Java代码 复制代码
  1. BufferWriter writer = new BufferWriter(new FileWriter(destFile));    
  2. BufferReader reader = new BufferReader(new FileReader(readFile));    
  3. String line = reader.readLine();    
  4. while (line != null){    
  5. writer.write(newline);    
  6. writer.newLine();//寫入行分割符    
  7. }    
  8. reader.close();    
  9. writer.close();   
BufferWriter writer = new BufferWriter(new FileWriter(destFile)); 
BufferReader reader = new BufferReader(new FileReader(readFile)); 
String line = reader.readLine(); 
while (line != null){ 
writer.write(newline); 
writer.newLine();//寫入行分割符 
} 
reader.close(); 
writer.close(); 

 
Store類的3個屬性:
Store.NO 不需存儲
Store.YES 需存儲
Store.COMPRESS 壓縮存儲

Index類的4個屬性
Index.NO 不需索引
Index.TOKENIZED 分詞索引
Index.UN_TOKENIZED 不分詞索引
Index.NO_NORMS 索引,但不使用Analyzer,且禁止參加評分

IndexWriter構造方法:

Java代码 复制代码
  1. private IndexWriter(Directory d, Analyzer a, final boolean create, boolean closeDir)    
  2.   
  3. public IndexWriter(File path, Analyzer a, boolean create)    
  4. throws IOException {    
  5. this(FSDirectory.getDirectory(path, create), a, create, true);    
  6. }    
  7.   
  8. public IndexWriter(Directory d, Analyzer a, boolean create)    
  9. throws IOException {    
  10. this(d, a, create, false);    
  11. }   
private IndexWriter(Directory d, Analyzer a, final boolean create, boolean closeDir) 

public IndexWriter(File path, Analyzer a, boolean create) 
throws IOException { 
this(FSDirectory.getDirectory(path, create), a, create, true); 
} 

public IndexWriter(Directory d, Analyzer a, boolean create) 
throws IOException { 
this(d, a, create, false); 
} 

 

往IndexWriter中添加Document

Java代码 复制代码
  1. public void addDocument(Document doc) throws IOException {   
  2.     addDocument(doc, analyzer);   
  3.   }   
  4.   
  5. public void addDocument(Document doc, Analyzer analyzer) throws IOException {   
  6.     DocumentWriter dw =   
  7.       new DocumentWriter(ramDirectory, analyzer, this);   
  8.     dw.setInfoStream(infoStream);   
  9.     String segmentName = newSegmentName();   
  10.     dw.addDocument(segmentName, doc);   
  11.     synchronized (this) {   
  12.       segmentInfos.addElement(new SegmentInfo(segmentName, 1, ramDirectory));   
  13.       maybeMergeSegments();   
  14.     }   
  15.   }  
public void addDocument(Document doc) throws IOException {
    addDocument(doc, analyzer);
  }

public void addDocument(Document doc, Analyzer analyzer) throws IOException {
    DocumentWriter dw =
      new DocumentWriter(ramDirectory, analyzer, this);
    dw.setInfoStream(infoStream);
    String segmentName = newSegmentName();
    dw.addDocument(segmentName, doc);
    synchronized (this) {
      segmentInfos.addElement(new SegmentInfo(segmentName, 1, ramDirectory));
      maybeMergeSegments();
    }
  }

 

 注意:在使用addDocument方法後,一定要使用IndexWriter的close方法關閉索引器。否則,索引不會被最終建立,同時可能出現下次加入索引時目錄鎖定的問題。

 

DocumentWriter的構造方法:

Java代码 复制代码
  1. DocumentWriter(Directory directory, Analyzer analyzer,   
  2.                Similarity similarity, int maxFieldLength) {   
  3.   this.directory = directory;   
  4.   this.analyzer = analyzer;   
  5.   this.similarity = similarity;   
  6.   this.maxFieldLength = maxFieldLength;   
  7. }   
  8.   
  9. DocumentWriter(Directory directory, Analyzer analyzer, IndexWriter writer) {   
  10.   this.directory = directory;   
  11.   this.analyzer = analyzer;   
  12.   this.similarity = writer.getSimilarity();   
  13.   this.maxFieldLength = writer.getMaxFieldLength();   
  14.   this.termIndexInterval = writer.getTermIndexInterval();   
  15. }  
  DocumentWriter(Directory directory, Analyzer analyzer,
                 Similarity similarity, int maxFieldLength) {
    this.directory = directory;
    this.analyzer = analyzer;
    this.similarity = similarity;
    this.maxFieldLength = maxFieldLength;
  }

  DocumentWriter(Directory directory, Analyzer analyzer, IndexWriter writer) {
    this.directory = directory;
    this.analyzer = analyzer;
    this.similarity = writer.getSimilarity();
    this.maxFieldLength = writer.getMaxFieldLength();
    this.termIndexInterval = writer.getTermIndexInterval();
  }

 

DocumentWriter的addDocument方法:

 

Java代码 复制代码
  1. final void addDocument(String segment, Document doc)   
  2.           throws IOException {   
  3.     // write field names   
  4.     fieldInfos = new FieldInfos();   
  5.     fieldInfos.add(doc);   
  6.     fieldInfos.write(directory, segment + ".fnm");   
  7.   
  8.     // write field values負責寫入.fdx和.fdt文件   
  9.     FieldsWriter fieldsWriter =   
  10.             new FieldsWriter(directory, segment, fieldInfos);   
  11.     try {   
  12.       fieldsWriter.addDocument(doc);   
  13.     } finally {   
  14.       fieldsWriter.close();   
  15.     }   
  16.   
  17.     // invert doc into postingTable   
  18.     postingTable.clear();             // clear postingTable初始化存儲所有詞條的HashTable   
  19.     fieldLengths = new int[fieldInfos.size()];    // init fieldLengths   
  20.     fieldPositions = new int[fieldInfos.size()];  // init fieldPositions所有Field在分析完畢後的最終Position   
  21.     fieldOffsets = new int[fieldInfos.size()];    // init fieldOffsets   
  22.   
  23.     fieldBoosts = new float[fieldInfos.size()];   // init fieldBoosts   
  24.     Arrays.fill(fieldBoosts, doc.getBoost());   
  25.   
  26.     invertDocument(doc);//倒排Document中每Field   
  27.   
  28.     // sort postingTable into an array對詞條進行排序   
  29.     Posting[] postings = sortPostingTable();   
  30.   
  31.     // write postings把詞條信息寫入索引,主要是向.frq和.prx文件中寫入詞條的頻率和位置信息   
  32.     writePostings(postings, segment);   
  33.   
  34.     // write norms of indexed fields把得分信息寫入索引,主要是向.f文件中寫入   
  35.     writeNorms(segment);   
  36.   
  37.   }  
final void addDocument(String segment, Document doc)
          throws IOException {
    // write field names
    fieldInfos = new FieldInfos();
    fieldInfos.add(doc);
    fieldInfos.write(directory, segment + ".fnm");

    // write field values負責寫入.fdx和.fdt文件
    FieldsWriter fieldsWriter =
            new FieldsWriter(directory, segment, fieldInfos);
    try {
      fieldsWriter.addDocument(doc);
    } finally {
      fieldsWriter.close();
    }

    // invert doc into postingTable
    postingTable.clear();			  // clear postingTable初始化存儲所有詞條的HashTable
    fieldLengths = new int[fieldInfos.size()];    // init fieldLengths
    fieldPositions = new int[fieldInfos.size()];  // init fieldPositions所有Field在分析完畢後的最終Position
    fieldOffsets = new int[fieldInfos.size()];    // init fieldOffsets

    fieldBoosts = new float[fieldInfos.size()];	  // init fieldBoosts
    Arrays.fill(fieldBoosts, doc.getBoost());

    invertDocument(doc);//倒排Document中每Field

    // sort postingTable into an array對詞條進行排序
    Posting[] postings = sortPostingTable();

    // write postings把詞條信息寫入索引,主要是向.frq和.prx文件中寫入詞條的頻率和位置信息
    writePostings(postings, segment);

    // write norms of indexed fields把得分信息寫入索引,主要是向.f文件中寫入
    writeNorms(segment);

  }

 

索引目錄內的文件:

segment,是一個邏輯概念,在每個segment時,有許多的Document。每個segment內的所有索引文件都具有相同的前綴,但後綴不同。每個segment的名稱都是由segmentInfos.counter先加1,再轉成36進制,再在前面加上_而成。segmentInfos.counter的值其實就是當前segemnt中總共的文檔數量。

而一個目錄下隻有一個segments和deleable文件

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值