Lucene-01:创建索引

我们在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下出现了一些文件:

时间: 2024-08-02 01:43:50

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

lucene学习-创建索引

本文的lucene是基于lucene3.5版本. 使用lucene实现搜索引擎开发,核心的部分是建立索引和搜索.本节主要是记录创建索引部分的内容. 创建的索引结构如图所示. 创建索引的步骤分为以下几个步骤: 1.建立索引器IndexWriter 2.创建文档对象Document 3.建立信息对象字段Field 4.将Field对象添加到Document 5.将Document对象添加到IndexWriter对象中 下面简要介绍几个核心对象. (1).创建IndexWriter对象. IndexW

lucene之创建索引代码

public void createIndex() throws IOException { // 第一步采集数据:(jdbc采集数据) BookDao dao = new BookDaoImpl(); List<Book> queryBookList = dao.queryBookList(); // 将数据采集放到docment对象中 Document doc = null; List<Document> docList = new ArrayList<>(); f

lucene入门创建索引——(一)

1.程序宏观结构图 2.创建索引过程 3.代码实现 创建索引库: 1)  创建JavaBean对象 2)  创建Docment对象 3)  将JavaBean对象所有的属性值,均放到Document对象中去,属性名可以和JavaBean相同或不同 4)  创建IndexWriter对象 5)  将Document对象通过IndexWriter对象写入索引库中 6)  关闭IndexWriter对象 Jar包: 代码: 1 // 创建索引 2 @Test 3 public void testInd

lucene中创建索引库

package com.hope.lucene; import org.apache.commons.io.FileUtils;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.document.TextField;import org.apache.lucene.index.IndexWriter;import org.apach

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

一.什么是Lucene? Lucene最初是由Doug Cutting开发的,2000年3月,发布第一个版本,是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎 :Lucene得名于Doug妻子的中名,同时这也她外祖母的姓;目前是Apache基金会的一个顶级项目,同时也是学习搜索引擎入门必知必会. Lucene 是一个 JAVA 搜索类库,它本身并不是一个完整的解决方案,需要额外的开发工作. 优点:成熟的解决方案,有很多的成功案例.apache 顶级项目,正在持续快速的进步.庞大而活跃的开

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

一.什么是Lucene? Lucene最初是由Doug Cutting开发的,2000年3月,发布第一个版本,是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎 :Lucene得名于Doug妻子的中名,同时这也她外祖母的姓;目前是Apache基金会的一个顶级项目,同时也是学习搜索引擎入门必知必会. Lucene 是一个 JAVA 搜索类库,它本身并不是一个完整的解决方案,需要额外的开发工作. 优点:成熟的解决方案,有很多的成功案例.apache 顶级项目,正在持续快速的进步.庞大而活跃的开

全文检索之lucene的优化篇--创建索引库

在上一篇HelloWorld的基础上,建立一个directory的包,添加一个DirectoryTest的测试类,用来根据指定的索引目录创建目录存放指引. DirectoryTest类中的代码如下,基本上就是在HelloWorld的基础上改改就可以了. 里面一共三个方法,testDirectory(),测试创建索引库;testDirectoryFSAndRAM(),结合方法1的两种创建方式,优化;testDirectoryOptimize(),在方法2个基础上,研究索引的优化创建,减少创建的索引

一、创建索引之代码开发

jar包: Lucene包: lucene-core-4.10.3.jar lucene-analyzers-common-4.10.3.jar lucene-queryparser-4.10.3.jar 其它: commons-io-2.4.jar junit-4.9.jar package com.itheima.lucene; import java.io.File; import java.io.IOException; import org.apache.commons.io.File

使用Lucene对预处理后的文档进行创建索引(可执行)

时间: 2015/3/18 杨鑫newlife 对于文档的预处理后.就要開始使用Lucene来处理相关的内容了. 这里使用的Lucene的过程例如以下: 首先要为处理对象机那里索引 二是构建查询对象 三是在索引中查找 这里的代码是处理创建索引的部分 代码: package ch2.lucenedemo.process; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import jav

Lucene 4.7 --创建索引

Lucene的最新版本和以前的语法或者类名,类规定都相差甚远 0.准备工作: 1). Lucene官方API http://lucene.apache.org/core/4_7_0/index.html 2). 我用到的常用JAR包下载:http://download.csdn.net/detail/yangxy81118/8062269 3). 所用到的jar包 lucene-analyzers-common-4.7.0.jar lucene-analyzers-smartcn-4.7.0.j