サーチ…


備考

Apache Luceneは、Javaベースの全文検索ライブラリです。

バージョン

バージョン発売日
2.9.4 2010-12-03
3.0.3 2010-12-03
3.6.2 2013-01-16
4.10.4 2015-10-14
5.5.2 2016年6月24日
6.3.0 2016-11-08

セットアップ

LuceneはJavaライブラリです。すでにJava開発環境がセットアップされていない場合は、 Javaのマニュアルを参照してください。

Apache Webサイトから Luceneの最新バージョンをダウンロードし、解凍します。

クラスパスに必要なjarを追加します。 Hello Worldの例を含む多くのプロジェクトでは、次のjarファイルが必要になります。

  • core/lucene-core-6.1.0.jar :コアLuceneの機能性。
  • core/analysis/common/lucene-analyzers-common-6.1.0.jar :ユビキタスなStandardAnalyzerを含むさまざまなアナライザを提供します。
  • queryparser/lucene-queryparser-6.1.0.jar :クエリパーサーを提供します。

HelloLucene.javaコードをHelloLucene.javaます。次のコマンドでコンパイルします。

javac -classpath "core/*:queryparser/*" HelloLucene.java

そして、次のコマンドで実行してください:

java -classpath ".:core/*:queryparser/*" HelloLucene

こんにちは世界

Luceneの基本的な例は単純なインデックスを作成し、それを検索します。

メモ:RAMDirectoryはメモリ常駐インデックスを作成し、実験やテストに便利ですが、実際にはほとんどの人がインデックスをファイルシステムに格納する必要があります( FSDirectory.openを参照)。

import java.io.IOException;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.*;
import org.apache.lucene.index.*;
import org.apache.lucene.queryparser.classic.*;
import org.apache.lucene.search.*;
import org.apache.lucene.store.*;

public class HelloLucene {
    public static void main(String[] args) throws IOException, ParseException
    {
        //Create a new index and open a writer
        Directory dir = new RAMDirectory();
        Analyzer analyzer = new StandardAnalyzer();
        IndexWriterConfig config = new IndexWriterConfig(analyzer);
        IndexWriter writer = new IndexWriter(dir, config);
    
        //Create a document to index
        Document doc = new Document();
        doc.add(new TextField("text", "Hello World!", Field.Store.YES));
    
        //Index the document and close the writer
        System.out.println("Indexing document: " + doc);
        writer.addDocument(doc);
        writer.close();
    
        //Open an IndexSearcher
        IndexReader reader = DirectoryReader.open(dir);
        IndexSearcher searcher = new IndexSearcher(reader);
    
        //Create a query
        QueryParser parser = new QueryParser("text", analyzer);
        Query query = parser.parse("world");
    
        //Search for results of the query in the index
        System.out.println("Searching for: \"" + query + "\"");
        TopDocs results = searcher.search(query, 10);
        for (ScoreDoc result : results.scoreDocs) {
            Document resultDoc = searcher.doc(result.doc);
            System.out.println("score: " + result.score + 
                    " -- text: " + resultDoc.get("text"));
        }
        reader.close();
    }
}


Modified text is an extract of the original Stack Overflow Documentation
ライセンスを受けた CC BY-SA 3.0
所属していない Stack Overflow