lucene3.5建立索引和搜索的简单步骤
来源:互联网 发布:培训行业网络推广方案 编辑:程序博客网 时间:2024/05/20 09:22
public class HelloLunce {
/**
*建立索引
* @throws IOException
*/
public void buildIndex() throws IOException{
//1:建立索引存放的目录Directory
File path = new File("E:/lucene/index");
//Directory dir = new RAMDirectory();
Directory dir = FSDirectory.open(path);
//2:创建IndexWriter
IndexWriterConfig iwc= new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35));
IndexWriter iw = new IndexWriter(dir, iwc);
//3:创建Doucument对象
File pathDoc = new File("E:/lucene/document");
Document doc = null;
for(File f : pathDoc.listFiles()){
doc = new Document();
//4:为Doucument添加Filed
doc.add(new Field("content", new FileReader(f)));
doc.add(new Field("name",f.getName(),Field.Store.YES,Field.Index.NOT_ANALYZED));
doc.add(new Field("path",f.getPath(),Field.Store.YES,Index.NOT_ANALYZED));
//5:通过IndexWriter添加文档到索引中
iw.addDocument(doc);
}
iw.close();
}
public void searcher() throws IOException, ParseException{
//1:创建Directory
Directory dir = FSDirectory.open(new File("E:/lucene/index"));
//2:创建IndexReader
IndexReader reader = IndexReader.open(dir);
//3:得到IndexSearcher
IndexSearcher sercher = new IndexSearcher(reader);
//4:得到query
QueryParser parser = new QueryParser(Version.LUCENE_35, "content", new StandardAnalyzer(Version.LUCENE_35));
Query query = parser.parse("lucene");
//5:通过IndexSercher和Query得到TopDoc
TopDocs topDocs = sercher.search(query, 10);
//6:从TopDocs中取出scordoc
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for(ScoreDoc sd : scoreDocs){
//7:根据scordoc和IndexSercher得到doc
Document doc = sercher.doc(sd.doc);
//8:根据doc的得到具体信息
System.out.println("[name]"+doc.get("name")+"[path]"+doc.get("path"));
}
reader.close();
}
}
/**
*建立索引
* @throws IOException
*/
public void buildIndex() throws IOException{
//1:建立索引存放的目录Directory
File path = new File("E:/lucene/index");
//Directory dir = new RAMDirectory();
Directory dir = FSDirectory.open(path);
//2:创建IndexWriter
IndexWriterConfig iwc= new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35));
IndexWriter iw = new IndexWriter(dir, iwc);
//3:创建Doucument对象
File pathDoc = new File("E:/lucene/document");
Document doc = null;
for(File f : pathDoc.listFiles()){
doc = new Document();
//4:为Doucument添加Filed
doc.add(new Field("content", new FileReader(f)));
doc.add(new Field("name",f.getName(),Field.Store.YES,Field.Index.NOT_ANALYZED));
doc.add(new Field("path",f.getPath(),Field.Store.YES,Index.NOT_ANALYZED));
//5:通过IndexWriter添加文档到索引中
iw.addDocument(doc);
}
iw.close();
}
public void searcher() throws IOException, ParseException{
//1:创建Directory
Directory dir = FSDirectory.open(new File("E:/lucene/index"));
//2:创建IndexReader
IndexReader reader = IndexReader.open(dir);
//3:得到IndexSearcher
IndexSearcher sercher = new IndexSearcher(reader);
//4:得到query
QueryParser parser = new QueryParser(Version.LUCENE_35, "content", new StandardAnalyzer(Version.LUCENE_35));
Query query = parser.parse("lucene");
//5:通过IndexSercher和Query得到TopDoc
TopDocs topDocs = sercher.search(query, 10);
//6:从TopDocs中取出scordoc
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for(ScoreDoc sd : scoreDocs){
//7:根据scordoc和IndexSercher得到doc
Document doc = sercher.doc(sd.doc);
//8:根据doc的得到具体信息
System.out.println("[name]"+doc.get("name")+"[path]"+doc.get("path"));
}
reader.close();
}
}
- lucene3.5建立索引和搜索的简单步骤
- lucene3.5学习笔记02--创建索引和建立搜索
- Lucene3.5 之 索引和搜索简单示例
- lucene3.5建立索引和查询
- 使用lucene3.6创建索引和实现简单搜索
- 搜索技术--建立索引(lucene3.0)
- 每日一练 Lucene3.5 建立索引
- Lucene3.5 之 索引删除和更新
- Lucene3.5 之 索引删除和更新
- Lucene4.10使用教程(二):简单的索引建立和读取步骤(HelloLucene)
- Lucene4.10使用教程(二):简单的索引建立和读取步骤(HelloLucene)
- Lucene4.10使用教程(二):简单的索引建立和读取步骤(HelloLucene)(转载)
- lucene3.5更新索引
- Lucene 建立索引和搜索
- 【Lucene02】索引和搜索建立
- Lucene3.4结合IKAnalyzer3.2 建立索引
- 搜索的前提:建立索引
- Luence 课程1----索引和搜索的建立
- c++实现split
- linux下OpenCms8.0.3安装教程
- java.sql.SQLException: ORA-00911: invalid character
- 存储过程从定义开始
- 如何屏蔽自动弹出的输入法软件盘
- lucene3.5建立索引和搜索的简单步骤
- 设置对话框图标
- sqlserver出错总结
- Lua语言中pairs和ipairs的区别
- 转:创业公司Fitbit:互联网思维再造计步器
- poj2828 线段树单点更新
- use strict
- linux下GateIn3.2.0安装教程
- 如何编程发现钢筋的碰撞和相交