前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Lucene索引库的维护和查询

Lucene索引库的维护和查询

作者头像
暴躁的程序猿
发布2022-03-24 15:57:01
4990
发布2022-03-24 15:57:01
举报
文章被收录于专栏:阿飞的学习记录

索引库的维护

索引库的添加

Field域的属性

是否分析:是否对域的内容进行分词处理。前提是我们要对域的内容进行查询。 是否索引:将Field分析后的词或整个Field值进行索引,只有索引方可搜索到。 比如:商品名称、商品简介分析后进行索引,订单号、身份证号不用分析但也要索引,这些将来都要作为查询条件。 是否存储:将Field值存储在文档中,存储在文档中的Field才可以从Document中获取 比如:商品名称、订单号,凡是将来要从Document中获取的Field都要存储。

代码实现

代码语言:javascript
复制
  @Test
    public void addDocument() throws Exception{
        //创建一个IndexWriter对象 需要使用IKAnalyzer作为分析器
        IndexWriter indexWriter=new IndexWriter(FSDirectory.open(new File("E:\\Download\\index").toPath()),
                new IndexWriterConfig(new IKAnalyzer()));
        //创建一个Document对象
        Document document=new Document();
        //向Document对象中添加域
        document.add(new TextField("name","新添加的文件", Field.Store.YES));
        document.add(new TextField("content","新添加的文件内容", Field.Store.NO));
        document.add(new StoredField("path","E:/temp/hello"));
        //把文档写入索引库
        indexWriter.addDocument(document);
        //关闭索引库
        indexWriter.close();
    }

索引库删除

删除全部

提取公共代码

代码语言:javascript
复制
 public IndexWriter indexWriter;
    @Before
    public void init() throws Exception{
        //创建一个IndexWriter对象 需要使用IKAnalyzer作为分析器
        indexWriter=new IndexWriter(FSDirectory.open(new File("E:\\BDownload\\index").toPath()),
                new IndexWriterConfig(new IKAnalyzer()));
    }
代码语言:javascript
复制
 @Test
    public void deleteAllDocument() throws Exception{
              //删除全部文档
        indexWriter.deleteAll();
        //关闭索引库
         indexWriter.close();
    }

指定查询条件删除

代码语言:javascript
复制
  @Test
    public void deleteDocumentByQuery() throws  Exception{
        indexWriter.deleteDocuments(new Term("name","apache"));
        indexWriter.close();
    }

索引库的修改

原理就是先删除后添加

代码语言:javascript
复制
  @Test
    public void updateDocument() throws Exception{
        //创建一个新的文档对象
        Document document=new Document();
        //向文档对象中添加域
        document.add(new TextField("name","更新之后的文档", Field.Store.YES));
        document.add(new TextField("name1","更新之后的文档1", Field.Store.YES));
        document.add(new TextField("name2","更新之后的文档2", Field.Store.YES));

        //更新操作
        indexWriter.updateDocument(new Term("name","spring"),document);
        //关闭索引库
        indexWriter.close();
    }

Lucene索引库查询 对要搜索的信息创建Query查询对象,Lucene会根据Query查询对象生成最终的查询语法,类似关系数据库Sql语法一样Lucene也有自己的查询语法,比如:“name:lucene”表示查询Field的name为“lucene”的文档信息。 可通过两种方法创建查询对象: 1.使用Lucene提供Query子类 2.使用QueryParse解析查询表达式

代码语言:javascript
复制
//使用Termquery查询
@Test
public void testTermQuery() throws Exception {
    Directory directory = FSDirectory.open(new File("E:\\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();
}

数值范围查询

代码语言:javascript
复制
 @Test
    public void testRangeQuery() throws Exception{
        //创建一个Query对象
        Query query = LongPoint.newRangeQuery("size", 0l, 10000l);

        printResult(query);
    }
代码语言:javascript
复制
  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();
    }

使用queryparser查询

通过QueryParser也可以创建Query,QueryParser提供一个Parse方法,此方法可以直接根据查询语法来查询。Query对象执行的查询语法可通过System.out.println(query);查询。 需要使用到分析器。建议创建索引时使用的分析器和查询索引时使用的分析器要一致。 需要加入queryParser依赖的jar包。

代码语言:javascript
复制
  @Test
    public void testQueryParser() throws Exception{
        // 创建一个QueryParser对象,两个参数
        //参数一是默认搜索域  参数二 分析器对象
        QueryParser queryParser=new QueryParser("name",new IKAnalyzer());
        //使用QueryParser对象 创建一个Query对象
        Query query = queryParser.parse("lucene是一个java开发全文检索工具包");
        //执行查询
        printResult(query);
    }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021/02/10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 索引库的维护
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档