lucene--索引的创建

Lucene索引创建示例
本文介绍了一个使用Java和Lucene创建文本索引的具体示例。通过该示例,读者可以了解到如何定义文档字段、如何配置索引写入器以及如何将多个文档添加到索引中。此外还涉及了异常处理机制。
public class IndexUtil {

    private String[] ids = { "1", "2", "3", "4", "5", "6" };

    private String[] emails = { "aa@qq.com", "bb@sina.com", "cc@nuc.com",
            "dd@qq.com", "ee@sina.com", "ff@nuc.com" };

    private String[] contents = {
            "My father was a self-taught mandolin player.",
            "Love your parents. We are too busy growing up yet we forget that they are already growing old",
            "Age has reached the end of the beginning of a word.",
            "As we drove off from Columbia, I wanted to write a letter to you to tell you all that is on my mind.",
            "The past is gone and static. Nothing we can do will change it. ",
            "I will persist until I succeed." };

    private int[] attachs = { 3, 1, 8, 5, 2, 7 };

    private String[] names = { "zhangsan", "lisi", "wangwu", "Tom", "jack",
            "Rose" };

    private Directory directory = null;

    public IndexUtil() {
        try {
            directory = FSDirectory.open(new File("D:/lucene/index02"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void index() {
        IndexWriter writer = null;
        Document document = null;
        try {
            writer = new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
            for(int i = 0; i < ids.length; i++){
                document = new Document();
                document.add(new Field("id", ids[i], Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
                document.add(new Field("email", emails[i], Field.Store.YES, Field.Index.NOT_ANALYZED));
                document.add(new Field("content", contents[i], Field.Store.NO, Field.Index.ANALYZED));
                document.add(new Field("name", names[i], Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
                writer.addDocument(document);
            }
        } catch (CorruptIndexException e) {
            e.printStackTrace();
        } catch (LockObtainFailedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(writer != null){
                    writer.close();
                }
            } catch (CorruptIndexException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值