Lucene学习笔记1(V7.1)
Lucene是一个搜索类库,solr、nutch和elasticsearch都是基于Lucene。个人感觉学习高级搜索引擎应用程序之前 有必要了解Lucene。
开发环境:idea maven springboot
开始贴代码:
maven配置
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4..RELEASE</version>
</parent> <properties>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- hot swapping, disable cache for template, enable live reload -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency> <!--Lucene-->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>7.1.</version>
</dependency> <!--中文分词器,一般分词器适用于英文分词(common)-->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-analyzers-smartcn</artifactId>
<version>7.1.</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-queryparser</artifactId>
<version>7.1.</version>
</dependency> <!--检索关键字高亮显示-->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-highlighter</artifactId>
<version>7.1.</version>
</dependency>
<!--Lucene--> <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency> </dependencies> <build>
<plugins>
<!-- Package as an executable jar/war -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
辅助类
public class LuceneConstants {
public static final String CONTENTS="contents";
public static final String FILE_NAME="filename";
public static final String FILE_PATH="filepath";
public static final int MAX_SEARCH = ;
public static final String IndexDir ="E:\\Lucene\\Index";
public static final String DataDir ="E:\\Lucene\\Data";
public static final String ArticleDir ="E:\\Lucene\\Files\\article.txt";
}
调用Lucene
public class Indexer {
public void addEntity() throws IOException {
Article article = new Article();
//article.setId(1);
//article.setTitle("Lucene全文检索");
//article.setContent("Lucene是apache软件基金会4 jakarta项目组的一个子项目,是一个开放源代码的全文检索引擎工具包,但它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎,部分文本分析引擎(英文与德文两种西方语言)。");
article.setId();
article.setTitle("Solr搜索引擎");
article.setContent("Solr是基于Lucene框架的搜索莹莹程序,是一个开放源代码的全文检索引擎。");
final Path path = Paths.get(LuceneConstants.IndexDir);
Directory directory = FSDirectory.open(path);//索引存放目录 存在磁盘
//Directory RAMDirectory= new RAMDirectory();// 存在内存
Analyzer analyzer = new StandardAnalyzer();
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
//indexWriterConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
indexWriterConfig.setOpenMode(IndexWriterConfig.OpenMode.APPEND);
IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);//更新或创建索引
Document document = new Document();
document.add(new TextField("id", article.getId().toString(), Field.Store.YES));
document.add(new TextField("title", article.getTitle(), Field.Store.YES));
document.add(new TextField("content", article.getContent(), Field.Store.YES));
indexWriter.addDocument(document);
indexWriter.close();
}
public void addFile() throws IOException {
final Path path = Paths.get(LuceneConstants.IndexDir);
Directory directory = FSDirectory.open(path);
Analyzer analyzer=new StandardAnalyzer();
IndexWriterConfig indexWriterConfig=new IndexWriterConfig(analyzer);
indexWriterConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
IndexWriter indexWriter=new IndexWriter(directory,indexWriterConfig);
InputStreamReader isr = new InputStreamReader(new FileInputStream(LuceneConstants.ArticleDir), "GBK");//.txt文档,不设置格式会乱码
BufferedReader bufferedReader=new BufferedReader(isr);
String content="";
while ((content=bufferedReader.readLine())!=null){
Document document=new Document();
document.add(new TextField("content",content,Field.Store.YES) );
indexWriter.addDocument(document);
}
bufferedReader.close();
indexWriter.close();
}
public List<String> SearchFiles() throws IOException, ParseException {
String queryString = "Solr";
final Path path = Paths.get(LuceneConstants.IndexDir);
Directory directory = FSDirectory.open(path);//索引存储位置
Analyzer analyzer = new StandardAnalyzer();//分析器
//单条件
//关键词解析
//QueryParser queryParser=new QueryParser("content",analyzer);
//Query query=queryParser.parse(queryString);
//多条件
Query mQuery = MultiFieldQueryParser.parse(new String[]{"Solr"},new String[]{"content"},new StandardAnalyzer());
IndexReader indexReader = DirectoryReader.open(directory);//索引阅读器
IndexSearcher indexSearcher = new IndexSearcher(indexReader);//查询
//TopDocs topDocs=indexSearcher.search(query,3);
TopDocs topDocs=indexSearcher.search(mQuery,);
long count = topDocs.totalHits;
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
List<String> list=new ArrayList<String>();
list.add(String.valueOf(count));
Integer cnt=;
for (ScoreDoc scoreDoc : scoreDocs) {
Document document = indexSearcher.doc(scoreDoc.doc);
//list.add(cnt.toString()+"-"+"相关度:"+scoreDoc.score+"-----time:"+document.get("time"));
//list.add("|||");
//list.add(cnt.toString()+"-"+document.get("content"));
list.add(document.get("content"));
cnt++;
}
return list;
}
}
查看运行效果
@Controller
public class LuceneController {
@RequestMapping("/add")
public String welcomepage(Map<String, Object> model) { try {
Indexer indexer = new Indexer();
indexer.addEntity(); model.put("message", "Success");
} catch (IOException ex) {
model.put("message", "Failure");
} return "welcome";
} @RequestMapping("/file")
public String fileindex(Map<String, Object> model) { try {
Indexer indexer = new Indexer();
indexer.addFile(); model.put("message", "SuccessF");
} catch (IOException ex) {
model.put("message", "FailureF");
} return "welcome";
} @RequestMapping("/search")
public String searchindex(Map<String, Object> model) { try {
Indexer indexer = new Indexer();
List<String> rlts = indexer.SearchFiles();
String message = "";
for (String str : rlts) {
message += str + " ";
}
model.put("message", message);
} catch (Exception ex) {
model.put("message", "FailureF");
} return "welcome";
} }

Lucene学习笔记1(V7.1)的更多相关文章
- Lucene学习笔记(更新)
1.Lucene学习笔记 http://www.cnblogs.com/hanganglin/articles/3453415.html
- Lucene学习笔记2-Lucene的CRUD(V7.1)
在进行CRUD的时候请注意IndexWriterConfig的设置. public class IndexCRUD { "}; private String citys[]={"j ...
- Apache Lucene学习笔记
Hadoop概述 Apache lucene: 全球第一个开源的全文检索引擎工具包 完整的查询引擎和搜索引擎 部分文本分析引擎 开发人员在此基础建立完整的全文检索引擎 以下为转载:http://www ...
- Lucene学习笔记
师兄推荐我学习Lucene这门技术,用了两天时间,大概整理了一下相关知识点. 一.什么是Lucene Lucene即全文检索.全文检索是计算机程序通过扫描文章中的每一个词,对每一个词建立一个索引,指明 ...
- Lucene学习笔记: 四,Lucene索引过程分析
对于Lucene的索引过程,除了将词(Term)写入倒排表并最终写入Lucene的索引文件外,还包括分词(Analyzer)和合并段(merge segments)的过程,本次不包括这两部分,将在以后 ...
- Solr学习笔记1(V7.2)
下载压缩包http://archive.apache.org/dist/lucene/,解压后放到某一盘符下面 Windows下启动命令 :\solr-7.2.0>bin\solr.cmd st ...
- Lucene学习笔记:基础
Lucence是Apache的一个全文检索引擎工具包.可以将采集的数据存储到索引库中,然后在根据查询条件从索引库中取出结果.索引库可以存在内存中或者存在硬盘上. 本文主要是参考了这篇博客进行学习的,原 ...
- Lucene学习笔记: 五,Lucene搜索过程解析
一.Lucene搜索过程总论 搜索的过程总的来说就是将词典及倒排表信息从索引中读出来,根据用户输入的查询语句合并倒排表,得到结果文档集并对文档进行打分的过程. 其可用如下图示: 总共包括以下几个过程: ...
- lucene学习笔记:三,Lucene的索引文件格式
Lucene的索引里面存了些什么,如何存放的,也即Lucene的索引文件格式,是读懂Lucene源代码的一把钥匙. 当我们真正进入到Lucene源代码之中的时候,我们会发现: Lucene的索引过程, ...
随机推荐
- 在Windows上运行Linux
在Windows上运行Linux 之前了解过一些适用于linux的Windows子系统,最近又听人提起,于是在自己的Windows 10专业版上安装了一个Ubuntu.运行起来还真方便,以后在wind ...
- MobaXterm
MobaXterm又名MobaXVT,是一款增强型终端.X服务器和Unix命令集(GNU/ Cygwin)封装在一个单一的便携式exe文件.MobaXterm可以开启多个终端视窗,以最新的X服务器为基 ...
- robotframework的学习笔记(十二)------DatabaseLibrary 库
1.安装DatabaseLibrary库 DatabaseLibrary 下载地址:https://pypi.python.org/pypi/robotframework-databaselibrar ...
- form表单样式
<BODY> <div id="modify-data"> <form class="modify-data-form"> ...
- 15.5 自学Zabbix之路15.5 Zabbix数据库表结构简单解析-其他 表
点击返回:自学Zabbix之路 自学Zabbix之路15.5 Zabbix数据库表结构简单解析-其他 表 1. Actions表 actions表记录了当触发器触发时,需要采用的动作. 2.Aler ...
- Java关于BufferedWriter.newline()换行的注意事项
最近项目中需要导出文件,其实导出文件是一个挺简单的事情.但是却遇到了很奇怪的问题. 首先导出到文件需要用到 BufferedWriter.而换行则是通过 bw.newline() 方法,问题将出在 n ...
- dict-命令行下中英文翻译工具
命令行下中英文翻译工具(Chinese and English translation tools in the command line) 安装(Install) ubuntu 安装 pip sud ...
- 解决Fiddler出现无线弹框 "creation of the root certificate was not successful”的问题
打开Fiddler 访问:http://localhost:8888/ 安装证书,如果提示没有证书(8888是你在Tool->Fiddler Option->Connections设置的F ...
- Oracle02——oracle分页、子查询、集合运算、处理数据、创建和管理表和其他数据库对象
作者: kent鹏 转载请注明出处: http://www.cnblogs.com/xieyupeng/p/7289451.html --oracle分页(Pageing Query) select ...
- spring官方学习地址
1.http://projects.spring.io/spring-framework/ 2.https://github.com/spring-projects/spring-mvc-showca ...