鍍金池/ 教程/ 大數(shù)據(jù)/ Lucene索引過(guò)程
Lucene TermRangeQuery類
Lucene排序
Lucene Query類
Lucene搜索操作
Lucene TokenStream
Lucene IndexWriter類
Lucene Term類
Lucene Token
Lucene索引操作
Lucene Field選項(xiàng)
Lucene BooleanQuery類
Lucene StandardAnalyzer類
Lucene字段
Lucene添加文檔操作
Lucene環(huán)境設(shè)置
Lucene Searching類
Lucene StopAnalyzer類
Lucene第一個(gè)應(yīng)用程序
Lucene MatchAllDocsQuery類
Lucene IndexSearcher類
Lucene索引類
Lucene更新文檔操作
Lucene教程
Lucene PrefixQuery類
Lucene Analyzer類
Lucene TopDocs類
Lucene TermQuery類
Lucene文檔
Lucene查詢編程
Lucene WildcardQuery類
Lucene WhitespaceAnalyzer
Lucene SimpleAnalyzer類
Lucene目錄
Lucene刪除文檔操作
Lucene索引過(guò)程
Lucene FuzzyQuery類
Lucene PhraseQuery類
Lucene分析

Lucene索引過(guò)程

索引過(guò)程是Lucene提供的核心功能之一。下圖說(shuō)明了索引過(guò)程和使用的類。IndexWriter是索引過(guò)程中最重要的和核心組件。

Indexing Process

添加文檔包含字段IndexWriter,該分析用分析儀分析文件,然后創(chuàng)建/根據(jù)需要并在目錄存儲(chǔ)/更新/打開(kāi)/編輯索引。IndexWriter用于更新或創(chuàng)建索引。它不是用來(lái)讀取索引。

現(xiàn)在,展示一個(gè)循序漸進(jìn)的過(guò)程,以獲得在索引過(guò)程的理解,使用一個(gè)基本的例子。

創(chuàng)建一個(gè)文檔

  • 創(chuàng)建一個(gè)方法來(lái)獲取從文本文件中獲得 Lucene 的文檔。

  • 創(chuàng)建各種類型的是含有鍵作為名稱和值作為內(nèi)容被編入索引鍵值對(duì)字段。

  • 設(shè)置字段中進(jìn)行分析或不設(shè)置。在我們的實(shí)例中,只有內(nèi)容被分析,因?yàn)樗赡馨瑪?shù)據(jù),諸如 a, am, are, an,它不要求在搜索操作等等。

  • 新創(chuàng)建的字段添加到文檔對(duì)象并返回給調(diào)用者的方法。

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

創(chuàng)建IndexWriter

  • IndexWriter 類作為它創(chuàng)建/在索引過(guò)程中更新指標(biāo)的核心組成部分

  • 創(chuàng)建一個(gè) IndexWriter 對(duì)象

  • 創(chuàng)建其應(yīng)指向位置,其中索引是存儲(chǔ)一個(gè)lucene的目錄

  • 初始化索引目錄,有標(biāo)準(zhǔn)的分析版本信息和其他所需/可選參數(shù)創(chuàng)建 IndexWriter 對(duì)象

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

開(kāi)始索引過(guò)程

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

應(yīng)用程序示例

讓我們創(chuàng)建一個(gè)測(cè)試 Lucene 應(yīng)用程序來(lái)測(cè)試索引過(guò)程。

步驟 描述
1 在 packagecom.yiibai.lucene 包下創(chuàng)建一個(gè)名稱 LuceneFirstApplication 項(xiàng)目用于解釋 Lucene - First Application chapter, 也可以使用 Lucene 的創(chuàng)建項(xiàng)目 -  在 First Application 章這樣本章理解索引過(guò)程。
2 創(chuàng)建LuceneConstants.java,TextFileFilter.java和 Indexer.java,其它的文件保存不變。
3 創(chuàng)建LuceneTester.java如下所述
4 清理和構(gòu)建應(yīng)用程序,以確保業(yè)務(wù)邏輯按要求

LuceneConstants.java

這個(gè)類是用來(lái)提供跨示例應(yīng)用程序中使用的各種常量

package com.yiibai.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 文件過(guò)濾器

package com.yiibai.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"上一篇:Lucene Analyzer類下一篇:Lucene索引操作