簡體   English   中英

Lucene 4.0索引編寫器-創建單個索引

[英]Lucene 4.0 Index writer - creating single index

我正在嘗試實現最簡單的Lucene搜索。 以此為出發點。

我可以理解示例代碼:

public static void indexHotel(Hotel hotel) throws IOException {
    IndexWriter writer = (IndexWriter) getIndexWriter(false);
    Document doc = new Document();
    doc.add(new Field("id", hotel.getId(), Field.Store.YES,
    Field.Index.NO));
    doc.add(new Field("name", hotel.getName(), Field.Store.YES,
    Field.Index.TOKENIZED));
    doc.add(new Field("city", hotel.getCity(), Field.Store.YES,
    Field.Index.UN_TOKENIZED));
    doc.add(new Field("description", hotel.getDescription(),
    Field.Store.YES,
    Field.Index.TOKENIZED));
    String fullSearchableText = hotel.getName() + " " + hotel.getCity() + " " + hotel.getDescription();

    doc.add(new Field("content", fullSearchableText,
    Field.Store.NO,
    Field.Index.TOKENIZED));
    writer.addDocument(doc);
}

我在這段代碼中無法得到的是getIndexWriter(false)作用。 我跟隨的帖子中沒有提到此方法。 此外,在另一個代碼塊中:

public void rebuildIndexes() throws IOException {
   //
   // Erase existing index
   //
   getIndexWriter(true);
   //
   // Index all hotel entries
   //
   Hotel[] hotels = HotelDatabase.getHotels();
   for(Hotel hotel: hotels) {
     indexHotel(hotel);
   }
   //
   // Don’t forget to close the index writer when done
   //
   closeIndexWriter();
 }

使用了一些未定義的方法。

對於像我這樣的初學者有些困惑。

我只想創建一個索引。 我認為getIndexWriter(true); closeIndexWriter()是一些實用的方法,只是為了獲取IndexWriter但我無法對getIndexWriter(true);中的true做任何假設getIndexWriter(true); 是用來。

通過遵循其他一些文章,iv'e對IndexWriter的創建更加困惑。

如果我做錯了任何人,可以請我走上正確的道路嗎?

好吧,根據您的索引(RAM或文件系統)在哪里,您可以打開不同的indexWriters。 假設您嘗試將索引寫入文件系統,則應具有以下內容:

public static final Version luceneVersion = Version.LUCENE_40;

IndexWriter getIndexWriter(){

    Directory indexDir = FSDirectory.open(new File( INDEX_PATH ));

    IndexWriterConfig luceneConfig = new IndexWriterConfig(
                luceneVersion, new StandardAnalyzer(luceneVersion));

    return(new IndexWriter(indexDir, luceneConfig));
   }

注意分析器類為“ StandardAnalyzer”,您應根據應用要求選擇分析器。 我認為StandardAnalyzer足以滿足您的需求。

輸入參數可能會詢問是否應創建新作家?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM