티스토리 뷰

Apache Lucene "데모 실행 및 분석"편
GoodBug

 

 

 

 

 

I. Welcom to Lucene !

 

  Lucene 은 자바로 구현된 고성능의 풀텍스트 검색엔진입니다

  Lucene 이 제공하는 API를 사용하여 강력하고 유연한 검색기능을 어플리케이션에 손쉽게 추가할 수 있습니다

  Lucene은 2005년 2월에 Apache top-level 로 등급이 상승하였고

  서브 프로젝트인 Nutch 또한 2005년 6월에 Apache Incubator를 졸업하였습니다

 

  Lucene는 다음 3가지 software를 포함하고 있습니다

  Lucene java : Lucene의 핵심 부분으로 indexing 과 search 구현 영역입니다

  Nutch : web search application을 지원합니다 (이슈!)

                 mp3, pdf, ms 등 다양한 문서를 검색할수 있도록 제공해 주지만

                 아직까지는 0.6 버젼이네요

  Lucene4c :  Lucene의 C 기반의 검색엔진입니다 아직까진 Incubator에 있군요

 

  이번 강좌에서는 Lucene java 함 뒤벼보고 웹에다 Lucene을 달아봅시다 ^^

 

  강좌 진행 순서는 "데모I 프로그램(어플리 케이션) -> 데모II 프로그램(웹 어플리케이션) -> 데모II 프로그램 수정" 순으로 갑니다

 

  ps. 한글검색이 가능하지만 한글 형태소 분석을 아직까지는 지원하지 않기 때문에

       한글은 단순검색으로 만족해야 합니다.

       하지만 영문은 기똥차게 잘됩니다

 

 

II. Download

 

  lucene 다운로드

  http://www.apache.org/dyn/closer.cgi/jakarta/lucene/binaries/

 

  참고 사이트

  http://lucene.apache.org/

  http://lucene.apache.org/java/docs/api/index.html

  http://lucene.apache.org/java/docs/index.html

  http://today.java.net/pub/a/today/2003/11/07/QueryParserRules.html

  http://www-128.ibm.com/developerworks/library/j-lucene/

  http://www.onjava.com/pub/a/onjava/2003/03/05/lucene.html

 

 

 

III. 설치

 

  그럼 이제 Lucene에 한번 빠져 봅시다~!

  다운받은 lucene-1.4.3.zip 파일을 C:\에 압축을 풉니다

 


 

  압축을 풀면 lucene-1.4.3.jarlucene-demos-1.4.3.jar 파일을 클래스 패스에 겁니다

 

 

C:\Documents and Settings\Administrator>cd c:\

C:\>cd lu*

C:\lucene-1.4.3>set classpath=C:\lucene-1.4.3\lucene-1.4.3.jar;C:\lucene-1.4.3\lucene-demos-1.4.3.jar

 

C:\lucene-1.4.3>set
ALLUSERSPROFILE=C:\Documents and Settings\All Users
ANT_HOME=C:\java\Jeus42\lib\etc\ant
APPDATA=C:\Documents and Settings\Administrator\Application Data
CLASSPATH=C:\lucene-1.4.3\lucene-1.4.3.jar;C:\lucene-1.4.3\lucene-demos-1.4.3.jar

...

 

 

  "set" 명령으로 확인합니다

  설치 끝 ~

 

 

IV. 데모I 실행하기

 

  Lucene 데모에는 두가지 데모가 있습니다

  하나는 일반적인 인덱싱, 및 검색이고 다른 하나는 웹에서 사용하기 위한 인덱싱 및 웹검색입니다

 

1) 데모I 실행


먼저 네이버나 야후같은 웹 검색엔진을 생각해 봅시다
수도없이 많은 문서들이 어떻게 해서 그렇게 빠릴 검색될까요?
바로 검색 전처리 작업을 하기 때문입니다
예를들어 간단하게 보자면 "love"라는 단어는 A문서, B문서, C문서에 포함되어 있다라는 정보를 미리 만들어 두는 것입니다
즉 인덱스를 만들어 두는 것이지요
그리고 웹로봇들이 문서를 수집해오면 추가된 문서들에 대해 하루에 몇번씩 배치작업으로 인덱스를 추가해 주겠지요
결과적으로 "love" 검색시 인덱스 정보를 뒤져서 A문서, B문서, C문서의 결과를 보여주는 겁니다

 

lucene도 마찬가지 입니다 인덱스를 먼저 만들어 주어야 합니다
먼저 일단 데모프로그램을 이용하여 인덱스를 만들어 보고 이를 이용하여 검색해 봅시다
내부 코딩은 일단 실행 이후에 살펴봅시다 ^_^ (눈에 먼저 보여야 멀 해도 잘되죵)

 

C:\lucene-1.4.3\src\ 는 데모을 위한 자바소스 파일입니다


 

인덱스 생성하기
다음 명령으로 인덱스를 생성해 봅시다
인덱스 생성 어플리케이션은 IndexFiles.java 이며 파라미터는 인덱스를 만들 소스파일들(검색대상파일들) 입니다
여기서는 lucene의 document를 index 처리해 보겠습니다

 

C:\lucene-1.4.3>
C:\lucene-1.4.3>java org.apache.lucene.demo.IndexFiles C:\lucene-1.4.3\docs
adding C:\lucene-1.4.3\docs\api\allclasses-frame.html
adding C:\lucene-1.4.3\docs\api\allclasses-noframe.html
adding C:\lucene-1.4.3\docs\api\constant-values.html
adding C:\lucene-1.4.3\docs\api\deprecated-list.html
adding C:\lucene-1.4.3\docs\api\help-doc.html
adding C:\lucene-1.4.3\docs\api\index-all.html
adding C:\lucene-1.4.3\docs\api\index.html
adding C:\lucene-1.4.3\docs\api\org\apache\lucene\analysis\Analyzer.html
...
18500 total milliseconds
C:\lucene-1.4.3>

 

야호~ 인덱스가 생성되었습니다



C:\lucene-1.4.3\index 풀더를보면 다음 파일들이 생성된 것을 알수있습니다

 

검색하기
검색 어플리케이션은 SearchFiles.java 이며 실행 후 Query: 에 검색할 단어를 입력해 봅시다

C:\lucene-1.4.3>
C:\lucene-1.4.3>java org.apache.lucene.demo.SearchFiles
Query: lucene
Searching for: lucene
324 total matching documents
0. C:\lucene-1.4.3\docs\api\allclasses-frame.html
1. C:\lucene-1.4.3\docs\api\allclasses-noframe.html
2. C:\lucene-1.4.3\docs\api\index-all.html
3. C:\lucene-1.4.3\docs\api\overview-frame.html
4. C:\lucene-1.4.3\docs\api\overview-tree.html
5. C:\lucene-1.4.3\docs\api\org\apache\lucene\queryParser\MultiFieldQueryParser.html
6. C:\lucene-1.4.3\docs\lucene-sandbox\index.html
7. C:\lucene-1.4.3\docs\resources.html
8. C:\lucene-1.4.3\docs\api\org\apache\lucene\search\class-use\Query.html
9. C:\lucene-1.4.3\docs\api\org\apache\lucene\index\class-use\Term.html
more (y/n) ?

 

와~ 성공! 엄청 빠릅니다!!

검색결과가 10개씩 리스팅되며 다음 리스트는 y버튼을 클릭하여 조회 할수 있습니다

 

 

2) 데모I 인덱싱 코드 분석

이 데모 프로그램은 가장 기본이 되는 프로그램으로 핵심 코딩만 되어 있으니 lucene을 사용하기 위해서는 꼭 알아 두어야 합니다

 

Analyzer 선택

Analyer 는 문서를 인덱싱 하거나 검색할때 핵심이 되는 요소로서, 텍스트를 파싱할 때 사용합니다 Analyzer의 종류에는 다음 몇가지 들이 있습니다

 

SimpleAnalyzer

  non-letters 를 기준으로 문자를 파싱합니다
  non-letters는 java.lang.Character.isLetter()에 의해 정의된 것을 사용합니다
  이 Analyzer는 대부분의 유럽권 언어에 적당하며 아시아권 언어에는 terrible 이라고 되어 있군요 -_-;
  대소문자 구분 안합니다

 

StopAnalyzer

  기본적으로 SimpleAnalyzer와 같으나 StopFilter를 두어 StopWord를 제거한 후 분석합니다
  StopWord란 일반적으로 검색시 유용하지 않는 단어들을 말합니다
  (a,an,and,are,as,at,be,but,by,for,if,in,into,is,it,no,not,of,on,or,s,such,t,that,the,their,then,there,these,they,this,to,was,will,with) 역시나 대소문자 구분 안합니다

 

StandardAnalyzer 

  대부분 유렵권 언어들에 최적화 되어있는 Analyzer 입니다
  StopFilter를 사용하며 대소문자 구분 안합니다

 

WhitespaceAnalyzer 

  whitespace, 즉 공백문자를 가지고 text를 나누는 방식입니다
  가장 기본적이면서도 무식한 방식입니다
  불행히도 한글검색은 이 WhitespaceAnalyzer 만 가능하며 대소문자 구분 합니다

  (왜 이것만 대소문자 구분 하냥 -_-)

 

이밖에도 GermanAnalyzer, PerFieldAnalyzerWrapper, RussianAnalyzer등이 있으며
이름에서도 알수있듯이 그다지 한글과 친하지 않은듯 합니다 -.-;;

 

IndexWriter 생성
자 이제 Analyer를 선택했으면 IndexWriter를 생성하여 Index를 만들어봅시다

 

IndexWriter writer = new IndexWriter("index", new StandardAnalyzer(), true);

 

첫번째 파라미터는 index가 생성될 위치를 말하며
두번째 파라미터는 선택한 Analyzer를,
세번째 파라미터는 index를 초기화 하여 다시 생성할것인지 말것인지를 말합니다
즉 추가/삭제만 할 것인지(false) 새로 만들것인지(true)를 나타냅니다
문서가 많을경우 매번 새로 만들수 없으며,
또한 만약 몇개의 문서만 변경되었는데 모두 다시 인덱스를 만들수는 없기 때문입니다 (시간 상당히 걸림 --)


Index에 document 추가
다음으로 소스 디렉토리(검색대상 문서들)의 파일들을 읽어가며 Analyzer에 의해 파싱된 문서 정보를 인덱스에 추가합니다

 

writer.addDocument(FileDocument.Document(file));

 

index optimize
마지막으로 인덱싱한 정보를 하나의 파일로 merge 합니다
즉 검색에 적합하도록 파일을 하나로 합치는 겁니다

 

writer.optimize();

 

index close

사용후 받드시 close 합시다!

 

writer.close();

 

끝~ 간단하죠?

이제 전체 소스를 살펴봅시다

소스는 C:\lucene-1.4.3\src\demo\org\apache\lucene\demo 에 있습니다

 

org.apache.lucene.demo.IndexFiles.java

 

class IndexFiles {

 

  public static void main(String[] args) throws IOException {

    String usage = "java " + IndexFiles.class + " <root_directory>";
   
    // 파라미터(소스디렉토리)를 입력하지 않았다면 사용법을 출력해 줍니다
    if (args.length == 0) {
      System.err.println("Usage: " + usage);
      System.exit(1);
    }

    Date start = new Date();
    try {
   
      // 선택한 Analyzer를 이용하여 IndexWriter를 생성합니다
      IndexWriter writer = new IndexWriter("index", new StandardAnalyzer(), true);
     
      // 소스디렉토리를 디비가며 문서를 파싱하여 인덱스에 추가하는 재귀함수
      indexDocs(writer, new File(args[0]));

      // 인덱싱한 여러 segment들을 검색에 알맞도록 하나로 합치는 작업
      writer.optimize();
     
      // 반드시 close()
      writer.close();

      Date end = new Date();

      // 인덱싱 처리시간 출력
      System.out.print(end.getTime() - start.getTime());
      System.out.println(" total milliseconds");

    } catch (IOException e) {
      System.out.println(" caught a " + e.getClass() +
       "\n with message: " + e.getMessage());
    }
  }

 

  // 소스 파일들을 읽어가며 파싱하여 인덱스에 추가하는 재귀함수
  public static void indexDocs(IndexWriter writer, File file)
    throws IOException {

 

    if (file.canRead()) {
      if (file.isDirectory()) {
        String[] files = file.list();

 

        if (files != null) {
          for (int i = 0; i < files.length; i++) {
            // 자기자신을 호출
            indexDocs(writer, new File(file, files[i]));
          }
        }
      } else {
        System.out.println("adding " + file);
        try {
          // 문서를 인덱스에 추가

          // FileDocument는 해당 file의 정보를 파싱해줌
          writer.addDocument(FileDocument.Document(file));
        }
       
        // 윈도에서 temporary filese등이 access denied로  exception이 발생할 수 있음
        catch (FileNotFoundException fnfe) {
          ;
        }
      }
    }
  }
}

 

 

3) 데모I 검색 코드분석
검색은 인덱스를 만드는 코드보다 훨씬 쉽습니다


IndexSearcher 생성
실질적으로 검색을 담당할 IndexSearcher를 생성해 봅시다
파라미터로는 index가 생성되어있는 위치를 입력합니다

 

Searcher searcher = new IndexSearcher("index");

 

Query 생성
Query는 질의 문자열을 파싱해 줍니다 (즉 AND,OR,NOT,!,-등의 논리연산이나 와일드카드 *,?등을 파싱합니다)

 

Query query = QueryParser.parse(line, "contents", analyzer);

 

첫번째 파라미터는 질의를,
두번째 파라미터는 검색 필드를,
세번째 파라미터는 인덱스를 만든 Analyzer와 동일한 Analyzer를 입력해 줘야 합니다

 

검색 및 검색한 결과 저장

파싱된 쿼리를 가지고 검색하여 그 결과값을 반환 받습니다

 

Hits hits = searcher.search(query);

 

반환된 Hits 클래스는 순위가 매겨진 문서들로 검색결과를 저장하고 유지합니다

 

IndexSearcher close
검색이 끝났으면 닫아줍시다

 

searcher.close();

 

전체 소스를 살펴봅시다

 

org.apache.lucene.demo.SearchFiles.java

class SearchFiles {


  public static void main(String[] args) {
    try {


      // index 폴더를 파라미터로 넘겨 IndexSearcher를 생성합니다
      Searcher searcher = new IndexSearcher("index");
     
      // 생성된 index와 동일한 Analyzer를 생성하여 넘겨줍니다
      Analyzer analyzer = new StandardAnalyzer();

 

      // 검색어를 입력받기 위한 reader 입니다
      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));


      while (true) {
        System.out.print("Query: ");
        String line = in.readLine();
       
        if (line.length() == -1)
          break;

 

        // 입력받은 질의를 파싱합니다
        Query query = QueryParser.parse(line, "contents", analyzer);
        System.out.println("Searching for: " + query.toString("contents"));

 

        // 팡싱된 질의로 검색을 합니다       
        Hits hits = searcher.search(query);
        System.out.println(hits.length() + " total matching documents");
       
        // 검색 결과 목록을 10개씩 보여줍니다
        final int HITS_PER_PAGE = 10;
        for (int start = 0; start < hits.length(); start += HITS_PER_PAGE) {
          int end = Math.min(hits.length(), start + HITS_PER_PAGE);
          for (int i = start; i < end; i++) {

 

            // 검색 결과로 부터 문서를 가져옵니다
            Document doc = hits.doc(i);

            // 문서정보를 출력합니다

            String path = doc.get("path");
            if (path != null) {
                  System.out.println(i + ". " + path);
            } else {
                  String url = doc.get("url");
              if (url != null) {
                System.out.println(i + ". " + url);
                System.out.println("   - " + doc.get("title"));
              } else {
                System.out.println(i + ". " + "No path nor URL for this document");
              }
            }
          }

 

          if (hits.length() > end) {
            System.out.print("more (y/n) ? ");
            line = in.readLine();
            if (line.length() == 0 || line.charAt(0) == 'n')
              break;
          }
        }
      }
     
      searcher.close();
       
    } catch (Exception e) {
      System.out.println(" caught a " + e.getClass() +
         "\n with message: " + e.getMessage());
    }
  }
}

 

 

V. 질의 문법

질의 문법에 대해 알아봅시다

 

A AND B

    A와 B가 모두 포함된 문서를 검색한다

A OR B

    A혹은 B가 포함된 문서를 검색한다

A NOT B

    A는 포함되고 B는 포함되지 않는 문서를 검색한다

    A ! B, A - B와 동일하다

+A OR B

    A OR B에서 A는 받드시 포함된 문서를 검색한다

A*

    A로 시작하는 단어가 있는 문서를 검색한다

A?

    A로 시작하는 두글자의 단어가 있는 문서를 검색한다

A~

    A와 스펠링이 비슷한 글자를 지닌 단어가 있는 문서를 검색한다

(A OR B) AND C

    논리연산의 그루핑 또한 지원한다

 

AND, OR, NOT등은 반드시 대문자로 입력해야 인식됩니다

 

다음 시간에는 두번째 데모 프로그램을 실행시켜 보고 lucene을 웹 어플리케이션에 달아 봅시다~

 

=============================================

본문서는 자유롭게 배포/복사 할수 있지만

이문서의 저자에 대한 언급을 삭제하시면 안됩니다

저자 : GoodBug (unicorn@jakartaproject.com)

최초 : http://www.jakartaproject.com 

=============================================

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/03   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함