2012-10-12 7 views
6

sürüm notları: Yeni bir "Blok" PostingsFormat sunan geliştirilmiş arama performansı ve dizin sıkıştırma •Yeni isteğe bağlı BlockPostingsFormat Lucene 4.0 için nasıl etkinleştirilir? kayda değer bir değişiklik olarak Lucene Çekirdekli 4,0 söz için

. Bu, gelecekteki bir sürümde muhtemelen varsayılan biçim olacaktır.

bu blog post Başına

, küçük dizinlerinde BlockPostingsFormat sonuçları ve önceki biçiminden daha (çoğu sorguları için) daha hızlıdır.

Ancak, bu biçimi 4.0'da nasıl seçeceğiniz konusunda hiçbir şey bulamıyorum. Yeni BlockPostingsFormat, eski varsayılana göre nerede belirtilebilir?

cevap

4

birçok adım:

  1. bir codec al. Sonra PostingFormat sınıfı olarak BlockPostingsFormat kullanmak için "değiştirin". Kodek sınıfını genişletebilir veya kodek ayarlarının bazılarını geçersiz kılmanıza olanak veren FilterCodec'u kullanabilirsiniz.
  2. META-INF/services/org.apache.lucene.codecs.Codec adresinde bir dosya oluşturun. Önceki adımda oluşturduğunuz codec sınıfının tam sınıf adını listelemelidir. Bu, Lucene 4'ün kodekleri yükleme şeklini tatmin etmek.
  3. Yeni oluşturduğunuz kodeki belirtmek için IndexWriterConfig.setCodec(Codec) numaralı telefonu arayın.
  4. IndexWriterConfig nesnesini her zamanki gibi kullanın. Lucene40PostingsFormat .frq ve .prx dosyaları oluşturur iken

Javadoc göre, BlockPostingsFormat, endeks dizinle içinde .doc ve .pos dosyaları oluşturur. Bu yüzden Lucene'nin blok yayınlama formatını gerçekten kullanıp kullanmadığını söylemenin bir yolu budur.

Blok gönderme biçimini test etmek için örneği Lucene core Javadoc'ta değiştirdim. İşte kod (ve umut olur):


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 

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(); 
    } 

} 

Lucene40WithBlockCodec.java

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(); 
    } 

}