利用空闲时间写了一个使用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. jquery upgrade

    http://www.ifanybug.com/article/00125 http://zhidao.baidu.com/question/69399476.html http://www.tmtp ...

  2. Error format not a string literal and no format arguments解决方案

    原地址: http://blog.csdn.net/joeblackzqq/article/details/25985299 cData.cpp:355:30:error:format not a s ...

  3. Vases and Flowers

    hdu4614:http://acm.hdu.edu.cn/showproblem.php?pid=4614 题意:给你n个花瓶,然后有两种操作:1从a开始选择b个花瓶,放进花,输出左端点,右端点 2 ...

  4. easyui源码翻译1.32--Droppable(放置)

    前言 使用$.fn.droppable.defaults重写默认值对象.下载该插件翻译源码 源码 /** * jQuery EasyUI 1.3.2 * *翻译:lbq --放置 拉伸 */ (fun ...

  5. eCos驱动分析 之 ISR是如何与硬件中断联系起来的?

    http://keendawn.blog.163.com/blog/static/8888074320116205833478/

  6. 告别山寨数据线:USB Type-C加密认证出炉

    从去年苹果发布的MacBook首次采用USB Type-C接口开始,这一标准逐渐成为主流,许多旗舰手机慢慢地采用了这种接口.今日,非盈利机构USB开发者论坛(USB-IF)宣布了USB Type-C认 ...

  7. 在Myeclipse buildpath 加server lib

    把eclipse下的工程复制过来后,发现缺少Server Runtime.本想直接在buildpath里加lib,在Myeclipse里找了一圈,恁是没发现在哪里可以添加,虽然在preference里 ...

  8. 【DataStructure In Python】Python实现各种排序算法

    使用Python实现直接插入排序.希尔排序.简单选择排序.冒泡排序.快速排序.归并排序.基数排序. #! /usr/bin/env python # DataStructure Sort # Inse ...

  9. 【 D3.js 进阶系列 — 6.2 】 饼状图的拖拽

    本文讲解稍微复杂一些的拖拽应用,即拖拽饼图的各部分. 在[入门 - 第 9.1 章]讲解了如何制作饼状图.饼状图的各部分是用具有宽度的弧线来表示的.在与用户进行交互的时候,如果每一部分都能拖拽,是很有 ...

  10. Android命名规范(自定义)

    此规范参考自android源码,并加以改进,仅供个人使用,如果你觉得还有可取之处,可以参考下,以后有好的规则,再补充 总体规范: 类名要清晰,能反映出这个类的作用,最好能达到见名知义的效果 方法名要使 ...