您的位置:首页 > 其它

Lucene入门实例

2013-08-02 10:07 309 查看
package cn.lucene.helloworld;

import java.io.File;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.junit.Test;

import cn.lucene.utils.LuceneUtil;

public class HelloWorld {

String filePath = "F:\\workspace\\Lucene\\resource\\resource.txt";
String indexPath = "F:\\workspace\\Lucene\\indexPath";

Analyzer analyzer = new StandardAnalyzer();
@Test
//建立索引
public void createIndex() throws Exception{
IndexWriter indexWriter = new IndexWriter(indexPath, analyzer,false, MaxFieldLength.LIMITED);
indexWriter.addDocument(LuceneUtil.fileToDocument(new File(filePath)));
indexWriter.close();
}

@Test
//根据关键字查询索引库
public void query() throws Exception{
String queryString = "document";
IndexSearcher indexSearcher = new IndexSearcher(indexPath);

String[] fields = {"name","content"};
QueryParser parser = new  MultiFieldQueryParser(fields, analyzer);
Query query = parser.parse(queryString);
Filter filter = null;
TopDocs topDocs = indexSearcher.search(query, filter, 1000);

for(ScoreDoc scoreDoc : topDocs.scoreDocs){
int index = scoreDoc.doc;
Document document = indexSearcher.doc(index);
LuceneUtil.printDocument(document);
}

}
}

工具类:LuceneUtil.java

package cn.lucene.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;

public class LuceneUtil {

public static Document fileToDocument(File file){
Document document = new Document();
document.add(new Field("name", file.getName(), Store.YES, Index.ANALYZED));
document.add(new Field("content", getFileContent(file), Store.YES, Index.ANALYZED));
return document;
}

public static String getFileContent(File file) {
StringBuffer sb = new StringBuffer();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String str = null;
while((str = br.readLine()) != null){
sb.append(str);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return sb.toString();
}

public static void printDocument(Document document){
System.out.println("-------------start-----------------");
System.out.println("name:" + document.get("name"));
System.out.println("content:" + document.get("content"));
System.out.println("-------------end-----------------");
}
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Lucene