创建索引.

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创建索引的更多相关文章

  1. lucene创建索引简单示例

    利用空闲时间写了一个使用lucene创建索引简单示例, 1.使用maven创建的项目 2.需要用到的jar如下: 废话不多说,直接贴代码如下: 1.创建索引的类(HelloLucene): packa ...

  2. Lucene创建索引和索引的基本检索(Lucene 之 Hello World)

    Author: 百知教育 gaozhy  注:演示代码所使用jar包版本为 lucene-xxx-5.2.0.jar 一.lucene索引操作 1.创建索引代码 try { // 1. 指定索引文件存 ...

  3. lucene创建索引的几种方式(一)

    什么是索引: 根据你输入的值去找,这个值就是索引 第一种创建索引的方式: 根据文件来生成索引,如后缀为.txt等的文件 步骤: 第一步:FSDirectory.open(Paths.get(url)) ...

  4. Lucene创建索引流程

    1.创建索引流程 原始文档:互联网上的网页(爬虫或蜘蛛).数据库中的数据.磁盘上的文件 创建文档对象(非结构化数据) 文档对象中的属性不叫属性现在成为域. 每个 Document 可以有多个 Fiel ...

  5. 搜索引擎学习(二)Lucene创建索引

    PS:需要用到的jar包: 代码实现 1.工程结构 2.设置工程依赖的jar包 3.代码实现 /** * Lucene入门 * 创建索引 */ public class CreateIndex { / ...

  6. 第五步:Lucene创建索引

    package cn.lucene; import java.io.IOException; import java.nio.file.Paths; import java.util.Date; im ...

  7. Apache Lucene(全文检索引擎)—创建索引

    目录 返回目录:http://www.cnblogs.com/hanyinglong/p/5464604.html 本项目Demo已上传GitHub,欢迎大家fork下载学习:https://gith ...

  8. Lucene 4.7 --创建索引

    Lucene的最新版本和以前的语法或者类名,类规定都相差甚远 0.准备工作: 1). Lucene官方API http://lucene.apache.org/core/4_7_0/index.htm ...

  9. Lucene.net 从创建索引到搜索的代码范例

    关于Lucene.Net的介绍网上已经很多了在这里就不多介绍Lucene.Net主要分为建立索引,维护索引和搜索索引Field.Store的作用是通过全文检查就能返回对应的内容,而不必再通过id去DB ...

随机推荐

  1. 点击后退按钮回到本页面中的另一个标签页(tab)

    在使用zepto进行微信网页开发的时候,遇到一个情况,在本页面存在四个TAB栏,每点击一个栏会显示相应的内容,下图这种: 现在有一个需求是,用户点击了后退按钮,需要回到上一次点击的tab栏. 这个需求 ...

  2. NGUI----简单聊天系统一

    1:聊天背景的创建 新建一个场景-----保存场景 NGUI---->Create-----Panel 选中UIRoot,然后新建一个sprite 选择图集 效果如下图 添加一个可拖拽的功能 选 ...

  3. vue2.0 带头冲锋(打包时,小心萝卜坑)

    距离上一期,时间间距可能有点长.谁让本人处于兴奋状态,生活已经不能自理. 哈哈哈,嗯,正经一下, 在已有的经验里总结一下那些容易抓狂的坑! 起因:npm run build 打包 本地运行,你以为可以 ...

  4. ASP.NET Core 如何在运行Docker容器时指定容器外部端口

    前面我写了一系列关于持续集成的文章,最终构建出来的镜像运行之后,应该会发现每次构建运行之后端口都变了,这对于我们来说是十分不方便的,所以我们可以通过修改docker compose的配置文件来完成我们 ...

  5. [LeetCode] Maximum Average Subarray I 子数组的最大平均值

    Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...

  6. Python系列-python函数(functools)

    有一些函数内置到了functools这个模块里 partial(func, *args, **keywords) from functools import partial def add(x,y): ...

  7. Python基础学习(第一周)

    Python是一门什么语言 编译型和解释型 通俗来讲,编译型就是一次性把所有程序写的代码都转换成机器可以识别的语言(机器语言),即可执行文件.exe: 解释型就是程序每执行到某一条指令,则会有有个称之 ...

  8. 【阿里聚安全·安全周刊】互联网时代人类还有被遗忘的权利吗 | Android与中兴

    本周七个关键词:互联网时代丨中兴和Android丨安卓厂商和安全补丁丨移动支付安全丨泰国移动运营商泄密丨格式化硬盘的恶意程序丨代码签名滥用 -1-   [互联网] 互联网时代 人类还有被遗忘的权利吗 ...

  9. [NOIp 2012]国王游戏

    Description 恰逢 H 国国庆,国王邀请 n 位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右手上面分别写下一个整数,国王自己也在左.右手上各写一个整数.然后,让这 n 位大臣排成一排,国 ...

  10. HDU2222 自动机(学习中)

    题目大意: 给你很多个单词,然后给你一篇文章,问给出的单词在文章中出现的次数. 解题思路: AC自动机入门题.需要注意的就是可能有重复单词 代码如下: #include<iostream> ...