루씬 잠깐 배워두고 프로젝트를 만들자니 갑갑하다.
다음주 수요일까지 먼가 나올수 있으려나 모르겠다.
일본어 시소러스 검색 엔진 만든다고 했는데 ....으으으
책은 2005년도 출판본 루씬 인 액션. 책장에 꽂혀있는게 이것밖에 없어서 하아.
책에서 다루는 루씬 버전은 1.4.3
현재 릴리즈 버전은 3.0.0( 이거슨 엄청난 갭)
(루씬 홈페이지 : http://lucene.apache.org/)
(다운로드 URL : http://www.apache.org/dist/lucene/java/)
(JAVA DOC : http://lucene.apache.org/java/3_0_0/api/core/index.html)
(최신 버전을 다운받고 싶어서...다운받았다. 그러나..)
....그렇다면 이 책의 예제를 테스트 해보는건 어렵다. 끙...닥치고 삽질이군.
일단 프로젝트를 만든다.
환경 : Tomcat 6.0, Eclipse Galileo
내이름에 걸맞게 프로젝트명은 BlackSearch
(이거 머 프로젝트 명만 거창하네.)
File -> New -> Other 로 들어가서 Web -> Dynamic Web Project 로 프로젝트를 만든다.
프로젝트 명 적어주고 Finish
다운받은 루씬 3.0 zip 파일에서 압축을 풀어주고 보면은
lucene-core-3.0.0.jar
lucene-demos-3.0.0.jar
를 볼 수 있다. 복사해서 생성한 프로젝트의 WebContent/WEB_INF/lib 폴더에 복사한다.
데모 소스도 참조해야 하므로
루씬 압축을 풀어준 폴더에서 src/demo 폴더 안의 내용을 생성한 프로젝트의 src 폴더로 복사한다.
이클립스 창에서 프로젝트 창에 F5를 눌러서 새로고침을 한번 해준다.
다음과 같이 나온다. 맨위에 보이는 com.blackun.lucene.test 패키지는 책에 있는 예제를 따라 해보려고 만든 패키지이다.
프로젝트에 data 란 폴더를 만들어주고 아무거나 텍스트 파일을 여러개 만들어서 저장한다.
아직은 웹으로 실행할 단계는 아니므로 자바 애플릿으로 실행한다.
Run -> Run Configurations 로 창을 열어서 프로젝트와 메인 클래스를 다음과 같이 설정한다.
Arguments 탭을 눌러서 실행시 인자로 사용할 인덱스 폴더와 데이타 폴더를 다음과 같이 입력한다.

Run 을 실행하면 다음과 같이 콘솔창에 실행결과가 뜨고 프로젝트에 index 폴더가 생기면서 인덱싱된 파일들이 나타난다.
6개의 파일을 색인하는데 0.83초 걸렸다.
data에는 원본 파일이 index에는 색인 파일이.
데모하나 따라해 보는데 벌서 밥먹을 때가 되었다. 배고파....ㅠㅠ 밥먹으러 가야지.
다음주 수요일까지 먼가 나올수 있으려나 모르겠다.
일본어 시소러스 검색 엔진 만든다고 했는데 ....으으으

책에서 다루는 루씬 버전은 1.4.3
현재 릴리즈 버전은 3.0.0( 이거슨 엄청난 갭)
(루씬 홈페이지 : http://lucene.apache.org/)
(다운로드 URL : http://www.apache.org/dist/lucene/java/)
(JAVA DOC : http://lucene.apache.org/java/3_0_0/api/core/index.html)
(최신 버전을 다운받고 싶어서...다운받았다. 그러나..)
....그렇다면 이 책의 예제를 테스트 해보는건 어렵다. 끙...닥치고 삽질이군.
일단 프로젝트를 만든다.
환경 : Tomcat 6.0, Eclipse Galileo
내이름에 걸맞게 프로젝트명은 BlackSearch
(이거 머 프로젝트 명만 거창하네.)
File -> New -> Other 로 들어가서 Web -> Dynamic Web Project 로 프로젝트를 만든다.
프로젝트 명 적어주고 Finish
다운받은 루씬 3.0 zip 파일에서 압축을 풀어주고 보면은
lucene-core-3.0.0.jar
lucene-demos-3.0.0.jar
를 볼 수 있다. 복사해서 생성한 프로젝트의 WebContent/WEB_INF/lib 폴더에 복사한다.
데모 소스도 참조해야 하므로
루씬 압축을 풀어준 폴더에서 src/demo 폴더 안의 내용을 생성한 프로젝트의 src 폴더로 복사한다.
이클립스 창에서 프로젝트 창에 F5를 눌러서 새로고침을 한번 해준다.

package com.blackun.lucene.test;
import java.io.*;
import java.util.*;
import org.apache.lucene.analysis.*;
import org.apache.lucene.document.*;
import org.apache.lucene.index.*;
import org.apache.lucene.store.*;
public class Indexer {
public static void main(String[] args) throws Exception {
if(args.length !=2){
throw new Exception("Usage: java "+Indexer.class.getName()
+ " <index dir> <data dir>");
}
File indexDir = new File(args[0]);
File dataDir = new File(args[1]);
long start = new Date().getTime();
int numIndexed = index(indexDir, dataDir);
long end = new Date().getTime();
System.out.println("Indexing "+numIndexed +" files took "
+(end-start)+"milliseconds");
}
public static int index(File indexDir, File dataDir) throws IOException {
if(!dataDir.exists() || !dataDir.isDirectory()){
throw new IOException(dataDir + " does not exist or is not a directiory");
}
// 1.4.3
// IndexWriter writer = new IndexWriter(indexDir, new StandardAnalyzer(), true);
// writer.setUseCompoundFile(false);
// 3.0.0 에서는 생성자가 바뀌었다.
IndexWriter writer = new IndexWriter(FSDirectory.open(indexDir), new WhitespaceAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED);
indexDirectory(writer, dataDir);
// 1.4.3
// int numIndexed = writer.docCount();
// 3.0.0 에서는 메소드 명칭이 바뀌었다.
int numIndexed = writer.numDocs();
writer.optimize();
writer.close();
return numIndexed;
}
private static void indexDirectory(IndexWriter writer, File dir) throws IOException{
File[] files = dir.listFiles();
for(int i = 0; i<files.length; i++){
File f = files[i];
if(f.isDirectory()){
indexDirectory(writer, f);
} else if (f.getName().endsWith(".txt")){
indexFile(writer, f);
}
}
}
private static void indexFile(IndexWriter writer, File f) throws IOException{
if(f.isHidden()||!f.exists()||!f.canRead()){
return;
}
System.out.println("Indexing " + f.getCanonicalPath());
Document doc = new Document();
// 1.4.3
// doc.add(Field.Text("contents", new FileReader(f)));
// 3.0.0 에서는 static 메소드를 호출하지 않는다. 객체를 하나 만든다.
// 분석 가능, 검색 가능, 저장 가능 한 Text 형 데이타는 아래와 같이 코드를 작성한다.
doc.add(new Field("contents", new FileReader(f)));
// 1.4.3
// doc.add(Field.KeyWord("filename", f.getCanonicalPath()));
// 3.0.0 에서는 아래와 같이 KeyWord 타입의 객체를 생성한다.
doc.add(new Field("filename", f.getCanonicalPath(), Field.Store.YES, Field.Index.NOT_ANALYZED));
writer.addDocument(doc);
}
}
프로젝트에 data 란 폴더를 만들어주고 아무거나 텍스트 파일을 여러개 만들어서 저장한다.
아직은 웹으로 실행할 단계는 아니므로 자바 애플릿으로 실행한다.
Run -> Run Configurations 로 창을 열어서 프로젝트와 메인 클래스를 다음과 같이 설정한다.
Run 을 실행하면 다음과 같이 콘솔창에 실행결과가 뜨고 프로젝트에 index 폴더가 생기면서 인덱싱된 파일들이 나타난다.

데모하나 따라해 보는데 벌서 밥먹을 때가 되었다. 배고파....ㅠㅠ 밥먹으러 가야지.





최근 덧글