メンテナンスLucene-インデックスライブラリ

フィールド属性ドメイン

  分析かどうか:ドメインのコンテンツは、我々はドメインの内容を照会することを提供し、ワープロであるかどうか。

  インデックスかどうか:分析や全体のフィールドフィールド値のインデックスの後の単語、インデックスのみしか検索します。

    例えば:商品名、指標の分析後の製品の導入、異なる順序番号とID番号だけでなく、将来的にクエリとしてなければならないインデックスを分析します。

  ストレージかどうか:、フィールドは、文書に格納されたフィールドDocumenr値から求めることができる文書に格納されているであろう。

    例えば:製品名、注文番号、文書のフィールドから取得された将来を格納します。標準の椎骨のかどうかストレージ:あなたはコンテンツをユーザーに表示するようにしてください。

  フィールドカテゴリ:

    StringField(たfieldName、fieldValueの、Stroe.YES / NO)Stroeが定義されたメモリは、アナライザを通過しないかに応じて、インデックスを含む文字列データ型として格納されています。

    StroeField(fieldNameに、fieldValueの)は、複数のデータ型、ノー分析、インデックスなし、それらの保存インデックスライブラリのデフォルトをサポートしています。

    LongPoint(名前、値)が分析される、インデックスを作成しますが、インデックスは、それらを保存しません。

    テキストフィールド(フィールド名、fieldValueの、Stroe.YES / NO)はStroeを保存するかどうかに応じて、インデックスが作成され、分析します。

    

インデックスを追加

コードをコピー
com.wn.Documentをパッケージ化。

輸入org.apache.lucene.document.Field; 
輸入org.apache.lucene.document.LongPoint; 
輸入org.apache.lucene.document.StoredField; 
輸入org.apache.lucene.document.TextField; 
輸入org.apache.lucene.index.IndexWriter; 
輸入org.apache.lucene.index.IndexWriterConfig; 
輸入org.apache.lucene.store.Directory。
輸入org.apache.lucene.store.FSDirectory。
輸入org.wltea.analyzer.lucene.IKAnalyzer; 

インポートのjava.io.File; 
インポートにjava.io.IOException; 

パブリッククラスAddDocument { 
    / *添加索引* / 
    パブリック静的な無効メイン(文字列[] args)はIOExceptionが{スロー 
        Directoryディレクトリ= FSDirectory.open(新しいファイル( "E:\\ Luceneの\\ TEMP \\インデックス"。)toPath()); 
        IndexWriterConfig設定=新しい新IndexWriterConfig(新新IKAnalyzer()); 
        // indexwriterを作成するには、オブジェクト
        IndexWriter indexWriterを新しい新しいIndexWriterは=(ディレクトリ、コンフィグ); 
        //ドキュメントオブジェクトの作成
        org.apache.lucene.document.Documentドキュメントを=新しい新しいorg.apache.lucene.document.Document(); 
        //別の文書が持つ異なるドメインを持つことができますドメイン文書は、同じドメインを持つことができます
        。(新しい新しいテキストフィールド(「フィールド名」、「HHH」、Field.Store.YESを))document.add 
        document.add(新しい新しいテキストフィールド(「fieldContentは」、「文書HHHの新しい内容を追加します」 、Field.Store.YES)); 
        // LongPointは、インデックス作成
        document.add(新しいLongPointを( "は、fieldSize" 、123));
        // storeFieldストアデータ
        (新StoredField( "は、fieldSize"、123))document.add。 
        //ストレージの使用上のインデックスを作成する必要はありませんがstoreFiled
        document.add(新StoredField( "fieldPath"、 "E:\\ Luceneの\\ TEMP \\ hhh.txtを")); 
        //添加文档的索引库
        indexWriter.addDocument(文書)。
        //关闭indexwriter 
        indexWriter.close(); 
    } 
}
コードをコピー

  実装の影響:

    

インデックスの削除

  1.削除すべて

コードをコピー
com.wn.Documentをパッケージ化。

輸入org.apache.lucene.index.IndexWriter; 
輸入org.apache.lucene.index.IndexWriterConfig; 
輸入org.apache.lucene.store.FSDirectory。
輸入org.wltea.analyzer.lucene.IKAnalyzer; 

インポートのjava.io.File; 
インポートにjava.io.IOException; 

パブリッククラスDeleteDocument { 
    / *删除全部* / 
    パブリック静的無効メイン(文字列[] args)はIOExceptionが{スロー
        ):IndexWriter indexWrite =新しいIndexWriter(FSDirectory.open(新しいファイル( "\\ Luceneの\\ TEMP \\指数Eを" .toPath())、新IndexWriterConfig(新IKAnalyzer())); 
        //删除索引
        indexWrite.deleteAll(); 
        //关闭资源
        indexWrite.close(); 
    } 

}
コードをコピー

    達成された結果

      

  ドメインまたはキーワードに応じて削除2

    削除する前に、

      

コードをコピー
package com.wn.Document;

import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.store.FSDirectory;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.File;
import java.io.IOException;

/*根据域和关键词删除*/
public class DeleteDanDocument {
    public static void main(String[] args)throws IOException {
        IndexWriter indexWrite=new IndexWriter(FSDirectory.open(new File("E:\\Lucene\\temp\\index").toPath()),new IndexWriterConfig(new IKAnalyzer()));
        //定义一个删除条件,定义一个查询对象
        Query query=new TermQuery(new Term("fieldName","text01.txt"));
        //删除索引
        indexWrite.deleteDocuments(query);
        //关闭资源
        indexWrite.close();
    }
}
コードをコピー

    删除后

      

索引修改

  原理是先删除和添加

  修改前的索引

    

コードをコピー
package com.wn.Document;

import org.apache.commons.io.FileUtils;
import org.apache.lucene.document.*;
import org.apache.lucene.index.*;
import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.File;
import java.io.IOException;
public class UpdateDocument {
    /*修改索引库*/
    public static void main(String[] args)throws IOException {
        IndexWriter indexWrite=new IndexWriter(FSDirectory.open(new File("E:\\Lucene\\temp\\index").toPath()),new IndexWriterConfig(new IKAnalyzer()));
        //创建文档
        Document document=new Document();
        document.add(new TextField("fieldName","hhh", Field.Store.YES));
        document.add(new StoredField("fieldPath","c://hhh.txt"));
        document.add(new LongPoint("fieldSize",456));
        document.add(new StoredField("fieldSize",456));
        document.add(new TextField("fieldContent","修改fieldName为全文检索的文档,进行文档替换,先删除掉fieldName为全文检索的两个文档,再添加一个fileName为new的新文档", Field.Store.YES));

        パラメータ条件II://パラメータ修正変更された文書の価値
        indexWrite.updateDocument(新しい用語(「フィールド名」 、「 テキスト」)、文書); 
        
        //閉じるリソースは
        、()indexWrite.close 
    } 
}を
コードをコピー

   改訂されたインデックス

     

おすすめ

転載: www.cnblogs.com/mayuan01/p/12391866.html