수색…


비고

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-06-24
6.3.0 2016-11-08

설정

Lucene은 Java 라이브러리입니다. 이미 Java 개발 환경을 설정하지 않은 경우 Java 설명서를 참조하십시오.

Apache 웹 사이트에서 Lucene의 최신 버전을 다운로드하고 압축을 풉니 다.

클래스 패스에 필요한 병을 추가하십시오. Hello World 예제를 포함하여 다음 프로젝트가 필요합니다.

  • 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