Lucene(01)
我的博客园博文地址:http://www.cnblogs.com/tenglongwentian/
Lucene,最新版是Lucene6.2.1,匹配的jdk版本是1.8正式版。
这里用jdk7最后一版,所以用Lucene5.3.3。
新建一个maven项目,如果不会可以参考前面的博文,前面的博文有专门提到如何新建maven项目。
新建的maven项目:<packaging>jar</packaging>,
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-core -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>5.5.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-queryparser -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-queryparser</artifactId>
<version>5.5.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-analyzers-common -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-analyzers-common</artifactId>
<version>5.5.3</version>
</dependency>
</dependencies>
因为我用jdk7,不喜欢每次更新maven仓库都要手动调整项目的jdk版本,所以
<!-- 源码目录,插件管理等配置 -->
<build>
<finalName>Lucene</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<!-- 指定source和target的版本 -->
<!-- source 指定用哪个版本的编译器对java源码进行编译 -->
<source>1.7</source>
<!-- target 指定生成的class文件将保证和哪个版本的虚拟机进行兼容 -->
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
可以这样。
新建两个类:
Indexer
import java.io.File;
import java.io.FileReader;
import java.nio.file.Paths; import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory; public class Indexer {
private IndexWriter writer;// 写索引实例 /**
* 构造方法实例化IndexWriter
*
* @param indexDir
* @throws Exception
*/
public Indexer(String indexDir) throws Exception {
Directory dir = FSDirectory.open(Paths.get(indexDir));
Analyzer analyzer = new StandardAnalyzer();// 标准分词器
IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
writer = new IndexWriter(dir, iwc);
} /**
* 关闭写索引
*
* @throws Exception
*/
public void close() throws Exception {
writer.close();
} /**
* 索引指定目录的所有文件
*
* @param dataDir
* @throws Exception
*/
public int index(String dataDir) throws Exception {
File[] files = new File(dataDir).listFiles();
for (File f : files) {
indexFile(f);
}
return writer.numDocs();
} /**
* 索引指定文件
*
* @param f
*/
private void indexFile(File f) throws Exception {
// TODO Auto-generated method stub
System.out.println("索引文件:" + f.getCanonicalFile());
Document doc = getDocument(f);
writer.addDocument(doc);
} /**
* 获取文档,文档里在设置每个字段
*
* @param f
* @return
* @throws Exception
*/
private Document getDocument(File f) throws Exception {
// TODO Auto-generated method stub
Document doc = new Document();
doc.add(new TextField("contents", new FileReader(f)));
doc.add(new TextField("fileName", f.getName(), Field.Store.YES));
doc.add(new TextField("fullPath", f.getCanonicalPath(), Field.Store.YES));
return doc;
}
public static void main(String[] args){
String indexDir="E:\\lucene";
String dataDir="E:\\lucene\\data";
Indexer indexer = null;
int numIndexed=0;
long start=System.currentTimeMillis();
try {
indexer = new Indexer(indexDir);
numIndexed=indexer.index(dataDir);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
indexer.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
long end=System.currentTimeMillis();
System.out.println("索引:"+numIndexed+"个文件,花费了"+(end-start)+"毫秒");
}
}
String indexDir="E:\\lucene";
String dataDir="E:\\lucene\\data";
看到这里不要好奇,盘符随意,在任意盘符根目录下新建文件夹,最好英文无空格,中文未测试,然后拷贝几个txt文件到data文件夹下面,一会测试用的到。
然后运行这个类,可以看到
然后可以在lucene文件夹下看到这几个奇怪的文件,是什么后面会提到,稍安勿躁。

新建另一个类:
Searcher
import java.nio.file.Paths; import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory; public class Searcher {
public static void search(String indexDir, String q) throws Exception {
Directory dir = FSDirectory.open(Paths.get(indexDir));
IndexReader reader = DirectoryReader.open(dir);
IndexSearcher is = new IndexSearcher(reader);
Analyzer analyzer = new StandardAnalyzer();
QueryParser parse = new QueryParser("contents", analyzer);
Query query = parse.parse(q);
long start = System.currentTimeMillis();
TopDocs hits = is.search(query, 10);
long end = System.currentTimeMillis();
System.out.println("匹配" + q + ",总共花费" + (end - start) + "毫秒," + "查询到" + hits.totalHits + "个记录");
for (ScoreDoc scoreDoc : hits.scoreDocs) {
Document doc = is.doc(scoreDoc.doc);
System.out.println(doc.get("fullPath"));
}
reader.close();
} public static void main(String[] args) {
String indexDir = "E:\\lucene";
//String q = "LICENSE-2.0";
String q = "Zygmunt Saloni";
try {
search(indexDir, q);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
运行这个类,

不要把第一个类生成的几个特殊的文件删除,任性的话,试试看,会报错,如果删除运行第一个类生成的几个特殊的奇怪文件后再运行第二个类的时候会报错。
还是任性的试试看吧。

对比String q = "Zygmunt Saloni";事实证明没什么影响,因为分词了,整体切割。
加上-运行第二个类的话,结果一样,自己试试看。
转载请注明出处,谢谢。
Lucene(01)的更多相关文章
- Lucene 01 - 初步认识全文检索和Lucene
目录 1 搜索简介 1.1 搜索实现方案 1.2 数据查询方法 1.2.1 顺序扫描法 1.2.2 倒排索引法(反向索引) 1.3 搜索技术应用场景 2 Lucene简介 2.1 Lucene是什么 ...
- lucene&solr-day1
全文检索课程 Lucene&Solr(1) 1. 计划 第一天:Lucene的基础知识 1.案例分析:什么是全文检索,如何实现全文检索 2.Lucene实现全文检索的流程 a) ...
- ES 01 - Elasticsearch入门 + 基础概念学习
目录 1 Elasticsearch概述 1.1 Elasticsearch是什么 1.2 Elasticsearch的优点 1.3 Elasticsearch的相关产品 1.4 Elasticsea ...
- JAVAEE——Lucene基础:什么是全文检索、Lucene实现全文检索的流程、配置开发环境、索引库创建与管理
1. 学习计划 第一天:Lucene的基础知识 1.案例分析:什么是全文检索,如何实现全文检索 2.Lucene实现全文检索的流程 a) 创建索引 b) 查询索引 3.配置开发环境 4.创建索引库 5 ...
- 全文搜索技术—Lucene
1. 内容安排 实现一个文件的搜索功能,通过关键字搜索文件,凡是文件名或文件内容包括关键字的文件都需要找出来.还可以根据中文词语进程查询,并且支持多种条件查询. 本案例中的原始内容就是磁盘上的文件 ...
- Elasticsearch入门 + 基础概念学习
原文地址:https://www.cnblogs.com/shoufeng/p/9887327.html 目录 1 Elasticsearch概述 1.1 Elasticsearch是什么 1.2 E ...
- Lucene.Net简单例子-01
前面已经简单介绍了Lucene.Net,下面来看一个实际的例子 1.1 引用必要的bll文件.这里不再介绍(Lucene.Net PanGu PanGu.HightLight PanGu.Luc ...
- 01 lucene基础 北风网项目培训 Lucene实践课程 索引
在创建索引的过程中IndexWriter会创建多个对应的Segment,这个Segment就是对应一个实体的索引段.随着索引的创建,Segment会慢慢的变大.为了提高索引的效率,IndexWrite ...
- 01 lucene基础 北风网项目培训 Lucene实践课程 系统架构
Lucene在搜索的时候数据源可以是文件系统,数据库,web等等. Lucene的搜索是基于索引,Lucene是基于前面建立的索引之上进行搜索的. 使用Lucene就像使用普通的数据库一样. Luce ...
随机推荐
- excel怎么固定第一行
这里给大家介绍一下怎么固定表格的第一行,或者说怎么固定表格的表头. 1.我这里有一个成绩表,希望固定住其第一行. 2.选择单元格A2 注意:你只需要选择所要固定行的下一行的任一单元格即可!!! 3.然 ...
- Thinking in java中关于Exception的一道面试题.
今天看到Thinking in Java中一个关于Exception的例子:最后看到有一篇总结的比较好的文章, 这里拿来记录下, 文章地址是:http://blog.csdn.net/salerzha ...
- atitit..代码生成流程图 流程图绘制解决方案 java c#.net php v2
atitit..代码生成流程图 流程图绘制解决方案 java c#.net php v2 1.1. Markdown 推荐,就是代码和flow都不能直接使用.1 1.2. Java code2fl ...
- SQL Server Window Function 窗体函数读书笔记二 - A Detailed Look at Window Functions
这一章主要是介绍 窗体中的 Aggregate 函数, Rank 函数, Distribution 函数以及 Offset 函数. Window Aggregate 函数 Window Aggrega ...
- writing-mode改变文字书写方式
古代书写方式都是垂直方向上的,如果要实现这种效果的话,还是挺麻烦的,不过现在CSS3有一个"writing-mode"属性,它可以改变文字的书写方式. writing-mode:h ...
- 初了解JS设计模式,学习笔记
什么是设计模式. 回答这个问题,往往我们得先知道我们为什么需要设计模式,正是因为有需求才会有设计模式,难道不是吗? 我们为什么需要设计模式. 如果没有按照设计模式去写,你的代码很可能是乱无肆忌写的,也 ...
- 在SQL Server中添加供应用程序使用的帐号
在之前客户咨询案例中,很多客户应用程序连接SQL Server直接用的就是SA帐号.如果对数据库管理稍微严格一点的话,就不应该给应用程序这种权限,通常应用程序只需要进行增删改查,而很少有DDL ...
- dubbox
github源码: https://github.com/dangdangdotcom/dubbox maven中央仓: 无 获取分支 git clone -b dubbox-2.8.4 https: ...
- codeforces Soldier and Number Game(dp+素数筛选)
D. Soldier and Number Game time limit per test3 seconds memory limit per test256 megabytes inputstan ...
- hdu 2896 病毒侵袭 ac自动机
/* hdu 2896 病毒侵袭 ac自动机 从题意得知,模式串中没有重复的串出现,所以结构体中可以将last[](后缀链接)数组去掉 last[]数组主要是记录具有相同后缀模式串的末尾节点编号 .本 ...
然后可以在lucene文件夹下看到这几个奇怪的文件,是什么后面会提到,稍安勿躁。