我们在D盘下建一个文件夹叫lucene,lucene内再建两个文件夹,一个叫example,一个叫index01。example文件夹下三个txt文件,a.txt内容为hello java,b.txt内容为hello lucene,c.txt内容为hello hadoop。

package com.amazing;

import java.io.File;
import java.io.FileReader;
import java.io.IOException; import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
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; public class HelloLucene { public void createIndex(){
IndexWriter writer = null;
try {
Directory directory = FSDirectory.open(new File("D:"+File.separator+"lucene"+File.separator+"index01"));
IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_35,new StandardAnalyzer(Version.LUCENE_35));
writer = new IndexWriter(directory,iwc);
Document doc = null;
File f = new File("D:"+File.separator+"lucene"+File.separator+"example");
for(File file:f.listFiles()){
doc = new Document();
doc.add(new Field("content",new FileReader(file)));
doc.add(new Field("filename",file.getName(),Field.Store.YES,Field.Index.NOT_ANALYZED));
doc.add(new Field("path",file.getAbsolutePath(),Field.Store.YES,Field.Index.NOT_ANALYZED));
writer.addDocument(doc);
}
} catch (IOException e) {
e.printStackTrace();
} finally{
if(writer != null){
try {
writer.close();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
} }
}

运行测试类:

package com.amazing;

import org.junit.Test;

public class TestLucene {

    @Test
public void testCreateIndex(){
HelloLucene hl = new HelloLucene();
hl.createIndex();
}
}

文件夹index01下出现了一些文件:

Lucene-01:创建索引的更多相关文章

  1. lucene简介 创建索引和搜索初步

    lucene简介 创建索引和搜索初步 一.什么是Lucene? Lucene最初是由Doug Cutting开发的,2000年3月,发布第一个版本,是一个全文检索引擎的架构,提供了完整的查询引擎和索引 ...

  2. lucene中创建索引库

    package com.hope.lucene;import org.apache.commons.io.FileUtils;import org.apache.lucene.document.Doc ...

  3. lucene学习-创建索引

    本文的lucene是基于lucene3.5版本. 使用lucene实现搜索引擎开发,核心的部分是建立索引和搜索.本节主要是记录创建索引部分的内容. 创建的索引结构如图所示. 创建索引的步骤分为以下几个 ...

  4. lucene入门创建索引——(二)

    1.程序宏观结构图

  5. 搜索引擎系列 ---lucene简介 创建索引和搜索初步

    一.什么是Lucene? Lucene最初是由Doug Cutting开发的,2000年3月,发布第一个版本,是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎 :Lucene得名于Doug妻子 ...

  6. 搜索引擎学习(二)Lucene创建索引

    PS:需要用到的jar包: 代码实现 1.工程结构 2.设置工程依赖的jar包 3.代码实现 /** * Lucene入门 * 创建索引 */ public class CreateIndex { / ...

  7. HBase中创建索引

    hbasene(https://github.com/akkumar/hbasene)是开源项目,在hbase存储上封装使用Lucene来创建索引,代码API非常简单,熟悉lucene的朋友可以很方便 ...

  8. Apache Lucene(全文检索引擎)—创建索引

    目录 返回目录:http://www.cnblogs.com/hanyinglong/p/5464604.html 本项目Demo已上传GitHub,欢迎大家fork下载学习:https://gith ...

  9. Lucene 4.7 --创建索引

    Lucene的最新版本和以前的语法或者类名,类规定都相差甚远 0.准备工作: 1). Lucene官方API http://lucene.apache.org/core/4_7_0/index.htm ...

  10. lucene创建索引简单示例

    利用空闲时间写了一个使用lucene创建索引简单示例, 1.使用maven创建的项目 2.需要用到的jar如下: 废话不多说,直接贴代码如下: 1.创建索引的类(HelloLucene): packa ...

随机推荐

  1. JDBC各种数据库连接方式

    1)连接Oracle 8/8i/9i/10g/11g(thin模式) Class.forName("oracle.JDBC.driver.OracleDriver").newIns ...

  2. 关于本地化(localization)

    关于本地化(localization) 我们都知道,如果不需要做国际化版本的App.我们只需要在info.plist 里修改CFBundleDisplayName就可以了,其实做国际化也就是在不同的国 ...

  3. python 循环语句 函数 模块

    python循环语句 while循环语法结构 当需要语句不断的重复执行时,可以使用while循环 while expression: while_suite 语句ehile_suite会被连续不断的循 ...

  4. javascript 学习笔记 三大特性

    <script type="text/javascript"> //封装 function Person (name,age,sal){ this.name=name; ...

  5. R语言︱函数使用技巧(循环、if族/for、switch、repeat、ifelse、stopifnot)

    每每以为攀得众山小,可.每每又切实来到起点,大牛们,缓缓脚步来俺笔记葩分享一下吧,please~ --------------------------- 后续加更内容: 应用一:if族有哪些成员呢?- ...

  6. dojo CsvStore简介

    dojox.data.CsvStore 作者:Jared Jurkiewicz 版本:V1.0 CsvStore是由dojo提供的简单的只读存储,并且是包含在dojox项目中的.CsvStore是读取 ...

  7. ClassLoader原理

    ClassLoader原理 JVM规范定义了两种类型的类装载器:启动内装载器 (bootstrap) 和用户自定义装载器 (user-defined class loader) . 一.    Cla ...

  8. JustMock .NET单元测试利器(一)

    1.什么是Mock? Mock一词是指模仿或者效仿,用于创建实例和静态模拟.安排和验证行为.在软件开发中提及"mock",通常理解为模拟对象.模拟对象的概念就是我们想要创建一个可以 ...

  9. jquery中ajax序列化提交form表单的几种方法。

    一,ajax主流的方法 $.ajax({ type: 'post', url: 'your url', data: $("form").serialize(), success: ...

  10. python 内置函数range和xrange

    range range 是一种类型(type),它是一个数字的序列,而且是不可变的,通常用在for循环中. class range(stop)class range(start, stop [, st ...