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 ...
随机推荐
- csuoj 1354: Distinct Subsequences
这个题是计算不同子序列的和: spoj上的那个同名的题是计算不同子序列的个数: 其实都差不多: 计算不同子序列的个数使用dp的思想: 从头往后扫一遍 如果当前的元素在以前没有出现过,那么dp[i]=d ...
- HDU4523+简单
题意很简单. 一次最多多切出一条边! 其余的就没什么好说的了 import java.util.*; import java.math.*; public class Main{ public sta ...
- [JAVA]各种杂七杂八的东西......
BigInteger / BigDecimal / string 一些常用的函数: 加 add减 substract乘 multiply除 divid取余 mod / remainder (remin ...
- asp.net中GridView的CheckedUnBindCheckBox属性
1. 获取GridView中CheckBox所选行的字段,即使是在绑定了数据源的时候,也可以获取选中的CheckedUnBindCheckBox对应的各个列的字段 使用时根据实际情况适当的修改即可. ...
- Oracle系列之表空间
涉及到表的处理请参看原表结构与数据 Oracle建表插数据等等 创建表空间等等 select tablespace_name from dba_tablespaces;--dba权限用户查询数据库中 ...
- [039] 微信公众帐号开发教程第15篇-自定义菜单的view类型(访问网页)
引言及内容概要 距离写上一篇文章<自定义菜单的创建及菜单事件响应>整整过了两个月的时间,那时公众平台还没有开放view类型的菜单.在不久前,微信公众平台悄悄开放了view类型的菜单,却没有 ...
- [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.2.4
(1). The singular value decomposition leads tot eh polar decomposition: Every operator $A$ can be wr ...
- 打印grid
void PrintButtonClick(object sender, EventArgs e) { PrintPreviewDialog dlg = new PrintPreviewDialog( ...
- HDU 1518
思路:从第一个数开始搜索,将其和与边长比对,相等则计数+1,计数达到3的时候说明可以组成,因为剩下那条必与边长相等,搜索过程注意剪枝,若某个数已被加入边长则不能重复计算,应将其标记,另外应在每一层递归 ...
- 《Android View 的事件分发和滑动冲突》 —预习资料
1. 阅读书籍<Android开发艺术探索>第三章 2. 提前阅读如下技术文章: http://blog.csdn.net/singwhatiwanna/article/details/3 ...