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的索引过程, ...
随机推荐
- CS:APP3e 深入理解计算机系统_3e MallocLab实验
详细的题目要求和资源可以到 http://csapp.cs.cmu.edu/3e/labs.html 或者 http://www.cs.cmu.edu/~./213/schedule.html 获取. ...
- python正则详解
正则表达式概述 正则表达式,又称正规表示式.正规表示法.正规表达式.规则表达式.常规表示法(英语:Regular Expression,在代码中常简写为regex.regexp或RE),是计算机科学的 ...
- iOS学习——获取iOS设备的各种信息
不管是在Android开发还是iOS开发过程中,有时候我们需要经常根据设备的一些状态或信息进行不同的设置和性能配置,例如横竖屏切换时,电池电量低时,内存不够时,网络切换时等等,我们在这时候需要进行一些 ...
- C#高级编程学习一-----------------第五章泛型
三层架构之泛型应用 概述 1.命名约定 泛型类型以T开头或就是T. 2.泛型类 2.1.创建泛型类
- 初读"Thinking in Java"读书笔记之第二章 --- 一切都是对象
用引用操纵对象 Java里一切都被视为对象,通过操纵对象的一个"引用"来操纵对象. 例如, 可以将遥控器视为引用,电视机视为对象. 创建一个引用,不一定需要有一个对象与之关联,但此 ...
- hdf5 AttributeError: 'UnImplemented' object has no attribute 'read'
问题现象:最近在用pandas分析数据时,用hdf5存储结果,当我监听不同文件时,多个进程同时写入hdf5(save到不同group)时,报hdf5 AttributeError: 'UnImplem ...
- 《JAVA程序设计与实例》记录与归纳--继承与多态
继承与多态 概念贴士: 1. 继承,即是在已经存在的类的基础上再进行扩展,从而产生新的类.已经存在的类成为父类.超类和基类,而新产生的类成为子类或派生类. 2. Java继承是使用已存在的类的定义作为 ...
- python库termcolor用法
termcolor是python中标注文本颜色的库 ANSII Color formatting for output in terminal. 利用termcolor查看log,进行代码调试,清晰标 ...
- Zabbix实战-简易教程(5)--Proxy和Agent端(源码和yum方式)
3.3.1 zabbix proxy安装(源码方式) 1.创建目录 mkdir -p /usr/local/zabbix 2.安装必要软件 yum install -y fping(若安装不成功) 或 ...
- 【JavaScript 实现当前动态时间】
实现一个简单动态的当前时间 <!doctype html> <html lang="en"> <head> <meta charset=& ...