1. 程式人生 > >lucene 7.2版本(之前用的4.0 的。。發現好多都變了。。)—— 建立索引(記憶體中)

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();
    }
}