Lucene创建索引

//Lucene创建索引
package com.lunces;


import java.io.IOException;
import java.nio.file.Paths;
import java.util.Date;
 
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.NumericDocValuesField;
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;
 
public class First {
 
    public static void main(String[] args) throws IOException {
        long startTime = new Date().getTime();
        /*
         * Analyzer:建立索引时使用的分析器 主要子类有StandardAnalyzer(一个汉字一个词)
         * 还可以由第三方提供如开源社区提供一些中文分词器
         */
        Analyzer analyzer = new StandardAnalyzer();
        /*
         * Directory:代表索引文档的存储位置 这是一个抽象类有FSDirectory和RAMDirectory两个主要子类
         * 前者将索引写入文件系统,后者将索引文档写入内存
         */
        Directory dir = FSDirectory.open(Paths.get("E:\\LuceneIndex"));
        // 操作索引库的配置信息
        IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
        // 建立索引的核心类,用来操作索引(增、删、改)
        IndexWriter iw = new IndexWriter(dir, iwc);
        addDoc(iw, "1", "libiao1", "张三是中国人1",20160101);
        addDoc(iw, "2", "libiao2", "李四是美国人2",20160102);
        addDoc(iw, "3", "libiao3", "王五是中国人3",20160103);
        addDoc(iw, "4", "libiao4", "马六是俄国人4",20160104);
        addDoc(iw, "5", "libiao5", "赵七是中国人5",20160105);
        addDoc(iw, "6", "qqqq", "苏八是中国人5",20160106);
        addDoc(iw, "7", "bbbb", "我是中国人5",20160107);
        iw.close();
 
        long endTime = new Date().getTime();
        System.out.println((endTime - startTime) + "s");
    }
 
    private static void addDoc(IndexWriter iw, String id, String name, String title, Integer dt) throws IOException {
        Document doc = new Document();
        /*
         * @NumericDocValuesField:存储long类型。日期与时间也可以转换为数字类型存储与筛选
         *
         * @DoubleDocValuesField:存储double类型
         *
         * @StringField:构造函数。内部调用setTokenized(false)设置不分词。一般用于国家名、作者名、id等
         *
         * @TextField:构造函数。内部调用setTokenized(true)实现分词。一般用于文档正文
         */
        doc.add(new StringField("id", id,Store.YES));
        doc.add(new StringField("name", name, Store.YES));
        doc.add(new TextField("title", title, Store.YES));
        doc.add(new StringField("dt", dt.toString(),Store.YES));//用于查询的属性
        doc.add(new NumericDocValuesField("dt", dt));//用于排序的属性
         
        iw.addDocument(doc);
    }
 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值