Lucene - 刪除文件操作



刪除文件是索引過程中的另一個重要操作。當已索引的內容被更新並且索引變得無效或索引變得非常大時,為了減小大小並更新索引,會執行刪除操作。

我們將包含欄位(Field)文件(Document)刪除到IndexWriter,其中IndexWriter用於更新索引。

我們現在將向您展示一個分步方法,並讓您瞭解如何使用一個基本示例刪除文件。

從索引中刪除文件

請按照以下步驟從索引中刪除文件:

步驟 1 - 建立一個方法來刪除過時文字檔案的 Lucene 文件。

private void deleteDocument(File file) throws IOException {
   
   //delete indexes for a file
   writer.deleteDocument(new Term(LuceneConstants.FILE_NAME,file.getName())); 

   writer.commit();
   System.out.println("index contains deleted files: "+writer.hasDeletions());
   System.out.println("index contains documents: "+writer.maxDoc());
   System.out.println("index contains deleted documents: "+writer.numDoc());
}   

建立 IndexWriter

IndexWriter 類充當核心元件,在索引過程中建立/更新索引。

請按照以下步驟建立 IndexWriter:

步驟 1 - 建立 IndexWriter 物件。

步驟 2 - 建立一個 Lucene 目錄,該目錄應指向要儲存索引的位置。

步驟 3 - 使用索引目錄、具有版本資訊和其它必需/可選引數的標準分析器初始化建立的 IndexWriter 物件。

private IndexWriter writer;

public Indexer(String indexDirectoryPath) throws IOException {
   //this directory will contain the indexes
   Directory indexDirectory = 
      FSDirectory.open(new File(indexDirectoryPath));
   
   //create the indexer
   writer = new IndexWriter(indexDirectory, 
      new StandardAnalyzer(Version.LUCENE_36),true,
      IndexWriter.MaxFieldLength.UNLIMITED);
}

刪除文件並啟動重新索引過程

以下是刪除文件的方法。

  • deleteDocuments(Term) - 刪除包含該術語的所有文件。

  • deleteDocuments(Term[]) - 刪除包含陣列中任何術語的所有文件。

  • deleteDocuments(Query) - 刪除與查詢匹配的所有文件。

  • deleteDocuments(Query[]) - 刪除與陣列中查詢匹配的所有文件。

  • deleteAll - 刪除所有文件。

private void indexFile(File file) throws IOException {
   System.out.println("Deleting index for "+file.getCanonicalPath());
   deleteDocument(file);   
}

示例應用程式

為了測試索引過程,讓我們建立一個 Lucene 應用程式測試。

步驟 描述
1

在包com.tutorialspoint.lucene下建立一個名為LuceneFirstApplication的專案,如Lucene - 第一個應用程式章節中所述。您也可以使用EJB - 第一個應用程式章節中建立的專案,以便在本節中瞭解索引過程。

2

建立LuceneConstants.java、TextFileFilter.javaIndexer.java,如Lucene - 第一個應用程式章節中所述。保持其餘檔案不變。

3

建立如下所示的LuceneTester.java

4

清理並構建應用程式,以確保業務邏輯按要求工作。

LuceneConstants.java

此類提供可在整個示例應用程式中使用的各種常量。

package com.tutorialspoint.lucene;

public class LuceneConstants {
   public static final String CONTENTS = "contents";
   public static final String FILE_NAME = "filename";
   public static final String FILE_PATH = "filepath";
   public static final int MAX_SEARCH = 10;
}

TextFileFilter.java

此類用作.txt檔案過濾器。

package com.tutorialspoint.lucene;

import java.io.File;
import java.io.FileFilter;

public class TextFileFilter implements FileFilter {

   @Override
   public boolean accept(File pathname) {
      return pathname.getName().toLowerCase().endsWith(".txt");
   }
}

Indexer.java

此類用於索引原始資料,從而使其可以使用 Lucene 庫進行搜尋。

package com.tutorialspoint.lucene;

import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.io.IOException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

public class Indexer {

   private IndexWriter writer;

   public Indexer(String indexDirectoryPath) throws IOException {
      //this directory will contain the indexes
      Directory indexDirectory = 
         FSDirectory.open(new File(indexDirectoryPath));

      //create the indexer
      writer = new IndexWriter(indexDirectory, 
         new StandardAnalyzer(Version.LUCENE_36),true,
         IndexWriter.MaxFieldLength.UNLIMITED);
   }

   public void close() throws CorruptIndexException, IOException {
      writer.close();
   }

   private void deleteDocument(File file) throws IOException {
      //delete indexes for a file
      writer.deleteDocuments(
         new Term(LuceneConstants.FILE_NAME,file.getName())); 

	  writer.commit();  
   }  

   private void indexFile(File file) throws IOException {
      System.out.println("Deleting index: "+file.getCanonicalPath());
      deleteDocument(file);      
   }

   public int createIndex(String dataDirPath, FileFilter filter) 
      throws IOException {
      //get all files in the data directory
      File[] files = new File(dataDirPath).listFiles();

      for (File file : files) {
         if(!file.isDirectory()
            && !file.isHidden()
            && file.exists()
            && file.canRead()
            && filter.accept(file)
         ){
            indexFile(file);
         }
      }
      return writer.numDocs();
   }
}

LuceneTester.java

此類用於測試 Lucene 庫的索引功能。

package com.tutorialspoint.lucene;

import java.io.IOException;

public class LuceneTester {
	
   String indexDir = "E:\\Lucene\\Index";
   String dataDir = "E:\\Lucene\\Data";
   Indexer indexer;
   
   public static void main(String[] args) {
      LuceneTester tester;
      try {
         tester = new LuceneTester();
         tester.createIndex();
      } catch (IOException e) {
         e.printStackTrace();
      } 
   }

   private void createIndex() throws IOException {
      indexer = new Indexer(indexDir);
      int numIndexed;
      long startTime = System.currentTimeMillis();	
      numIndexed = indexer.createIndex(dataDir, new TextFileFilter());
      long endTime = System.currentTimeMillis();
      indexer.close();
   }
}

資料和索引目錄建立

我們使用了從 record1.txt 到 record10.txt 的 10 個文字檔案,其中包含學生姓名和其他詳細資訊,並將它們放在 E:\Lucene\Data 目錄中。 測試資料。應建立索引目錄路徑為 E:\Lucene\Index。執行此程式後,您可以在該資料夾中看到建立的索引檔案列表。

執行程式

完成原始碼、原始資料、資料目錄和索引目錄的建立後,您可以編譯並執行程式。為此,請保持LuceneTester.Java檔案選項卡處於活動狀態,並使用 Eclipse IDE 中提供的“執行”選項,或使用Ctrl + F11編譯並執行LuceneTester應用程式。如果您的應用程式成功執行,它將在 Eclipse IDE 的控制檯中列印以下訊息:

Deleting index E:\Lucene\Data\record1.txt
Deleting index E:\Lucene\Data\record10.txt
Deleting index E:\Lucene\Data\record2.txt
Deleting index E:\Lucene\Data\record3.txt
Deleting index E:\Lucene\Data\record4.txt
Deleting index E:\Lucene\Data\record5.txt
Deleting index E:\Lucene\Data\record6.txt
Deleting index E:\Lucene\Data\record7.txt
Deleting index E:\Lucene\Data\record8.txt
Deleting index E:\Lucene\Data\record9.txt
10 File indexed, time taken: 109 ms

成功執行程式後,您的索引目錄中將包含以下內容:

Lucene Index Directory
lucene_indexing_operations.htm
廣告

© . All rights reserved.