鍍金池/ 教程/ 大數(shù)據(jù)/ Lucene添加文檔操作
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索引過程
Lucene FuzzyQuery類
Lucene PhraseQuery類
Lucene分析

Lucene添加文檔操作

添加文件是核心的操作,索引進(jìn)程的一部分中的一個(gè)。

添加包含字段IndexWriter ,IndexWriter用于更新或創(chuàng)建索引文件。

現(xiàn)在,我們將展示一個(gè)循序漸進(jìn)的過程,以得到附加文件的理解,開始使用一個(gè)基本的例子。

文檔添加到索引。

  • 創(chuàng)建一個(gè)方法來獲取從文本文件 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)建一個(gè)IndexWriter

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

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

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

  • 初始化索引目錄,有標(biāo)準(zhǔn)的分析版本信息和其他所需/可選參數(shù)創(chuàng)建 IndexWricrter 對(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);
}

添加文件,并開始索引過程

下面的兩個(gè)是添加的文件的方式。

  • addDocument(Document) - 添加使用默認(rèn)的分析文檔(在創(chuàng)建索引時(shí),指定寫入器)

  • addDocument(Document,Analyzer) - 添加使用提供的分析文檔。

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)用程序來測(cè)試索引過程。

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

LuceneConstants.java

這個(gè)類是用來提供可應(yīng)用于示例應(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文件過濾器

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(