Lucene入门实例-CRUD

1、导入jar包

lucene-analyzers-common-7.6.0.jar

lucene-analyzers-smartcn-7.6.0.jar

lucene-core-7.6.0.jar

2、代码

package org.longIt.Lucene_app;

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.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.*;
import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import java.nio.file.Paths;


public class LuceneIndex {

    public static void main(String[] args) {
        addIndex();
        //searchIndex();
        //deleteIndex();
        //updateIndex();
    }

    private static void updateIndex() {
        // TODO Auto-generated method stub
        try {
            //指定索引库的目录
            Directory directory = FSDirectory.open(Paths.get("D:\\study\\lucene\\lucene_index\\article_tb"));

            //创建分词器  暂时使用  单字分词器  后期再改善
            Analyzer analyzer = new StandardAnalyzer();
            //创建IndexWriterConfig实例,通过IndexWriterConfig实例来指定创建索引的相关信息,比如指定分词器
            IndexWriterConfig config = new IndexWriterConfig(analyzer);
            //指定索引的创建方式
            config.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);

            //创建索引  更新索引   删除索引都是IndexWriter来实现
            IndexWriter  indexWriter = new IndexWriter(directory,config);


            //一个Document实例代表一条记录
            Document document = new Document();
            /**
             * StringField不会对关键字进行分词
             * Store.YES:会对数据进行存储并分词,如果为NO则不会对数据进行存储,索引还是会创建
             *
             * */
            document.add(new StringField("articleId", "0001", Field.Store.YES));
            document.add(new TextField("title", "幽幽而来", Field.Store.YES));
            document.add(new TextField("content", "这世间,必有一种懂得是精神,穿越灵魂", Field.Store.YES));

            /**
             * 通过indexWriter将数据写入至索引库
             * 更新的原理是先删除之前的索引,再创建新的索引,相当于更新是  删除与添加两个动作的合集
             * **/
            indexWriter.updateDocument(new Term("articleId","0001"), document);
            //提交事务
            indexWriter.commit();
            //关闭流资源
            indexWriter.close();
            System.out.println("=======索引更新成功======");
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }

    public static void addIndex() {
        try {
            Directory directory = FSDirectory.open(Paths.get("D:\\study\\lucene\\lucene_index\\article_tb"));
//创建IndexWriterConfig实例,通过IndexWriterConfig实例来指定创建索引的相关信息,比如指定分词器
            //创建分词器  暂时使用  单字分词器  后期再改善
            Analyzer analyzer = new StandardAnalyzer();

            IndexWriterConfig config = new IndexWriterConfig(analyzer);
            //指定索引的创建方式
            config.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);

            //创建索引  更新索引   删除索引都是IndexWriter来实现
            IndexWriter indexWriter = new IndexWriter(directory, config);


            //一个Document实例代表一条记录
            Document document = new Document();
            /**
             * StringField不会对关键字进行分词
             * Store.YES:会对数据进行存储并分词,如果为NO则不会对数据进行存储,索引还是会创建
             *
             * */
            document.add(new StringField("articleId", "0001", Field.Store.YES));
            document.add(new TextField("title", "懂得人生0001", Field.Store.YES));
            document.add(new TextField("content", "一生一世", Field.Store.YES));

            //通过indexWriter将数据写入至索引库
            indexWriter.addDocument(document);
            //提交事务
            indexWriter.commit();
            //关闭流资源
            indexWriter.close();
            System.out.println("=======索引创建成功======");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void searchIndex() {

        try {
            Directory directory = FSDirectory.open(Paths.get("D:\\study\\lucene\\lucene_index\\article_tb"));

            //DirectoryReader的open方法指定需要读取的索引库信息,并返回相应的实例
            IndexReader indexReader = DirectoryReader.open(directory);

            //创建IndexSearcher实例,通过IndexSearcher实例进行全文检索
            IndexSearcher  indexSearcher = new IndexSearcher(indexReader);

            /*
            通过indexSearcher进行检索并指定两个参数
                第一个参数:封装查询的相关信息,比如说查询的关键字,是否需要分词或者需要分词的话采取什么分词器
               第二个参数:最多只要多少条记录
             TermQuery:中指定了查询的关键字以及查询哪一个字段
             TermQuery不会对关键字进行分词
            */
            Query query = new TermQuery(new Term("title","幽"));
            //查询索引表,最终数据都被封装在 TopDocs的实例中
            TopDocs topDocs = indexSearcher.search(query,10);

            //通过topDocs获取匹配全部记录
            ScoreDoc[] scoreDocs = topDocs.scoreDocs;
            System.out.println("获取到的记录数:"+scoreDocs.length);

            for (int i = 0; i < scoreDocs.length; i++) {
                //获取记录的id
                int id = scoreDocs[i].doc;
                //文章的得分
                float score = scoreDocs[i].score;
                System.out.println("id:"+id+" 分章的得分:"+score);
                //查询数据表
                Document document = indexSearcher.doc(id);
                String articleId = document.get("articleId");
                String title = document.get("title");
                String content = document.get("content");
                System.out.println("articleId:"+articleId+" title:"+title+" content:"+content);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    private static void deleteIndex() {
        // TODO Auto-generated method stub
        try {
            //指定索引库的目录
            Directory directory = FSDirectory.open(Paths.get("D:\\study\\lucene\\lucene_index\\article_tb"));

            //创建分词器  暂时使用  单字分词器  后期再改善
            Analyzer analyzer = new StandardAnalyzer();
            //创建IndexWriterConfig实例,通过IndexWriterConfig实例来指定创建索引的相关信息,比如指定分词器
            IndexWriterConfig config = new IndexWriterConfig(analyzer);
            //指定索引的创建方式
            config.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);

            //创建索引  更新索引   删除索引都是IndexWriter来实现
            IndexWriter  indexWriter = new IndexWriter(directory,config);

            //删除指定的索引
            indexWriter.deleteDocuments(new Term("articleId","0001"));

            //删除索引库中全部的索引
            //indexWriter.deleteAll();
            //提交事务
            indexWriter.commit();
            //关闭流资源
            indexWriter.close();
            System.out.println("=======索引删除成功======");
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }

}

猜你喜欢

转载自www.cnblogs.com/xiaofengfree/p/10353679.html