您的位置:首页 > 其它

Lucene搭建和基本使用

2016-02-19 17:23 302 查看
1.下载及安装

地址:http://lucene.apache.org/  下载src.tgz包和lucene.xxx.zip

2.查看文档构建eclipse工程

  2.1  ucene-4.6.0\docs\index.html 参考文档。点击lucene domo根据导航setting yourclasspath 。



  2.2  然后设置build path,把src包关联。打开demo

2.3   打开indexFiles,SearchFiles 的源码,参考源码自己写出例子程序。

  大概脉络是:indeFiles
中构建indexWriter(需要directory和IndexWriterConfig),然后把需要相应的Filed
写进document中,indexWriter.Add(doc)写进文件最后关掉。

SearchFiles的使用也是很简单。利用indexRead.search(query,num),然后遍历document。

       以下是快速使用的源码:

public classLuceneDemo {

public static voidindexFiles(String indexPath){
File indexFile = newFile(indexPath);
try {
Directory directory = FSDirectory.open(indexFile);
Analyzer analyzer = newStandardAnalyzer(Version.LUCENE_46);
AnalyzerikAnalyzer = new IKAnalyzer(true);//
IndexWriterConfig config = newIndexWriterConfig(Version.LUCENE_46,ikAnalyzer);
IndexWriter indexWriter = newIndexWriter(directory, config);

//FileInputStreamfis = new FileInputStream(indexFile);
Document doc = newDocument();
//Fieldfield = new StringField("path", "this is a test",Field.Store.YES);
Field field = newLongField("id", Long.valueOf(1), Field.Store.YES);
Field field2 = newTextField("title", "圣诞节日记", Field.Store.YES);
Field field3 = newTextField("content", "今天圣诞节过得很开心!因为我收到了圣诞礼物。哈哈!",Field.Store.YES);

/* Fieldfield = new LongField("id", Long.valueOf(2), Field.Store.YES);
Field field2 = newTextField("title", "平安夜",Field.Store.YES);
Field field3 = newTextField("content", "今天平安夜过得很开心!因为我收到了圣诞礼物。哈哈!", Field.Store.YES);
*/
/*Fieldfield = new LongField("id", Long.valueOf(3), Field.Store.YES);
Field field2 = newTextField("title", "圣诞节的礼物",Field.Store.YES);
Field field3 = newTextField("content", "今天早上突然发现圣诞节礼物在我的袜子里。",Field.Store.YES);
*/
doc.add(field);
doc.add(field2);
doc.add(field3);
indexWriter.addDocument(doc);
indexWriter.commit();
indexWriter.close();

} catch(Exception e) {
e.printStackTrace();
}
}
public static voidsearchFiles(String indexPath){
File indexFile = newFile(indexPath);
try {
IndexReader indexReader =DirectoryReader.open(FSDirectory.open(indexFile));
IndexSearcher indexSearcher = newIndexSearcher(indexReader);
Analyzer analyzer = newStandardAnalyzer(Version.LUCENE_46);
//QueryParserparser = new QueryParser(Version.LUCENE_40, "path", analyzer);
TermQuery query = newTermQuery(new Term("content","圣诞礼物"));

TopDocs topDocs = indexSearcher.search(query,10);
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
int hits =topDocs.totalHits;
System.out.println(hits);
for(ScoreDocsd : scoreDocs){
int docID =sd.doc;
Document document =indexSearcher.doc(docID);

System.out.println(document.get("id"));
System.out.println(document.get("title"));
System.out.println(document.get("content"));

}
} catch(Exception e) {
// TODOAuto-generated catch block
e.printStackTrace();
}
}

public static voiddelIndexFiles(String indexPath){
File indexFile = newFile(indexPath);
try {
Directory directory = FSDirectory.open(indexFile);
Analyzer analyzer = newStandardAnalyzer(Version.LUCENE_46);
Analyzer ikAnalyzer = newIKAnalyzer(true);//
IndexWriterConfig config = newIndexWriterConfig(Version.LUCENE_46,ikAnalyzer);
IndexWriter indexWriter = newIndexWriter(directory, config);

//indexWriter.deleteAll();
indexWriter.deleteDocuments(new Term("content","圣诞礼物"));
indexWriter.commit();
indexWriter.close();

} catch(Exception e) {
e.printStackTrace();
}
}
}

 

 2.4 然后在测试程序中输入路径测试程序

@Test
public void test1(){
String indexPath = "F:/lucene/myretrieve/luceneData";
LuceneDemo.indexFiles(indexPath);
//LuceneDemo.searchFiles(indexPath);
}
@Test
public void test2(){
String indexPath = "F:/lucene/myretrieve/luceneData";
//LuceneDemo.indexFiles(indexPath);
LuceneDemo.searchFiles(indexPath);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: