lucene创建索引简单示例
利用空闲时间写了一个使用lucene创建索引简单示例,
1.使用maven创建的项目
2.需要用到的jar如下:

废话不多说,直接贴代码如下:
1.创建索引的类(HelloLucene):
package test.lucene;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
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.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import java.io.File;
import java.io.FileReader;
/**
* 创建索引类
* Created with IntelliJ IDEA.
* User: jackzhao
* Date: 14-3-13
* Time: 下午2:57
* To change this template use File | Settings | File Templates.
*/
public class HelloLucene {
/**
* 创建索引
*/
public void createIndex(){
try
{
//1.创建Directory
//在磁盘上创建索引
Directory dir= FSDirectory.open(new File("d:/lucene/TestIndex"));
//2.创建IndexWriter
IndexWriterConfig iwc=new IndexWriterConfig(Version.LUCENE_47,new StandardAnalyzer(Version.LUCENE_47));
IndexWriter writer=new IndexWriter(dir,iwc);
//3.创建Document
Document document=null;
File f=new File("d:/lucene/TestData");
for(File file:f.listFiles())
{
document=new Document();
//4.为Document添加Field对象
document.add(new StringField("filename",f.getName(), Field.Store.YES));
document.add(new StringField("path",f.getAbsolutePath(), Field.Store.YES));
document.add(new TextField("context",new FileReader(file)));
}
//5.关闭IndexWriter
writer.close();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
2.单元测试类(LuceneTest):
import org.junit.Test;
import test.lucene.HelloLucene;
/**
* 单元测试类
* Created with IntelliJ IDEA.
* User: jackzhao
* Date: 14-3-13
* Time: 下午3:15
* To change this template use File | Settings | File Templates.
*/
public class LuceneTest {
@Test
public void testLucene(){
HelloLucene hl=new HelloLucene();
hl.createIndex();
}
}
创建的索引如下图:

lucene创建索引简单示例的更多相关文章
- Lucene创建索引和索引的基本检索(Lucene 之 Hello World)
Author: 百知教育 gaozhy 注:演示代码所使用jar包版本为 lucene-xxx-5.2.0.jar 一.lucene索引操作 1.创建索引代码 try { // 1. 指定索引文件存 ...
- lucene创建索引
创建索引. 1.lucene下载. 下载地址:http://archive.apache.org/dist/lucene/java/. lucene不同版本之间有不小的差别,这里下载的是lucene ...
- lucene创建索引的几种方式(一)
什么是索引: 根据你输入的值去找,这个值就是索引 第一种创建索引的方式: 根据文件来生成索引,如后缀为.txt等的文件 步骤: 第一步:FSDirectory.open(Paths.get(url)) ...
- Lucene创建索引流程
1.创建索引流程 原始文档:互联网上的网页(爬虫或蜘蛛).数据库中的数据.磁盘上的文件 创建文档对象(非结构化数据) 文档对象中的属性不叫属性现在成为域. 每个 Document 可以有多个 Fiel ...
- 搜索引擎学习(二)Lucene创建索引
PS:需要用到的jar包: 代码实现 1.工程结构 2.设置工程依赖的jar包 3.代码实现 /** * Lucene入门 * 创建索引 */ public class CreateIndex { / ...
- TDirectory.Delete 创建删除目录简单示例
使用函数: 1.System.IOUtils.TDirectory.CreateDirectory//创建目录 2.System.IOUtils.TDirectory.Exists // ...
- 第五步:Lucene创建索引
package cn.lucene; import java.io.IOException; import java.nio.file.Paths; import java.util.Date; im ...
- mysql 创建索引和删除索引
索引的创建可以在CREATE TABLE语句中进行,也可以单独用CREATE INDEX或ALTER TABLE来给表增加索引.删除索引可以利用ALTER TABLE或DROP INDEX语句来实现. ...
- mysql索引 ->创建索引、修改索引、删除索引的命令语句
查看表中已经存在 index:show index from table_name; 创建和删除索引索引的创建可以在CREATE TABLE语句中进行,也可以单独用CREATE INDEX或ALTER ...
随机推荐
- SDUT 1646 Complicated Expressions
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1646 题意 : 话说我根本没读题,,,因为实在 ...
- Android WebView 开发详解(二)
转载请注明出处 http://blog.csdn.net/typename/article/details/39495409 powered by miechal zhao 概览: Androi ...
- new Option及用法
op.sclassnumber.options.add(new Option(secondMenuClassNumber[0]+":"+secondMenuText[0]),se ...
- BZOJ_1600_[Usaco2008_Oct]_建造栅栏_(动态规划)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1600 将长度为n的线段分成4段,长度为整数,围成面积>0的四边形,求方案数. 分析 首先 ...
- NOI2014 起床困难综合症
3668: [Noi2014]起床困难综合症 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 225 Solved: 153[Submit][Stat ...
- -_-#【Cookie】使用无 cookies 的域
cookie-free域名提高网页效率
- 浅谈 HTML5 的 DOM Storage 机制 (转)
在开发 Web 应用时,开发者有时需要在本地存储数据.当前浏览器支持 cookie 存储,但其大小有 4KB 的限制.这对于一些 Ajax 应用来说是不够的.更多的存储空间需要浏览器本身或是插件的支持 ...
- 【转】OS X Mavericks: 防止 Mac 进入睡眠 -- 不错
原文网址:https://support.apple.com/kb/PH13808?locale=zh_CN&viewlocale=zh_CN 某些 Mac 电脑将在不活跃一段时间后自动进入睡 ...
- Node.js 创建第一个应用
如果我们使用PHP来编写后端的代码时,需要Apache 或者 Nginx 的HTTP 服务器,并配上 mod_php5 模块和php-cgi. 从这个角度看,整个"接收 HTTP 请求并提供 ...
- UVa 11752 - The Super Powers 数学
请看这个说明http://blog.csdn.net/u014800748/article/details/45914353 #define _CRT_SECURE_NO_WARNINGS #incl ...