10 Lucene:04.索引库维护

10 Lucene:04.索引库维护

17.索引库维护-添加文档、18.索引库维护-删除索引库、19.索引库维护-更新索引库

20.数值范围查询、21.QueryParser进行查询

Lucene的官网下载 点击Download

本例程使用:lucene-7.4.0.zip

运行环境要求jdk1.9版本或以上

开发工具:IntelliJ IDEA 2019.2.2

本入门案例用到的jar包:

commons-io-2.6.jar

lucene-core-7.4.0.jar

lucene-analyzers-common-7.4.0.jar

IK-Analyzer-1.0-SNAPSHOT

一、本教程之前的环境搭建及源码


=====================================

10 Lucene:01.全文检索基本介绍

10 Lucene:02.lucene入门案例

10 Lucene:03.中文分析器

=====================================


二、Luence索引库维护相关操作

1、索引库的添加

Field域的属性

是否分析:是否对域的内容进行分词处理。前提是我们要对域的内容进行查询。

是否索引:将Field分析后的词或整个Field值进行索引,只有索引方可搜索到。

比如:商品名称、商品简介分析后进行索引,订单号、身份证号不用分析但也要索引,这些将来都要作为查询条件。

是否存储:将Field值存储在文档中,存储在文档中的Field才可以从Document中获取

比如:商品名称、订单号,凡是将来要从Document中获取的Field都要存储。 

是否存储的标准:是否要将内容展示给用户

    Field类

数据类型

Analyzed

是否分析

Indexed

是否索引

Stored

是否存储

说明

    StringField(FieldName,        FieldValue,Store.YES))

字符串

N

Y

YN

这个Field用来构建一个字符串Field,但是不会进行分析,会将整个串存储在索引中,比如(订单号,姓名等)

是否存储在文档中用Store.YES或Store.NO决定

    LongPoint(String name,     long... point)

Long

Y

Y

N

可以使用LongPointIntPoint等类型存储数值类型的数据。让数值类型可以进行索引。但是不能存储数据,如果想存储数据还需要使用StoredField

    StoredField(FieldName,     FieldValue)

重载方法,支持多种类型

N

N

Y

这个Field用来构建不同类型Field

不分析,不索引,但要Field存储在文档中

    TextField(FieldName,     FieldValue, Store.NO)

 或

    TextField(FieldName,     reader)

字符串

Y

Y

YN

如果是一个Reader, lucene猜测内容比较多,会采用Unstored的策略.

  添加索引的代码  

  

    //添加索引
    @Test
    public void addDocument() throws Exception {
        //1、创建一个Director对象,指定索引库保存的位置。
        //把索引库保存在磁盘上
        Path path = new File("C:\\FFOutput\\index").toPath();
        Directory directory = FSDirectory.open(path);
        //2、基于Directory对象创建一个IndexWriter对象
        //此处配置使用中文分析器
        IndexWriterConfig config = new IndexWriterConfig(new IKAnalyzer());
        IndexWriter indexWriter =new IndexWriter(directory,config);
        //3、读取磁盘上的文件,对应每个文件创建一个文档对象
        File dir= new File("C:\\FFOutput\\searchsource");
        File[] files =dir.listFiles();
        for (File file:files
        ) {
            //取文件名
            String fileName =file.getName();
            //文件路径
            String filePath =file.getPath();
            //文件的内容commons-io工具类
            String fileContent= FileUtils.readFileToString(file,"utf-8");
            //文件的大小
            long fileSize = FileUtils.sizeOf(file);
            //创建Field ,根据需求选择不同的Field类型
            //参数1:域的名称;参数2:域的内容;参数3:是否存储
            Field fieldName = new TextField("name",fileName,Field.Store.YES);
            //Field fieldPath = new TextField("path",filePath,Field.Store.YES);
            Field fieldPath = new StoredField("path",filePath);
            Field fieldContent = new TextField("content",fileContent,Field.Store.YES);
            //Field fieldSize = new TextField("size",fileSize +"",Field.Store.YES);
            Field fieldSizeValue = new LongPoint("size",fileSize );
            Field fieldSizeStore = new StoredField("size",fileSize);
            //4、创建文档对象
            Document document = new Document();
            //向文档对象中添加域
            document.add(fieldName);
            document.add(fieldPath);
            document.add(fieldContent);
            //document.add(fieldSize);
            document.add(fieldSizeValue);
            document.add(fieldSizeStore);
            //5、把文档对象写入索引库
            indexWriter.addDocument(document);

        }
        //6、关闭indexWriter对象
        indexWriter.close();

    }

  索引库添加文档代码实现

  

package cn.bjut;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.*;
import org.junit.Test;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.File;
import java.nio.file.Path;

public class IndexManager {

    @Test
    public void addDocunment() throws Exception{
        //1、创建一个Director对象,指定索引库保存的位置。
        Path path = new File("C:\\FFOutput\\index").toPath();
        Directory directory = FSDirectory.open(path);
        //2、创建一个IndexWriter对象,需要使用IKAnalyzer作为分析器。
        IndexWriterConfig config = new IndexWriterConfig(new IKAnalyzer());
        IndexWriter indexWriter = new IndexWriter(directory,config);

        //创建一个Document文档对象
        Document document = new Document();
        //向Document对象中添加Field域
        document.add(new TextField("name","新添加的文件",Field.Store.YES));
        document.add(new TextField("content","新添加的文件内容",Field.Store.NO));
        document.add(new StoredField("path","C:\\FFOutput\\Hello"));

        // 把文档写入索引库
        indexWriter.addDocument(document);
        // 关闭索引库
        indexWriter.close();
    }
    
}

 18. 删除索引库 

说明:将索引目录的索引信息全部删除,直接彻底删除,无法恢复。

    //删除全部索引
    @Test
    public void deleteAllIndex() throws Exception {
        IndexWriter indexWriter = getIndexWriter();
        //删除全部索引
        indexWriter.deleteAll();
        //关闭indexwriter
        indexWriter.close();
    }

指定查询条件删除

    //根据查询条件删除索引
    @Test
    public void deleteIndexByQuery() throws Exception {
        IndexWriter indexWriter = getIndexWriter();
        //创建一个查询条件
        Query query = new TermQuery(new Term("filename", "apache"));
        //根据查询条件删除
        indexWriter.deleteDocuments(query);
        //关闭indexwriter
        indexWriter.close();
    }

    

    @Test
    public void deleteDocumentByQuery() throws Exception {


        //根据查询条件删除
        indexWriter.deleteDocuments(new Term("name","apache"));
        //关闭indexwriter
        indexWriter.close();

    }

19.更新索引库 

 原理就是先删除后添加。

    //更新修改索引库
    @Test
    public void updateIndex() throws Exception {
        IndexWriter indexWriter = getIndexWriter();
        //创建一个Document对象
        Document document = new Document();
        //向document对象中添加域。
        //不同的document可以有不同的域,同一个document可以有相同的域。
        document.add(new TextField("filename", "要更新的文档", Field.Store.YES));
        document.add(new TextField("content", " Lucene 简介 Lucene 是一个基于 Java 的全文信息检索工具包," +
                "它不是一个完整的搜索应用程序,而是为你的应用程序提供索引和搜索功能。",
                Field.Store.YES));
        indexWriter.updateDocument(new Term("content", "java"), document);
        //关闭indexWriter
        indexWriter.close();
    }

 三、Lucene索引库查询 

对要搜索的信息创建Query查询对象,Lucene会根据Query查询对象生成最终的查询语法,类似关系数据库Sql语法一样Lucene也有自己的查询语法,比如:“name:lucene”表示查询Field的name为“lucene”的文档信息。

         可通过两种方法创建查询对象:

         1)使用Lucene提供Query子类

         2)使用QueryParse解析查询表达式

1、TermQuery 

TermQuery,通过项查询,TermQuery不使用分析器所以建议匹配不分词的Field域查询,比如订单号、分类ID号等。

指定要查询的域和要查询的关键词。

//使用Termquery查询
@Test
public void testTermQuery() throws Exception {
    Directory directory = FSDirectory.open(new File("D:\\temp\\index").toPath());
    IndexReader indexReader = DirectoryReader.open(directory);
    IndexSearcher indexSearcher = new IndexSearcher(indexReader);
    
    //创建查询对象
    Query query = new TermQuery(new Term("content", "lucene"));
    //执行查询
    TopDocs topDocs = indexSearcher.search(query, 10);
    //共查询到的document个数
    System.out.println("查询结果总数量:" + topDocs.totalHits);
    //遍历查询结果
    for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
        Document document = indexSearcher.doc(scoreDoc.doc);
        System.out.println(document.get("filename"));
        //System.out.println(document.get("content"));
        System.out.println(document.get("path"));
        System.out.println(document.get("size"));
    }
    //关闭indexreader
    indexSearcher.getIndexReader().close();
}

 2、数值范围查询

public class SearchIndex {
    private IndexReader indexReader;
    private IndexSearcher indexSearcher;
    @Before
    public void init() throws Exception {
        indexReader = DirectoryReader.open(FSDirectory.open(new File("C:\\temp\\index").toPath()));
        indexSearcher = new IndexSearcher(indexReader);
    }

    @Test
    public void testRangeQuery() throws Exception {
        //创建一个Query对象
        Query query = LongPoint.newRangeQuery("size", 0l, 100l);
        printResult(query);
    }

    private void printResult(Query query) throws Exception {
        //执行查询
        TopDocs topDocs = indexSearcher.search(query, 10);
        System.out.println("总记录数:" + topDocs.totalHits);
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;
        for (ScoreDoc doc:scoreDocs){
            //取文档id
            int docId = doc.doc;
            //根据id取文档对象
            Document document = indexSearcher.doc(docId);
            System.out.println(document.get("name"));
            System.out.println(document.get("path"));
            System.out.println(document.get("size"));
            //System.out.println(document.get("content"));
            System.out.println("-----------------寂寞的分割线");
        }
        indexReader.close();
    }

    @Test
    public void testQueryParser() throws Exception {
        //创建一个QueryPaser对象,两个参数
        QueryParser queryParser = new QueryParser("name", new IKAnalyzer());
        //参数1:默认搜索域,参数2:分析器对象
        //使用QueryPaser对象创建一个Query对象
        Query query = queryParser.parse("lucene是一个Java开发的全文检索工具包");
        //执行查询
        printResult(query);
    }
}

 3、使用queryparser查询 

通过QueryParser也可以创建Query,QueryParser提供一个Parse方法,此方法可以直接根据查询语法来查询。Query对象执行的查询语法可通过System.out.println(query);查询。

需要使用到分析器。建议创建索引时使用的分析器和查询索引时使用的分析器要一致。

需要加入queryParser依赖的jar包。

package com.itheima.lucene;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.LongPoint;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.FSDirectory;
import org.junit.Before;
import org.junit.Test;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.File;

public class SearchIndex {
    private IndexReader indexReader;
    private IndexSearcher indexSearcher;
    @Before
    public void init() throws Exception {
        indexReader = DirectoryReader.open(FSDirectory.open(new File("C:\\temp\\index").toPath()));
        indexSearcher = new IndexSearcher(indexReader);
    }

    @Test
    public void testRangeQuery() throws Exception {
        //创建一个Query对象
        Query query = LongPoint.newRangeQuery("size", 0l, 100l);
        printResult(query);
    }

    private void printResult(Query query) throws Exception {
        //执行查询
        TopDocs topDocs = indexSearcher.search(query, 10);
        System.out.println("总记录数:" + topDocs.totalHits);
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;
        for (ScoreDoc doc:scoreDocs){
            //取文档id
            int docId = doc.doc;
            //根据id取文档对象
            Document document = indexSearcher.doc(docId);
            System.out.println(document.get("name"));
            System.out.println(document.get("path"));
            System.out.println(document.get("size"));
            //System.out.println(document.get("content"));
            System.out.println("-----------------寂寞的分割线");
        }
        indexReader.close();
    }

    @Test
    public void testQueryParser() throws Exception {
        //创建一个QueryPaser对象,两个参数
        QueryParser queryParser = new QueryParser("name", new IKAnalyzer());
        //参数1:默认搜索域,参数2:分析器对象
        //使用QueryPaser对象创建一个Query对象
        Query query = queryParser.parse("lucene是一个Java开发的全文检索工具包");
        //执行查询
        printResult(query);
    }
}

==================

end

发布了95 篇原创文章 · 获赞 20 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/qq_40993412/article/details/101673369
今日推荐