2012-10-12 18 views
6

對Lucene的核心4.0提及的發行說明作爲一個值得關注的變化:如何爲Lucene 4.0啓用新的可選BlockPostingsFormat?

•新的「塊」 PostingsFormat提供改進的搜索性能和索引壓縮。這可能會成爲未來版本的默認格式。

根據此blog post,BlockPostingsFormat產生較小的索引,並且對於大多數查詢而言,速度比先前的格式更快。

但是,我找不到在4.0中如何選擇此格式的任何地方。新的BlockPostingsFormat在哪裏可以優先於舊的默認指定?

回答

4

幾個步驟:

  1. 選擇一個編解碼器。然後「修改」它使用BlockPostingsFormat作爲PostingFormat類。您可以擴展編解碼器的類,也可以使用FilterCodec,它可以覆蓋一些編解碼器的設置。
  2. 在META-INF/services/org.apache.lucene.codecs.Codec中創建一個文件。它應該列出在上一步中創建的編解碼器類的完整類名。這是爲了滿足Lucene 4加載編解碼器的方式。
  3. 致電IndexWriterConfig.setCodec(Codec)指定您剛創建的編解碼器。
  4. 像往常一樣使用IndexWriterConfig對象。

根據Javadoc,BlockPostingsFormat在索引directoy中創建.doc和.pos文件,而Lucene40PostingsFormat創建.frq和.prx文件。所以這是告訴Lucene是否真的使用塊發佈格式的一種方法。

我修改了Lucene核心Javadoc中的例子來測試塊發佈格式。下面是代碼(並希望它可以幫助):


org.apache.lucene.codecs.Codec

# See http://www.romseysoftware.co.uk/2012/07/04/writing-a-new-lucene-codec/ 
# This file should be in /somewhere_in_your_classpath/META-INF/services/org.apache.lucene.codecs.Codec 
# 
# List of codecs 
lucene4examples.Lucene40WithBlockCodec 

Lucene40WithBlockCodec.java

package lucene4examples; 

import org.apache.lucene.codecs.FilterCodec; 
import org.apache.lucene.codecs.PostingsFormat; 
import org.apache.lucene.codecs.block.BlockPostingsFormat; 
import org.apache.lucene.codecs.lucene40.Lucene40Codec; 

// Lucene 4.0 codec with block posting format 

public class Lucene40WithBlockCodec extends FilterCodec { 

    public Lucene40WithBlockCodec() { 
    super("Lucene40WithBlock", new Lucene40Codec()); 

    } 

    @Override 
    public PostingsFormat postingsFormat() { 
    return new BlockPostingsFormat(); 
    } 

} 

BlockPostingsFormatExample.java

package lucene4examples; 

import java.io.File; 
import java.io.IOException; 

import org.apache.lucene.analysis.Analyzer; 
import org.apache.lucene.analysis.standard.StandardAnalyzer; 
import org.apache.lucene.document.Document; 
import org.apache.lucene.document.Field; 
import org.apache.lucene.document.TextField; 
import org.apache.lucene.index.DirectoryReader; 
import org.apache.lucene.index.IndexWriter; 
import org.apache.lucene.index.IndexWriterConfig; 
import org.apache.lucene.queryparser.classic.ParseException; 
import org.apache.lucene.queryparser.classic.QueryParser; 
import org.apache.lucene.search.IndexSearcher; 
import org.apache.lucene.search.Query; 
import org.apache.lucene.search.ScoreDoc; 
import org.apache.lucene.store.Directory; 
import org.apache.lucene.store.FSDirectory; 
import org.apache.lucene.util.Version; 

// This example is based on the one that comes with Lucene 4.0.0 core API Javadoc 
// (http://lucene.apache.org/core/4_0_0/core/overview-summary.html) 

public class BlockPostingsFormatExample { 

    public static void main(String[] args) throws IOException, ParseException { 
    Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_40); 

    // Store the index on disk: 
    Directory directory = FSDirectory.open(new File(
     "/index_dir")); 
    IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_40, 
     analyzer); 

    // If the following line of code is commented out, the original Lucene 
    // 4.0 codec will be used. 
    // Else, the Lucene 4.0 codec with block posting format 
    // (http://blog.mikemccandless.com/2012/08/lucenes-new-blockpostingsformat-thanks.html) 
    // will be used. 
    config.setCodec(new Lucene40WithBlockCodec()); 

    IndexWriter iwriter = new IndexWriter(directory, config); 
    Document doc = new Document(); 
    String text = "This is the text to be indexed."; 
    doc.add(new Field("fieldname", text, TextField.TYPE_STORED)); 
    iwriter.addDocument(doc); 
    iwriter.close(); 

    // Now search the index: 
    DirectoryReader ireader = DirectoryReader.open(directory); 
    IndexSearcher isearcher = new IndexSearcher(ireader); 
    // Parse a simple query that searches for "text": 
    QueryParser parser = new QueryParser(Version.LUCENE_40, "fieldname", 
     analyzer); 
    Query query = parser.parse("text"); 
    ScoreDoc[] hits = isearcher.search(query, null, 1000).scoreDocs; 
    System.out.println("hits.length = " + hits.length); 
    // Iterate through the results: 
    for (int i = 0; i < hits.length; i++) { 
     Document hitDoc = isearcher.doc(hits[i].doc); 
     System.out.println("text: " + hitDoc.get("fieldname")); 
    } 
    ireader.close(); 
    directory.close(); 
    } 

}