package org.nl.config.lucene;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
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;
import org.springframework.beans.factory.annotation.Value;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Set;

/**
 * lucene索引器
 */
public class Indexer {
    /**
     * 写索引实例
     */
    private IndexWriter writer;

    public IndexWriter getWriter() {
        return writer;
    }

    /**
     * 构造方法，实例化IndexWriter
     * @param indexDir
     * @throws Exception
     */
    public Indexer(String indexDir) throws Exception {
        Directory dir = FSDirectory.open(Paths.get(indexDir));
        //标准分词器，会自动去掉空格啊，is a the等单词
//        Analyzer analyzer = new StandardAnalyzer();
        Analyzer analyzer = new IKAnalyzer();
        //将标准分词器配到写索引的配置中
        IndexWriterConfig config = new IndexWriterConfig(analyzer);
        //实例化写索引对象
        writer = new IndexWriter(dir, config);
    }

    /**
     * 索引指定目录下的所有文件
     * @param dataDir
     * @return
     * @throws Exception
     */
    public int indexAll(String dataDir) throws Exception {
        // 获取该路径下的所有文件
        File[] files = new File(dataDir).listFiles();
        if (null != files) {
            for (File file : files) {
                //调用下面的indexFile方法，对每个文件进行索引
                indexFile(file);
            }
        }
        //返回索引的文件数
//        return writer.numDocs();
        return writer.numRamDocs();
    }

    /**
     * 索引指定的文件
     * @param file
     * @throws Exception
     */
    private void indexFile(File file) throws Exception {
        System.out.println("索引文件的路径：" + file.getCanonicalPath());
        //调用下面的getDocument方法，获取该文件的document
        Document doc = getDocument(file);
        //添加索引文档
        //Document doc = json2Doc(jsonDoc);
//        Document doc = new Document();
//        doc.add(new TextField("content", jsonDoc, Field.Store.YES));
        Field fieldContent=new TextField("fieldContent", FileUtils.readFileToString(null,"UTF-8"), Field.Store.YES);

        //将doc添加到索引中
        writer.addDocument(doc);
    }

    /**
     * 获取文档，文档里再设置每个字段，就类似于数据库中的一行记录
     * @param file
     * @return
     * @throws Exception
     */
    private Document getDocument(File file) throws Exception {
        Document doc = new Document();
        //开始添加字段
        //添加内容
        doc.add(new TextField("contents", new FileReader(file)));
        //添加文件名，并把这个字段存到索引文件里
        doc.add(new TextField("fileName", file.getName(), Field.Store.YES));
        //添加文件路径
        doc.add(new TextField("fullPath", file.getCanonicalPath(), Field.Store.YES));
        return doc;
    }
    public Document json2Doc(String strDoc) {
        Document doc = new Document();
        JSONObject jsonDoc = JSONObject.parseObject(strDoc);
        Set<String> keys = jsonDoc.keySet();
        for (String key : keys) {
            doc.add(new TextField(key, jsonDoc.getString(key), Field.Store.YES));
        }
        return doc;
    }

    public void addLogIndex(String msg) throws IOException {
        //步骤一：创建Directory对象，用于指定索引库的位置    RAMDirectory内存
        Directory directory = FSDirectory.open(new File("D:\\lucene\\index").toPath());
        //步骤二：创建一个IndexWriter对象，用于写索引
//        Analyzer analyzer = new StandardAnalyzer();
        IndexWriter indexWriter=new IndexWriter(directory,new IndexWriterConfig(new IKAnalyzer(false)));
//        indexWriter.deleteAll();//清理所有索引库
//        IndexWriter indexWriter=new IndexWriter(directory,new IndexWriterConfig(new StandardAnalyzer()));
        //记录索引开始时间
        long startTime = System.currentTimeMillis();
        //步骤三：读取磁盘中文件，对应每一个文件创建一个文档对象
        Document document = new Document();
//        document.add(new TextField("fieldContent", device_id, Field.Store.YES));
        document.add(new TextField("fieldContent", msg, Field.Store.YES));
        indexWriter.addDocument(document);
        //记录索引结束时间
        long endTime = System.currentTimeMillis();
        System.out.println("建立索引"+ "共耗时" + (endTime-startTime) + "毫秒");
        indexWriter.commit();
        //步骤八：关闭资源
        indexWriter.close();
        System.out.println("建立索引成功-----关闭资源");
    }
    //系统的日志文件路径
    @Value("${logging.file.path}")
    private String logUrl;

    public static void main(String[] args)throws IOException {
        //步骤一：创建Directory对象，用于指定索引库的位置    RAMDirectory内存
        Directory directory = FSDirectory.open(new File("D:\\lucene\\index").toPath());
        //步骤二：创建一个IndexWriter对象，用于写索引
//        Analyzer analyzer = new StandardAnalyzer();
       IndexWriter indexWriter=new IndexWriter(directory,new IndexWriterConfig(new IKAnalyzer(false)));

       indexWriter.deleteAll();//清理所有索引库
//        IndexWriter indexWriter=new IndexWriter(directory,new IndexWriterConfig(new StandardAnalyzer()));
        //记录索引开始时间
        long startTime = System.currentTimeMillis();
        //步骤三：读取磁盘中文件，对应每一个文件创建一个文档对象
        File file=new File("D:\\testlog");
        //步骤四：获取文件列表
        File[] files = file.listFiles();
        for (File item:files) {
            BufferedReader bufferedReader = new BufferedReader(new FileReader(item));
            String strLine = null;
            while(null != (strLine = bufferedReader.readLine())){
                Document document = new Document();
//                document.add(new Field());
                document.add(new TextField("fieldContent", strLine, Field.Store.YES));
                indexWriter.addDocument(document);
            }
        }
        //记录索引结束时间
        long endTime = System.currentTimeMillis();
        System.out.println("建立索引"+ "共耗时" + (endTime-startTime) + "毫秒");
        indexWriter.commit();
        //步骤八：关闭资源
        indexWriter.close();
        System.out.println("建立索引成功-----关闭资源");
    }
}
