Lucene-01:创建索引

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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下出现了一些文件:

技术分享

 

以上是关于Lucene-01:创建索引的主要内容,如果未能解决你的问题,请参考以下文章

Lucene_Hello(示例)

Lucene初体验——Hello Word实现

从ViewPager android替换片段

Elasticsearch-PHP 索引操作

solr分布式索引实战分片配置读取:工具类configUtil.java,读取配置代码片段,配置实例

java.lang.IllegalStateException:键 f0 的片段不再存在:索引 1