Lucene索引库的维护和查询

索引库的维护

索引库的添加

Field域的属性

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

代码实现

  @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();
    }

索引库删除

删除全部

提取公共代码

 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()));
    }
 @Test
    public void deleteAllDocument() throws Exception{
              //删除全部文档
        indexWriter.deleteAll();
        //关闭索引库
         indexWriter.close();
    }

指定查询条件删除

  @Test
    public void deleteDocumentByQuery() throws  Exception{
        indexWriter.deleteDocuments(new Term("name","apache"));
        indexWriter.close();
    }

索引库的修改

原理就是先删除后添加

  @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解析查询表达式

//使用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();
}

数值范围查询

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

        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();
    }

使用queryparser查询

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

  @Test
    public void testQueryParser() throws Exception{
        // 创建一个QueryParser对象,两个参数
        //参数一是默认搜索域  参数二 分析器对象
        QueryParser queryParser=new QueryParser("name",new IKAnalyzer());
        //使用QueryParser对象 创建一个Query对象
        Query query = queryParser.parse("lucene是一个java开发全文检索工具包");
        //执行查询
        printResult(query);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值