一个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. 全卷积目标检测:FCOS

    全卷积目标检测:FCOS FCOS: Fully Convolutional One-Stage Object Detection 原文链接:https://arxiv.org/abs/1904.01 ...

  2. 数据、人工智能和传感器按COVID-19新冠流感排列

    数据.人工智能和传感器按COVID-19新冠流感排列 Data, AI and sensors arrayed against COVID-19 各国政府.卫生保健专业人士和工业界争先恐后地应对Cov ...

  3. 『居善地』接口测试 — 11、接口签名sign原理

    目录 1.什么是加密以及解密? 2.加密方式的分类 (1)对称加密 (2)非对称加密 (3)总结: 3.接口签名sign原理 (1)什么是接口签名? (2)为什么需要做接口签名 (3)接口签名的实践方 ...

  4. 安全Web服务器

    https协议: 443 端口 虚拟Server0: 1.部署 网站证书(营业执照)# cd /etc/pki/tls/certs/ # wget http://classroom.example.c ...

  5. 徒手用 Go 写个 Redis 服务器(Godis)

    作者:HDT3213 今天给大家带来的开源项目是 Godis:一个用 Go 语言实现的 Redis 服务器.支持: 5 种数据结构(string.list.hash.set.sortedset) 自动 ...

  6. 从Vehicle-ReId到AI换脸,应有尽有,解你所惑

    最近在做视频搜索的技术调研,已经初步有了一些成果输出,算法准确性还可以接受,基本达到了调研的预期.现将该技术调研过程中涉及到的内容总结一篇文章分享出来,内容比较多,初看起来可能关系不大,但是如果接触面 ...

  7. 温故而知新--day5

    温故而知新--day5 ip地址 IP是英文Internet Protocol的缩写,意思是"网络之间互连的协议",也就是为计算机网络相互连接进行通信而设计的协议.当多个设备要进行 ...

  8. 关于 Windows 下 Qt 开发,这个问题必须要搞清楚!

    小伙伴们,大家好,小北师兄又来喂饭啦,从上次写完<一个例子让你秒懂 Qt Creator 编译原理>后,师兄对于 Qt 的一些环境配置有了更深的理解,这对师兄进行 Qt 的后续学习起到了很 ...

  9. Java-学习日记(Java8异步)

    今天用到的中异步操作:异步编程与异步处理数据 //里面返回其他接口服务使用CompletableFuture CompletableFuture.runAsync(()->{ driverNoR ...

  10. Golang超时机制--2秒内某个函数没被调用就认为超时

    Golang超时机制--2秒内某个函数没被调用就认为超时 需求描述 当一整套流程需要其他程序来调用函数完成时通常需要一个超时机制,防止别人程序故障不调你函数导致你的程序流程卡死 实现demo pack ...