您的位置:首页 > 其它

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

2018-01-12 16:05 946 查看
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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: