利用空闲时间写了一个使用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扩展编写示例

    1.生成描述文件,包含对函数等的定义 [chengyi@localhost php-extension]$ cat hello_cy.def string self_concat(string str ...

  2. VirtualDub - 开源视频捕捉及线性处理软件

    VirtualDub是一个开放源代码的视频捕捉及线性处理软件.它由Avery Lee编写,遵循GPL协议.VirtualDub可以通过摄像头捕捉视频. 官方网站http://virtualdub.or ...

  3. cocos-html5 Json 灵活 遍历方式 不同方式的缺陷,优点总结

    1,四种解析Json的方式:Part 1 var list1 = [1,3,4]; alert(list1[1]); var list2 = [{"name":"leam ...

  4. easyui源码翻译1.32--Form(表单)

    前言 使用$.fn.form.defaults重写默认值对象下载该插件翻译源码 form提供了各种方法来操作执行表单字段,比如:ajax提交, load, clear等等.当提交表单的时候可以调用va ...

  5. SPRING IN ACTION 第4版笔记-第五章BUILDING SPRING WEB APPLICATIONS-005-以path parameters的形式给action传参数(value=“{}”、@PathVariable)

    一 1.以path parameters的形式给action传参数 @Test public void testSpittle() throws Exception { Spittle expecte ...

  6. USB Type-C 应用面临安全性考验,USB-IF 将推动新认证机制

    USB 应用已经达到空前盛况,横跨电脑.移动设备.周边设备.影音器材等范畴,是一个极为普遍常见的界面.进入 USB Type-C 世代由于一并推动 USB-PD,过去没有严格执行的认证要求,基于安全性 ...

  7. Android Binder设计与实现 - 设计篇

    要 Binder是Android系统进程间通信(IPC)方式之一.Linux已经拥有管道,system V IPC,socket等IPC手段,却还要倚赖Binder来实现进程间通信,说明Binder具 ...

  8. leetcode面试准备:Sliding Window Maximum

    leetcode面试准备:Sliding Window Maximum 1 题目 Given an array nums, there is a sliding window of size k wh ...

  9. USACO4.12Beef McNuggets(背包+数论)

    昨天晚上写的一题 结果USACO一直挂中 今天交了下 有一点点的数论知识  背包很好想 就是不好确定上界 官方题解: 这是一个背包问题.一般使用动态规划求解. 一种具体的实现是:用一个线性表储存所有的 ...

  10. PHP 'ext/gd/gd.c' gdImageCrop整数符号错误漏洞

    漏洞版本: PHP 5.5.x 漏洞描述: CVE ID:CVE-2013-7328 PHP是一种HTML内嵌式的语言. PHP 'ext/gd/gd.c' gdImageCrop函数存在多个整数符号 ...