一个lucene创建索引和查找索引的样例:

创建索引:

public class Indexer {
private IndexWriter indexWriter;
/**
* 构造器实例化indexWriter
* @throws Exception
*/
public Indexer(String indexPath) throws Exception {
Directory directory = FSDirectory.open(Paths.get(indexPath));//索引存储的位置
Analyzer analyzer = new StandardAnalyzer();//标准分析器
IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
indexWriter = new IndexWriter(directory, iwc);
}
/**
* 关闭indexWriter
* @param indexWriter
* @throws IOException
*/
public void close() throws Exception {
indexWriter.close();
}
/**
* 获取文档Document
* @throws FileNotFoundException
*/
public Document getDocumnet(File f) throws Exception {
Document doc = new Document();
doc.add(new TextField("content", new FileReader(f)));
doc.add(new TextField("tittle",f.getName(),Field.Store.YES));
doc.add(new TextField("path",f.getCanonicalPath(), Field.Store.YES));
return doc;
}
/**
* 索引当个文件
* @throws Exception
*/
public void indexFile(File f) throws Exception {
System.out.println(f.getName());
Document doc = this.getDocumnet(f);
indexWriter.addDocument(doc);
}
/**
* 索引一个目录下的所有文件
* @param filePath 目录路径
* @return 索引文件的个数
* @throws Exception
*/
public int index(String filePath) throws Exception {
File[] files = new File(filePath).listFiles();
for(File f:files) {
this.indexFile(f);
}
return indexWriter.numDocs();
}
public static void main(String[] args) {
String indexPath = "G:\\工作\\luence\\index";
String dataPath = "G:\\工作\\luence\\data";
Indexer indexer = null;
int indexNum=0;
try {
indexer = new Indexer(indexPath);
indexNum = indexer.index(dataPath);
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
indexer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("索引了"+indexNum+"个文件");
}
}

查找索引:

public class Searcher {
public static void search(String indexPath,String searchStr) throws Exception { Directory dir = FSDirectory.open(Paths.get(indexPath));
IndexReader indeReader = DirectoryReader.open(dir);
IndexSearcher indexSearch = new IndexSearcher(indeReader); Analyzer analyzer = new StandardAnalyzer();//标准分词器
QueryParser parser = new QueryParser("content", analyzer);
Query query = parser.parse(searchStr);
TopDocs td = indexSearch.search(query, 10);
for(ScoreDoc sc:td.scoreDocs) {
Document doc = indexSearch.doc(sc.doc);
System.out.println(doc.get("tittle"));
System.out.println(doc.get("path"));
}
}
public static void main(String[] args) throws Exception {
Searcher.search("G:\\工作\\luence\\index\\", "Hollywood");
}
}

lucene Hello World的更多相关文章

  1. lucene 基础知识点

    部分知识点的梳理,参考<lucene实战>及网络资料 1.基本概念 lucence 可以认为分为两大组件: 1)索引组件 a.内容获取:即将原始的内容材料,可以是数据库.网站(爬虫).文本 ...

  2. 用lucene替代mysql读库的尝试

    采用lucene对mysql中的表建索引,并替代全文检索操作. 备注:代码临时梳理很粗糙,后续修改. import java.io.File; import java.io.IOException; ...

  3. Lucene的评分(score)机制研究

    首先,需要学习Lucene的评分计算公式—— 分值计算方式为查询语句q中每个项t与文档d的匹配分值之和,当然还有权重的因素.其中每一项的意思如下表所示: 表3.5 评分公式中的因子 评分因子 描 述 ...

  4. Lucene的分析资料【转】

    Lucene 源码剖析 1 目录 2 Lucene是什么 2.1.1 强大特性 2.1.2 API组成- 2.1.3 Hello World! 2.1.4 Lucene roadmap 3 索引文件结 ...

  5. Lucene提供的条件判断查询

    第一.按词条搜索 - TermQuery query = new TermQuery(new Term("name","word1"));hits = sear ...

  6. Lucene 单域多条件查询

    在Lucene 中 BooleanClause用于表示布尔查询子句关系的类,包括:BooleanClause.Occur.MUST表示and,BooleanClause.Occur.MUST_NOT表 ...

  7. lucene自定义过滤器

    先介绍下查询与过滤的区别和联系,其实查询(各种Query)和过滤(各种Filter)之间非常相似,可以这样说只要用Query能完成的事,用过滤也都可以完成,它们之间可以相互转换,最大的区别就是使用过滤 ...

  8. lucene+IKAnalyzer实现中文纯文本检索系统

    首先IntelliJ IDEA中搭建Maven项目(web):spring+SpringMVC+Lucene+IKAnalyzer spring+SpringMVC搭建项目可以参考我的博客 整合Luc ...

  9. 全文检索解决方案(lucene工具类以及sphinx相关资料)

    介绍两种全文检索的技术. 1.  lucene+ 中文分词(IK) 关于lucene的原理,在这里可以得到很好的学习. http://www.blogjava.net/zhyiwww/archive/ ...

  10. MySQL和Lucene索引对比分析

    MySQL和Lucene都可以对数据构建索引并通过索引查询数据,一个是关系型数据库,一个是构建搜索引擎(Solr.ElasticSearch)的核心类库.两者的索引(index)有什么区别呢?以前写过 ...

随机推荐

  1. 使用OneFlow搭建神经网络

    使用OneFlow搭建神经网络 在 识别 MNIST 手写体数字 的例子中,通过 flow.layers 和 flow.nn 中提供的接口搭建了一个简单的 LeNet 网络.下面,将通过LeNet来介 ...

  2. 基于C语言文件操作的学生成绩管理系统

    原理 在一个班级学生成绩管理系统中,希望处理每个学生的学习情况信息,其中包括学生的学号.姓名.各科名称和成绩等并能使管理人员通过界面完成对学生信息的录入及对学生信息的录入及对数据的查找.浏览.插入.排 ...

  3. 【Java面试真题】剑指Offer53.2——0~n-1中缺失的数字(异或、二分两种解法)

    [Java实现]剑指Offer53.2--0~n-1中缺失的数字:面试真题,两种思路分享 前面有另一道面试题[Java实现]剑指offer53.1--在排序数组中查找数字(LeetCode34:在排序 ...

  4. git介绍及使用

    一.架构 版本库(仓库):工作区中有一个隐藏目录.git,这个目录不属于工作区,而是git的版本库,是git管理的所有内容. 暂存区:版本库中包含一个临时区域,保存下一步要提交的文件. 分支:版本库中 ...

  5. P5132 Cozy Glow之拯救小马国

    题目描述 Cozy Glow偷偷摸摸的造了一个魔法阵,这个魔法阵在吸取小马国的魔力,所以你得赶紧把它毁掉. 这个魔法阵由若干个神器组成,每个神器都有一个法力值,每两个神器之间也都有一个关联值.你要依次 ...

  6. 强烈推荐!15 个Github 顶级Java教程类开源项目

    大家好,我是 Guide 哥!今天给大家推荐 15 个新手也能看懂的 Java 教程方向的开源项目.这些项目无论是对于你学习 Java 还是准备 Java 方向的面试都非常有帮助. 正如我第一个要推荐 ...

  7. 【dp】背包问题

    01背包 呐,为什么叫它01背包呢,因为装进去就是1,不装进去就是0.所以针对每个物品就两种状态,装,不装(请允许我用这么老套的开篇,相信听过很多次背包讲解的人,大多都是这个开篇的)所以咯,我这个背包 ...

  8. Django(70)接口版本控制

    前言   在RESTful规范中,有关版本的问题,用restful规范做开放接口的时候,用户请求API,系统返回数据.但是难免在系统发展的过程中,不可避免的需要添加新的资源,或者修改现有资源.因此,改 ...

  9. 远程连接MySQL错误“plugin caching_sha2_password could not be loaded”的解决办法

    远程连接MySQL错误"plugin caching_sha2_password could not be loaded"的解决办法 问题描述: 今天在阿里云租了一个服务器,当我用 ...

  10. moment常用方法

    1.subtract方法,时间加减处理 console.log(moment().format("YYYY-MM-DD HH:mm:ss")); //当前时间 console.lo ...