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 ...
随机推荐
- Spring MVC使用commons fileupload实现文件上传功能
通过Maven建立Spring MVC项目,引入了Spring相关jar依赖. 1.为了使用commons fileupload组件,需要在pom.xml中添加依赖: <properties&g ...
- SQL中Case的使用方法(上篇)(转)
http://www.cnblogs.com/fxgachiever/archive/2010/09/09/1822106.html Case具有两种格式.简单Case函数和Case搜索函数. --简 ...
- LINUX关闭防火墙(转载)
(1) 重启后永久性生效: 开启:chkconfig iptables on 关闭:chkconfig iptables off (2) 即时生效,重启后失效: 开启:service iptables ...
- XML和JSON 序列化以及DataTable转JSON
using System.IO; using System.Text; using System.Xml.Serialization; using System.Xml; using System.R ...
- java1.8的几大新特性(二)
七.Date APIJava 8 在包java.time下包含了一组全新的时间日期API.新的日期API和开源的Joda-Time库差不多,但又不完全一样,下面的例子展示了这组新API里最重要的一些部 ...
- phpcms 2008 /preview.php SQL注入漏洞
漏洞版本: phpcms 2008 漏洞描述: phpcms2008 是一款基于 PHP+Mysql 架构的网站内容管理系统,也是一个开源的 PHP 开发平台. phpcms 2008的preview ...
- Windows下的.NET+ Memcached安装
转载请标明出处: http://www.yaosansi.com/ 原文:http://www.yaosansi.com/post/1396.html Memcached官方:http://danga ...
- c#抓取当前电脑显示分辨率
using System.Windows.Forms; 获取屏幕分辨率 int SH = Screen.PrimaryScreen.Bounds.Height; ...
- LightOJ 1370 Bi-shoe and Phi-shoe 欧拉函数+线段树
分析:对于每个数,找到欧拉函数值大于它的,且标号最小的,预处理欧拉函数,然后按值建线段树就可以了 #include <iostream> #include <stdio.h> ...
- [Irving]Android 常用布局之RelativeLayout
RelativeLayout相对布局 相对布局 RelativeLayout 允许子元素指定它们相对于其父元素或兄弟元素的位置,这是实际布局中最常用的布局方式之一. 它灵活性大很多,当然属性也多,操作 ...