运行lucene所需要的JAR包
lucene-core-3.6.0.jar(核心包)
lucene-analyzers-3.6.0.jar(分词器)
lucene-highlighter-3.6.0.jar(高亮)
lucene-memory-3.6.0.jar(高亮)
public class HelloWord {
public static void createIndexFile() {
IndexWriter indexWriter=null;
try {
// 需要的分词器
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
// 创建的是哪个版本的IndexWriterConfig
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(
Version.LUCENE_36, analyzer);
// 创建系统文件----- ./ 当前路径下的
Directory directory = new SimpleFSDirectory(new File("./indexDir/"));
indexWriter = new IndexWriter(directory,indexWriterConfig);
//获取实体对象
Article article=new Article(11,"最XX的城市","XX");
//indexWriter添加索引
Document doc=new Document();
//文本中添加内容 标题 内容
/*doc.add(new Field("title","中国的首都在哪里",Store.YES,Index.ANALYZED));
doc.add(new Field("content","中国的首都在北京",Store.YES,Index.ANALYZED));*/
doc.add(new Field("id",article.getId().toString(),Store.YES,Index.ANALYZED));
doc.add(new Field("title",article.getTitle().toString(),Store.YES,Index.ANALYZED));
doc.add(new Field("content",article.getContent().toString(),Store.YES,Index.ANALYZED));
//添加到索引中去
indexWriter.addDocument(doc);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(indexWriter!=null){
try {
indexWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//如果查询是需要用到解析器,那解析器必须和创建时的解析器相同
public static void searchIndexFileResult() throws IOException {
List<Article> articles=new ArrayList<Article>();
//得到索引的目录
Directory directory = new SimpleFSDirectory(new File("./indexDir/"));
//根据目录打开一个indexReader
IndexReader indexReader=IndexReader.open(directory);
//System.out.println(indexReader.maxDoc());
//获取最小值的document对象
//Document doc=indexReader.document(0);
//获取最大值的document对象
//Document doc=indexReader.document(indexReader.maxDoc()-1);
//document对象的get(字段名称)方法获取字段的值
/*System.out.println(doc.get("id"));
System.out.println(doc.get("title"));
System.out.println(doc.get("content"));*/
int n=indexReader.maxDoc();
for(int i=0;i<n;i++){
Document doc=indexReader.document(i);
Article article=new Article();
if(doc.get("id")==null){
System.out.println("id为空");
}else{
article.setId(Integer.parseInt(doc.get("id")));
article.setTitle(doc.get("title"));
article.setContent(doc.get("content"));
articles.add(article);
}
}
for(Article article:articles){
System.out.println(article.toString());
}
}
public static void main(String[] args) throws IOException {
// 建立要索引的文件
// createIndexFile();
// 从索引文件中查询数据
searchIndexFileResult();
// 获得结果,然后交由相关应用程序处理
}
}
分享到:
相关推荐
这个是使用lucene实现全文检索的jar包 包含IkAnalyer jar这个分词器 使用非常方便
lucene3.6入门实例教程 完整代码示例,lucene入门学习很好的资料
lucene3.6 搜索例子
在网上找了实例,但是发现不能使用,只能简历索引。...lucene3.6版本,能够建立索引,能搜索。inderwriter,indexsearch. 其中包C下的helloword实例能用,其余的全是网上不能用的。直接下载 可以运行
第一章 LUCENE基础 2 1.1 索引部分的核心类 2 1.2 分词部分的核心类 2 1.3 搜索部分的核心类 2 第二章 索引建立 3 2.1 创建Directory 3 2.2 创建Writer 3 2.3 创建文档并且添加索引 4 2.4 查询索引的基本信息 5 2.5 ...
说明: 例子是根据lucene3.6写的,也可以说是直接copy别人的。 包括参考文章的代码,以及修改部分之后的代码
lucene 3.6 的入门例子 代码简洁 注释清晰 是入门只必备啊 附带了ik中文分词器 支持 停用词 扩展词等
lucene3.6 模仿百度自动补全(lucene3.6 模仿百度自动补全(lucene3.6 模仿百度自动补全(lucene3.6 模仿百度自动补全(lucene3.6 模仿百度自动补全(lucene3.6 模仿百度自动补全(lucene3.6 模仿百度自动补全
lucene-3.6.0.zip
lucene-highlighter.jar lucene-highlighter.jar
最新版linux lucene-8.10.0.tgz最新版linux lucene-8.10.0.tgz最新版linux lucene-8.10.0.tgz
NULL 博文链接:https://yuan-bin1990.iteye.com/blog/1700272
luceneTest 博文链接:https://wolftou.iteye.com/blog/148701
本文档详细介绍了lucene3.6中的索引,以及每个部分对应于硬盘下的文件夹里的哪个文件。这个根据本人多年学术及编程经验总结的
lucene3.6的src包,可以用于附加上去看相应的源代码
超全的lucene3.6学习记录,实现了3中不同方法的检索,而且都很简单,容易上手,附带工程原文件
不过千万别以为Lucene是一个象google那样的搜索引擎,Lucene甚至不是一个应用程序,它仅仅是一个工具,一个Library.你也可以把它理解为一个将索引,搜索功能封装的很好的一套简单易用的API.利用这套API你可以做很多有关...
修复IKAnalyzer2012存在的无法添加扩展的中文停用词的bug。详见:http://blog.csdn.net/kmguo/article/details/8779522
支持高版本Lucene,包括最新的Lucene7.3.1,本人亲自测试过,绝对能用!