lucene 7.2版本(之前用的4.0 的。。发现好多都变了。。)—— 创建索引(内存中)

package com.shang.lucene.index.create;

import com.shang.lucene.index.abstracts.IndexAbstract;
import com.shang.lucene.jdbc.DataBase;
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;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.*;
import org.apache.lucene.store.RAMDirectory;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class CreateRAMIndex extends IndexAbstract {

    RAMDirectory dir = new RAMDirectory();

    public static IndexReader indexReader;
    private Connection conn;
    public void createIndex() {
        try {
            System.out.println("========试试水=====");
            Analyzer analyzer = new StandardAnalyzer(); //分词器(有简体中文分词器)
            IndexWriter indexWriter = new IndexWriter(dir, new IndexWriterConfig(analyzer));
            Document doc = new Document();
            Document doc1 = new Document();
            Document doc2 = new Document();
            Document doc3 = new Document();
            FieldType fieldType = new FieldType();
            fieldType.setStored(true); // 设置为true,存储此字段
            doc.add(new Field("bookName", "白夜行", fieldType));
            doc1.add(new Field("bookName", "时间移民", fieldType));
            doc2.add(new Field("bookName", "假面饭店", fieldType));
            doc3.add(new Field("bookName", "废都", fieldType));
            indexWriter.addDocument(doc);
            indexWriter.addDocument(doc1);
            indexWriter.addDocument(doc2);
            indexWriter.addDocument(doc3);
            indexWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        CreateRAMIndex s = new CreateRAMIndex();
        s.createIndex();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值