[Lucene 7회 차] about index

2012. 10. 4. 20:53OpenSource/Lucene

반응형


2012/09/20 - [OpenSource/Lucene] - [Lecene 6회차] Welcome to New face & Analyze about Index.

6회차에서 아래와 같이
를 공부하였다.

더욱 더 오늘 파고 들어보자!^0^ good~

Directory : 루씬의 책에서는 Directory 클래스가 루씬의 색인 파일 저장을 책임 진다고 나와있다.
여러가지는 지원하지만 보통 이야기를 할 때  FSDirectory, RAMDirectory를 말한다.

특히,  FSDirectory를 많이 쓰는 편 이다. 이름에서 알 수 있듯이 RAMDirectory는 컴퓨터의 메인 메모리를 색인 저장소로 사용하게 해주는 Directory의 하위 클래스이다.

소스에서 보면

import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
이런식으로 import를 해주어야 한다.

이제 메소드들에 대해서 살펴 보자!

메소드를 살펴보기 위해 레퍼런스를 보면..
http://lucene.apache.org/core/old_versioned_docs/versions/3_5_0/api/all/index.html

static FSDirectory open(File path) 
          Creates an FSDirectory instance, trying to pick the best implementation given the current environment.
static FSDirectory open(File path, LockFactory lockFactory) 
          Just like open(File), but allows you to also specify a custom LockFactory.

위의 open 메소드를 살펴보자.
레퍼런스를 보면 " Creates an FSDirectory instance, trying to pick the best implementation given the current environment"  이러하다.

String idxPath = "D:\\IR\\index";
Directory idxDir = FSDirectory.open(new File(idxPath));
File dataDir = new File(dataPath);


위의 코드를 보면 Directory 즉, 우리가 말하는 루씬에서 색인파일의 저장을 책임진다는 Directory~!!
에다가 FSDirectory의 메소드 open을 이용하여 new File을 해준다는 것이다.(파일 open!!)

여기에서!!
책에서의 루씬 구버전에서는 아래와 같이 모두 File Class를 이용하여 idx와 data file을 만든다.
  1) File indexDir = new File(idxPath);
  2) File dataDir = new File(dataPath);

하지만 지금 보고 있는 소스코드에서는 Directory와 File로 구분되어진다.
Directory idxDir = FSDirectory.open(new File(idxPath));
File dataDir = new File(dataPath);

Why???

물론..Directory idxDir = FSDirectory.open(new File(idxPath));를 file로 해줘도 무관 할 것이다.
   1) File idxDir = new File(idxPath);
   2) numIndexed = index(idxDir, dataDir);
   3) public static int index(File idxDir, File dataDir) throws IOException {
   4) IndexWriter writer = new IndexWriter(FSDirectory.open(idxDir), conf);

위와같이..써놓으면 뭔지 잘 모를지도 모르지만..ㅋㅋ
설명을 하면, File로 받아서 index메소드를 호출을 할 때 file을 인자로 넣고,
호출되어진 메소드에서는 File로 받는다.
여기서 중요한건 => FSDirectory.open(idxDir)  이부분이다.
즉, IndexWriter가 구버전과 3.5버전이 달라진 것이다.

구버전에서는
IndexWriter writer = new IndexWriter(indexdir, new StandardAnalyzer(), true); 로 루씬책에 나와있다.
현재버전에서는
IndexWriter writer = new IndexWriter(FSDirectory.open(idxDir), conf);
와 같이 FSDirectory와 conf가 인자로 들어간다.
conf는 IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_35, new KeywordAnalyzer() );
로 표현 할 수 있다.

여기서 느낌이 팍! 오셨는지 모르겠다.

다른점은 구버전에서는 File자체를! 3.5버전에서는 FSDirectory 를 사용한다는 것이다.

조금 더 정리하지 못한게 아쉽지만..조금씩 파고 들어보자!+ㅁ+/






반응형