利用空闲时间写了一个使用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创建索引简单示例的更多相关文章

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

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

  2. lucene创建索引

    创建索引. 1.lucene下载. 下载地址:http://archive.apache.org/dist/lucene/java/. lucene不同版本之间有不小的差别,这里下载的是lucene ...

  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. TDirectory.Delete 创建删除目录简单示例

    使用函数: 1.System.IOUtils.TDirectory.CreateDirectory//创建目录 2.System.IOUtils.TDirectory.Exists        // ...

  7. 第五步:Lucene创建索引

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

  8. mysql 创建索引和删除索引

    索引的创建可以在CREATE TABLE语句中进行,也可以单独用CREATE INDEX或ALTER TABLE来给表增加索引.删除索引可以利用ALTER TABLE或DROP INDEX语句来实现. ...

  9. mysql索引 ->创建索引、修改索引、删除索引的命令语句

    查看表中已经存在 index:show index from table_name; 创建和删除索引索引的创建可以在CREATE TABLE语句中进行,也可以单独用CREATE INDEX或ALTER ...

随机推荐

  1. ***PHP多线程pthreads 实现QQ号码爬虫

    通过空间历史浏览,爬出查看你空间的人(一般限制20人,除非开通黄钻),然后在爬出这20人的浏览记录,依次向下爬,你可以控制爬行深度.这里仅仅给出怕中代码片段,你可以进一步优化,将QQ分类存储.通过QQ ...

  2. ZOJ 3705 Applications

    点我看题目 题意 : 真是一道又臭又长的模拟题啊,不过比赛的时候没看,赛完了补的. 给你N个候选人,让你从中选M个候选人,根据四个大规则来确定每个人的分数,然后选分数前M个人的输出. 1.在MOJ上做 ...

  3. 怎样在WINDOWS下面编译LIBCURL

    我测试过,好像没OK This is a short note about building cURL with SSL support on Windows. Tools required: cUR ...

  4. ArcGIS Engine Style文件操作

    对于一个GISer来说,地图,符号这些都应该有着比别人更深刻的理解和认识,作为平台软件都会提供一套自己的符号库,符号库里面根据类别和种类进行区分,因为点,线,面的自然存在和固有属性是不肯能让你用面状符 ...

  5. leetcode面试准备:Kth Largest Element in an Array

    leetcode面试准备:Kth Largest Element in an Array 1 题目 Find the kth largest element in an unsorted array. ...

  6. 安装gem invalid date format in specification错误的解决方法

    别的不说,报错信息直接贴图: 解决方法: 1.找到你环境目录下的spec,例如:D:\Ruby187\lib\ruby\gems\1.8\specifications. 2.找到引起错误文件的gems ...

  7. 存储过程系列之存储过程sql查询存储过程的使用

    1.查询某个表被哪些存储过程(以下简称 SP)使用到 : select distinct object_name(id) from syscomments where id in (select ob ...

  8. find 日常使用

    find 查找文件 fidn 目录 参数 find ~ -type f find /log -name "root.log" find . -type f -exec ls -l ...

  9. c#反射重载方法(发现不明确的匹配)

    GetMethod(string name) 在反射重载方法时,如果调用此重载方法,会产生 发现不明确的匹配 的错误. 解决方案如下: GetMethod("MethodName" ...

  10. .net 破解的几个常用工具

    在.net 破解中我们经常会提到 Reflector\SimpleAssemblyExplorer和CFF Explore这几个工具. 我们以一个简单的确Windows Form程序为例来说说他们怎么 ...