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);
}
地址: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);
}
相关文章推荐
- maven常用命令介绍
- 用robotframework + selenium2library做自动化测试遇到的一些问题,及解决方法。
- 如何在Eclipse下安装myeclipse插件
- ulua slua 下载地址
- QQ视差特效和ListView侧滑删除
- codevs 4633 [Mz]树链剖分练习
- JSP页面与JSP页面之间传输参数出现中文乱码的解决方案
- >/dev/null 2>&1
- Lua实现在字符之间插入指定字符
- 3143: [Hnoi2013]游走 概率与期望 高斯消元解期望方程组
- 解析Java的迭代器中的fast-fail错误检测机制
- 4000 今天碰到一个PHP配置问题
- Linux权限基础及常用命令(CentOS)
- Eclipse不能自动联想类成员的解决方法之一
- React Native For Android On Windows
- Xutils和Xutils3的基本使用
- C语言 百炼成钢17
- poj2676——Sudoku(深度搜索)
- redis3.0.0 集群安装详细步骤
- nginx+tomcat取得远程用户IP问题的解决办法