lucene创建索引
创建索引.
1.lucene下载.
下载地址:http://archive.apache.org/dist/lucene/java/.
lucene不同版本之间有不小的差别,这里下载的是lucene 4.3.
2.导入jar包
打开eclipse,新建dynamic web project.解压下载的lucene压缩包,依次找到下面几个jar包,加到/WebContent/WEB-INF/lib目录下,然后Add to Build Path:
| 包名 | 位置 |
| lucene-analyzers-common-4.3.0.jar | lucene-4.3.0/analysis/common |
| lucene-analyzers-smartcn-4.3.0.jar | lucene-4.3.0/analysis/smartcn |
| lucene-core-4.3.0.jar | lucene-4.3.0/core |
| lucene-highlighter-4.3.0.jar | lucene-4.3.0/highlighter |
| lucene-queries-4.3.0.jar | lucene-4.3.0/queries |
| lucene-queryparser-4.3.0.jar | lucene-4.3.0/queryparser |
3.创建索引
package ac.ucas.lucene;
import java.io.File;
import java.io.IOException;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.IntField;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
public class IndexCreate {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 创建标准分词器
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_43);
// 创建indexwriter配置信息
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_43, analyzer);
// 设置索引的打开方式
indexWriterConfig.setOpenMode(OpenMode.CREATE_OR_APPEND);
// 索引的存储路径
Directory directory = null;
// 索引的增删改由indexWriter创建
IndexWriter indexWriter = null;
try {
directory = FSDirectory.open(new File("/Users/yaopan/Documents/eclipseworkspace/test"));
if (indexWriter.isLocked(directory)) {//若indexWriter锁定则解锁
indexWriter.unlock(directory);
}
//实例化indexWriter
indexWriter = new IndexWriter(directory, indexWriterConfig);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Document doc1 = new Document();
//添加三个域
doc1.add(new StringField("id", "abcde", Store.YES));
doc1.add(new TextField("content", "极客学院", Store.YES));
doc1.add(new IntField("num", 1, Store.YES));
// 写入索引
try {
indexWriter.addDocument(doc1);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Document doc2 = new Document();
doc2.add(new StringField("id", "addff", Store.YES));
doc2.add(new TextField("content", "LUCENE案例", Store.YES));
doc2.add(new IntField("num", 2, Store.YES));
// 写入索引
try {
indexWriter.addDocument(doc2);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
indexWriter.commit();
indexWriter.close();
directory.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("index ceate complete!");
}
}
4.lucene分词器
{%codeblock lang:java lucene分词器 %}
package ac.ucas.lucene;
import java.io.IOException;
import java.io.StringReader;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.cjk.CJKAnalyzer;
import org.apache.lucene.analysis.core.KeywordAnalyzer;
import org.apache.lucene.analysis.core.SimpleAnalyzer;
import org.apache.lucene.analysis.core.StopAnalyzer;
import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.util.Version;
import sun.dc.pr.PRError;
public class AnalyerStudy {
private static String str = "lucene, 全文检索框架";
public static void print(Analyzer analyzer){
StringReader stringReader=new StringReader(str);
try {
TokenStream tokenStream=analyzer.tokenStream(str, stringReader);
tokenStream.reset();
CharTermAttribute term=tokenStream.getAttribute(CharTermAttribute.class);
System.out.println("分词技术:"+analyzer.getClass());
while(tokenStream.incrementToken()){
System.out.print(term.toString()+" | ");
}
System.out.println("\n");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
Analyzer analyzer=null;
//标准分词
analyzer=new StandardAnalyzer(Version.LUCENE_43);
print(analyzer);
//空格分词
analyzer =new WhitespaceAnalyzer(Version.LUCENE_43);
print(analyzer);
//简单分词
analyzer=new SimpleAnalyzer(Version.LUCENE_43);
print(analyzer);
//二分法
analyzer=new CJKAnalyzer(Version.LUCENE_43);
print(analyzer);
//关键字
analyzer=new KeywordAnalyzer();
print(analyzer);
//
analyzer=new StopAnalyzer(Version.LUCENE_43);
print(analyzer);
}
}
{% endcodeblock %}
分词结果:
分词技术:class org.apache.lucene.analysis.standard.StandardAnalyzer
lucene | 全 | 文 | 检 | 索 | 框 | 架 |
分词技术:class org.apache.lucene.analysis.core.WhitespaceAnalyzer
lucene, | 全文检索框架 |
分词技术:class org.apache.lucene.analysis.core.SimpleAnalyzer
lucene | 全文检索框架 |
分词技术:class org.apache.lucene.analysis.cjk.CJKAnalyzer
lucene | 全文 | 文检 | 检索 | 索框 | 框架 |
分词技术:class org.apache.lucene.analysis.core.KeywordAnalyzer
lucene, 全文检索框架 |
分词技术:class org.apache.lucene.analysis.core.StopAnalyzer
lucene | 全文检索框架 |
5. 使用luke打开索引
Luke是一个用于Lucene搜索引擎的,方便开发和诊断的第三方工具,它可以访问现有Lucene的索引.
luke下载地址:https://github.com/DmitryKey/luke/releases
lucene创建索引的更多相关文章
- lucene创建索引简单示例
利用空闲时间写了一个使用lucene创建索引简单示例, 1.使用maven创建的项目 2.需要用到的jar如下: 废话不多说,直接贴代码如下: 1.创建索引的类(HelloLucene): packa ...
- Lucene创建索引和索引的基本检索(Lucene 之 Hello World)
Author: 百知教育 gaozhy 注:演示代码所使用jar包版本为 lucene-xxx-5.2.0.jar 一.lucene索引操作 1.创建索引代码 try { // 1. 指定索引文件存 ...
- lucene创建索引的几种方式(一)
什么是索引: 根据你输入的值去找,这个值就是索引 第一种创建索引的方式: 根据文件来生成索引,如后缀为.txt等的文件 步骤: 第一步:FSDirectory.open(Paths.get(url)) ...
- Lucene创建索引流程
1.创建索引流程 原始文档:互联网上的网页(爬虫或蜘蛛).数据库中的数据.磁盘上的文件 创建文档对象(非结构化数据) 文档对象中的属性不叫属性现在成为域. 每个 Document 可以有多个 Fiel ...
- 搜索引擎学习(二)Lucene创建索引
PS:需要用到的jar包: 代码实现 1.工程结构 2.设置工程依赖的jar包 3.代码实现 /** * Lucene入门 * 创建索引 */ public class CreateIndex { / ...
- 第五步:Lucene创建索引
package cn.lucene; import java.io.IOException; import java.nio.file.Paths; import java.util.Date; im ...
- Apache Lucene(全文检索引擎)—创建索引
目录 返回目录:http://www.cnblogs.com/hanyinglong/p/5464604.html 本项目Demo已上传GitHub,欢迎大家fork下载学习:https://gith ...
- Lucene 4.7 --创建索引
Lucene的最新版本和以前的语法或者类名,类规定都相差甚远 0.准备工作: 1). Lucene官方API http://lucene.apache.org/core/4_7_0/index.htm ...
- Lucene.net 从创建索引到搜索的代码范例
关于Lucene.Net的介绍网上已经很多了在这里就不多介绍Lucene.Net主要分为建立索引,维护索引和搜索索引Field.Store的作用是通过全文检查就能返回对应的内容,而不必再通过id去DB ...
随机推荐
- Extensions in UWP Community Toolkit - WebViewExtensions
概述 UWP Community Toolkit Extensions 中有一个为 WebView 提供的扩展 - WebViewExtensions,本篇我们结合代码详细讲解 WebView Ext ...
- Terminal,git,vim常用命令整理以及删除本地git仓库
Terminal常用命令 目录操作 ll 打开目录列表 ls -a 打开目录下所有文件列表 cd 切换到某目录 mkdir 创建目录 rmdir 删除空目录 rm -rf 目录名字 强制删除目录 mv ...
- 学习React系列(七)——Fragments、Portals、Error Boundaries与WEB组件
React.Fragment portals Error Boundaries WEB组件 React.Fragment 想象一个场景,想把td包装为组件添加到table中去,代码如下: class ...
- 基于angularJS搭建的管理系统
前言 angularJS搭建的系统,是一年前用的技术栈,有些地方比较过时,这里只是介绍实现思路 前端架构 工程目录 项目浅析 项目依赖包配置package.json { "name" ...
- HTC Vive 叠影器无法创建设备
今天使用笔记本电脑打开SteamVR发生错误:SteamVR启动失败,"Shared IPC Compositor Connected Fail(306)",然后启动失败,在UI界 ...
- 涨薪必备Javascript,快点放进小口袋!
摘要: 嗨,新一年的招聘季,你找到更好的工作了吗?小姐姐最近刚换的工作,来总结下面试必备小技能,从this来看看javascript,让我们更深入的了解它. 前言 在JavaScript中,被吐槽最多 ...
- mysql sql语句执行时是否使用索引检查方法
在日常开发中,使用到的数据表经常都会有索引,这些索引可能是开发人员/DBA建表时创建的,也可能是在使用过程中新增的.合理的使用索引,可以加快数据库查询速度.然而,在实际开发工作中,会出现有些sql语句 ...
- C++ 前期准备
在线编译网站: http://www.dooccn.com/cpp/ 刷题: https://leetcode.com/ https://leetcode-cn.com/
- 双边滤波算法的简易实现bilateralFilter
没怎么看过双边滤波的具体思路,动手写一写,看看能不能突破一下. 最后,感觉算法还是要分开 水平 与 垂直 方向进行分别处理,才能把速度提上去. 没耐性写下去了,发上来,给大伙做个参考好了. 先上几张效 ...
- [USACO 09FEB]Fair Shuttle
Description 逛逛集市,兑兑奖品,看看节目对农夫约翰来说不算什么,可是他的奶牛们非常缺乏锻炼——如果要逛完一整天的集市,他们一定会筋疲力尽的.所以为了让 奶牛们也能愉快地逛集市,约翰准备让奶 ...