首页 > 技术文章 > Lucene04-Lucene的基本使用

yuanke-blog 2019-07-20 15:45 原文

Lucene04-Lucene的基本使用

导入的包

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.*;
import org.apache.lucene.index.*;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.FSDirectory;
import org.junit.Test;
import org.omg.CORBA.PUBLIC_MEMBER;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

1、修改

    /**
     * 修改
     *
     * @throws IOException
     */
    @Test
    public void updateDoc() throws IOException {
        //        1 创建索引库对象,指定索引库的位置
        //1.1 创建索引库位置
        Path path = new File("D:\\lucene").toPath();
        //1.2 创建索引库对象,关联索引库位置
        FSDirectory directory = FSDirectory.open(path);
//        2 创建IndexWriterConfig对象并指定分词器对象
        //2.1 创建分词器对象用于指定分词规则
        StandardAnalyzer standardAnalyzer = new StandardAnalyzer();//标准分词器,分词规则:单字分词
        //2.2 创建写出器配置对象,关联分词器对象
        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(standardAnalyzer);
//        3 创建一个IndexWriter对象 &指定索引库的位置&指定一个IndexWriterConfig对象。
        IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
        // 4 构建修改的doc文档数据.
        Document document = new Document();
//        5 创建field对象,将field添加到document对象中。
        document.add(new StringField("docId", "docIdOne", Field.Store.YES));
        document.add(new TextField("title", "修改的是文档2", Field.Store.YES));
        document.add(new TextField("content", "我的祖国是一个伟大的国家", Field.Store.YES));
        document.add(new StringField("score", "100", Field.Store.YES));
        document.add(new StoredField("name", "张三"));
        //6 执行修改(删除一些或者一个,再新增一个),按照docId分词查询文档,查到对应文档就修改,查不到就新增
        indexWriter.updateDocument(new Term("docId", "docIdTwo"), document);
        //按照title分词查询文档,查到对应文档就修改,查不到就新增  第二个参数是List<Document>,批量修改
//        List<Document> documents = Arrays.asList(document);
//        indexWriter.updateDocuments(new Term("title", "智"), Arrays.asList(document));
        //修改完提交
        indexWriter.commit();
        //7 关闭indexWriter
        indexWriter.close();
    }

 

2、批量新增

    /**
     * 批量新增
     *
     * @throws IOException
     */
    @Test
    public void batchAddDoc() throws IOException {
        //1 创建索引库对象&指定索引库的位置
        FSDirectory directory = FSDirectory.open(new File("D:\\lucene").toPath());
        //2 创建IndexWriterConfig对象并指定分词器对象
        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(new StandardAnalyzer());
        //3 创建一个IndexWriter对象 &指定索引库的位置&指定一个IndexWriterConfig对象。
        IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
        //4 批量创建document对象。
        //4.1 创建list集合用来存储Document对象
        List<Document> documents = new ArrayList<>();
        //4.2 创建10个Document对象
        for (int i = 0; i < 10; i++) {
            //4.3 创建document对象。
            Document document = new Document();
            //4.4 将field添加到document对象中。
            document.add(new StringField("docId", "docId" + i, Field.Store.YES));
            document.add(new TextField("title", "我的祖国" + i, Field.Store.YES));
            document.add(new TextField("content", "我的祖国是一个伟大的国家" + i, Field.Store.YES));
            document.add(new StringField("score", "100" + i, Field.Store.YES));
            document.add(new StoredField("name", "张三" + i));
            //4.4 添加到集合中
            documents.add(document);
        }
        //5 使用indexwriter对象将documents对象批量写入索引库中。
        indexWriter.addDocuments(documents);
        //6 关闭indexwriter对象。
        indexWriter.close();
    }

 

3、删除

3.1 删除所有

 

    /**
     * 删除所有
     *
     * @throws IOException
     */
    @Test
    public void deleteAllDoc() throws IOException {
        //1 创建索引库对象,指定索引库的位置
        FSDirectory directory = FSDirectory.open(new File("D:\\lucene").toPath());
//        2 创建IndexWriterConfig对象并指定分词器对象
        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(new StandardAnalyzer());
//        3 创建一个IndexWriter对象 &指定索引库的位置&指定一个IndexWriterConfig对象。
        IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
        //4 执行方法删除所有
        indexWriter.deleteAll();
        //5 关闭资源
        indexWriter.close();
    }

 

 

 

3.2 根据查询条件删除

 

    /**
     * 根据查询条件删除
     *
     * @throws IOException
     */
    @Test
    public void deleteDocByQuery() throws IOException {
        //1 创建索引库对象&指定索引库的位置
        FSDirectory directory = FSDirectory.open(new File("D:\\lucene").toPath());
        //2 创建IndexWriterConfig对象并指定分词器对象
        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(new StandardAnalyzer());
        //3 创建一个IndexWriter对象 &指定索引库的位置&指定一个IndexWriterConfig对象。
        IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
        //4 执行删除 先查询然后把满足查询条件的数据进行查询  好比在mysql 先select然后delete
        //4.1 指定查询条件 & 关联分词对象
        TermQuery query = new TermQuery(new Term("title", "9"));
        //4.2 执行删除
        indexWriter.deleteDocuments(query);
        //5 关闭资源
        indexWriter.close();
    }

 

 

 

3.3 根据分词删除

    /**
     * 根据分词删除
     *
     * @throws IOException
     */
    @Test
    public void deleteDocByTerm() throws IOException {
        //1 创建索引库对象&指定索引库的位置
        FSDirectory directory = FSDirectory.open(new File("D:\\lucene").toPath());
        //2 创建IndexWriterConfig对象并指定分词器对象
        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(new StandardAnalyzer());
        //3 创建一个IndexWriter对象 &指定索引库的位置&指定一个IndexWriterConfig对象。
        IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
        //4 执行删除 直接删除 好比在mysql 直接delete
        //4.1 指定分词
        Term term = new Term("title", "8");
        //4.2 执行删除
        indexWriter.deleteDocuments(term);
        //5 关闭资源
        indexWriter.close();
    }

4、查询

参考Lucene02--入门程序

推荐阅读