Lucene-fieldoptions

提供:Dev Guides
移動先:案内検索

Lucene-フィールドオプション

フィールドは、インデックス作成プロセスの最も重要な単位です。 これは、インデックス付けされるコンテンツを含む実際のオブジェクトです。 フィールドを追加すると、Luceneはフィールドオプションを使用して、フィールドを検索できるようにするフィールドの数を制御する多数のコントロールを提供します。

_Field(s)_を含む_Document(s)_を_IndexWriter_に追加します。IndexWriterを使用して、インデックスを更新または作成します。

ここでは、段階的なアプローチを示し、基本的な例を使用してさまざまなフィールドオプションを理解するのに役立ちます。

さまざまなフィールドオプション

以下は、さまざまなフィールドオプションです-

  • Index.ANALYZED -これでは、最初に分析を行い、次にインデックス作成を行います。 これは通常のテキストインデックス作成に使用されます。 アナライザーはフィールドの値をトークンのストリームに分割し、各トークンは個別に検索可能です。
  • Index.NOT_ANALYZED -これでは、分析は行いませんが、インデックスを作成します。 これは、完全なテキストインデックス作成に使用されます。 たとえば、人の名前、URLなど。
  • Index.ANALYZED_NO_NORMS -これは Index.ANALYZED のバリアントです。 アナライザはフィールドの値をトークンのストリームに分割し、各トークンは個別に検索可能です。 ただし、NORMはインデックスに保存されません。 NORMSは検索を後押しするために使用され、多くの場合、多くのメモリを消費します。
  • Index.Index.NOT_ANALYZED_NO_NORMS -これは Index.NOT_ANALYZED のバリアントです。 インデックスは作成されますが、NORMはインデックスに保存されません。
  • Index.NO -フィールド値は検索できません。

フィールドオプションの使用

以下は、フィールドオプションを使用できるさまざまな方法です-

  • テキストファイルからLuceneドキュメントを取得するメソッドを作成します。
  • インデックスを作成するコンテンツとして名前と値としてキーを含むキーと値のペアであるさまざまなタイプのフィールドを作成します。
  • 分析するフィールドを設定するかどうか。 この場合、a、am、are、anなどのデータを含むことができるため、コンテンツのみが分析されます。 検索操作では必要ありません。
  • 新しく作成されたフィールドをドキュメントオブジェクトに追加し、呼び出し元のメソッドに返すには。
private Document getDocument(File file) throws IOException {
   Document document = new Document();

  //index file contents
   Field contentField = new Field(LuceneConstants.CONTENTS,
      new FileReader(file));

  //index file name
   Field fileNameField = new Field(LuceneConstants.FILE_NAME,
      file.getName(),
      Field.Store.YES,Field.Index.NOT_ANALYZED);

  //index file path
   Field filePathField = new Field(LuceneConstants.FILE_PATH,
      file.getCanonicalPath(),
      Field.Store.YES,Field.Index.NOT_ANALYZED);

   document.add(contentField);
   document.add(fileNameField);
   document.add(filePathField);

   return document;
}

応用例

インデックス作成プロセスをテストするには、Luceneアプリケーションテストを作成する必要があります。

Step Description
1 Create a project with a name LuceneFirstApplication under a package com.finddevguides.lucene as explained in the Lucene - First Application chapter. You can also use the project created in EJB - First Application chapter as such for this chapter to understand the indexing process.
2 Create LuceneConstants.java,TextFileFilter.java and Indexer.java as explained in the Lucene - First Application chapter. Keep the rest of the files unchanged.
3 Create LuceneTester.java as mentioned below.
4 Clean and Build the application to make sure the business logic is working as per the requirements.

LuceneConstants.java

このクラスは、サンプルアプリケーション全体で使用されるさまざまな定数を提供するために使用されます。

package com.finddevguides.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.finddevguides.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.finddevguides.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.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 Document getDocument(File file) throws IOException {
      Document document = new Document();

     //index file contents
      Field contentField = new Field(LuceneConstants.CONTENTS,
         new FileReader(file));

     //index file name
      Field fileNameField = new Field(LuceneConstants.FILE_NAME,
         file.getName(),
         Field.Store.YES,Field.Index.NOT_ANALYZED);

     //index file path
      Field filePathField = new Field(LuceneConstants.FILE_PATH,
         file.getCanonicalPath(),
         Field.Store.YES,Field.Index.NOT_ANALYZED);

      document.add(contentField);
      document.add(fileNameField);
      document.add(filePathField);

      return document;
   }

   private void indexFile(File file) throws IOException {
      System.out.println("Indexing "+file.getCanonicalPath());
      Document document = getDocument(file);
      writer.addDocument(document);
   }

   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.finddevguides.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();
      System.out.println(numIndexed+" File indexed, time taken: "
         +(endTime-startTime)+" ms");
   }
}

データおよびインデックスディレクトリの作成

record1.txtからrecord10.txtまでの10個のテキストファイルを使用して、学生の名前やその他の詳細を含め、E:\ Lucene \ Dataディレクトリに配置しました。 link:/lucene/data.zip [テストデータ]。 インデックスディレクトリパスは、 E:\ Lucene \ Index として作成する必要があります。 このプログラムを実行すると、そのフォルダーに作成されたインデックスファイルのリストを見ることができます。

プログラムを実行する

ソース、生データ、データディレクトリ、インデックスディレクトリの作成が完了したら、プログラムをコンパイルして実行できます。 これを行うには、 LuceneTester.Java ファイルタブをアクティブのままにして、Eclipse IDEで使用可能な実行オプションを使用するか、 Ctrl + F11 を使用して LuceneTester アプリケーションをコンパイルおよび実行します。 アプリケーションが正常に実行されると、Eclipse IDEのコンソールに次のメッセージが出力されます-

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

プログラムを正常に実行すると、* indexディレクトリ*に次のコンテンツが表示されます-

Lucene Index Directory