5 Mart 2018 Pazartesi

Lucene Index Yazma Sınıfları

IndexWriter Sınıfı
Giriş
Şu satırı dahil ederiz.
import org.apache.lucene.index.IndexWriter;
constructor
İmzası şöyle
IndexWriter(Directory d, Analyzer a, boolean create, IndexWriter.MaxFieldLength mfl) 
Açıklaması şöyle
IndexWriter accepts an analyzer used to tokenize data before it is indexed.
Şöyle yaparız.
File directory = ...;
IndexWriter indexWriter = new IndexWriter(FSDirectory.open(directory), analyzer,
  IndexWriter.MaxFieldLength.LIMITED);
constructor - Directory + IndexWriterConfig
Şöyle yaparız.
RAMDirectory ramDir = ...;
Analyzer analyzer = ...;

// Setup the configuration for the index
IndexWriterConfig config = new IndexWriterConfig(analyzer);
config.setOpenMode(IndexWriterConfig.OpenMode.CREATE);

// IndexWriter creates and maintains the index
IndexWriter writer = new IndexWriter(ramDir, config);
close metodu
Şöyle yaparız.
indexWriter.close();
constructor - IndexWriterConfig
Şöyle yaparız.
Analyzer analyzer = ...;
Directory directory = new RAMDirectory();
IndexWriterConfig config = new IndexWriterConfig(analyzer);
IndexWriter indexWriter = new IndexWriter(directory, config);
addDocument metodu
Örnek
Şöyle yaparız.
Document doc = new Document();
...
indexWriter.addDocument(doc);
Örnek
Şöyle yaparız.
// Create the documents
indexDoc(writer, "document-1", "hello planet mercury");
indexDoc(writer, "document-2", "hi PLANET venus");
indexDoc(writer, "document-3", "howdy Planet Earth");
indexDoc(writer, "document-4", "hey planet MARS");
indexDoc(writer, "document-5", "ayee Planet jupiter");

// Close down the writer
writer.close();


private static void indexDoc(IndexWriter writer, String name, String content) 
        throws IOException {
  Document document = new Document();
  document.add(new TextField("name", name, Field.Store.YES));
  document.add(new TextField("body", content, Field.Store.YES));

  writer.addDocument(document);
}
getReader metodu
Şöyle yaparız.
IndexReader indexReader = indexWriter.getReader();
IndexWriterConfig Sınıfı
Giriş
Şu satırı dahil ederiz.
import org.apache.lucene.index.IndexWriterConfig;
constructor metodu - Analyzer
Örnek
Şöyle yaparız.
// Create the analyzer (has default stop words)
Analyzer analyzer = new StandardAnalyzer();

// Setup the configuration for the index
IndexWriterConfig config = new IndexWriterConfig(analyzer);
config.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
Örnek
Şöyle yaparız.
EnglishAnalyzer an = new EnglishAnalyzer();
IndexWriterConfig config = new IndexWriterConfig(an);
constructor metodu - version + Analyzer
Şöyle yaparız.
StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_30,analyzer);
setCommitOnClose metodu
Şöyle yaparız.
config.setCommitOnClose(true);
setOpenMode metodu
Şöyle yaparız.
config.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
Şöyle yaparız.
config.setOpenMode(OpenMode.CREATE_OR_APPEND);

Hiç yorum yok:

Yorum Gönder