Lucene-2.2.0 源代码阅读学习(29)

xiaoxiao2026-09-16  21

关于IndexSearcher检索器。

在学习IndexSearcher检索器之前,先大致了解一下下面几项:

1、首先,要知道Weight(接口)存在的目的:

使得检索不改变一个Query,使得Query可以重用。所以就出现了Weight,一个Weight可以保存与某次检索相关的IndexSearcher检索器的独立状态值。其实Weight间接保存了IndexSearcher索引器的独立状态信息。

每次检索,即初始化一个IndexSearcher检索器,都需要一个Query,例如

   Query query = new TermQuery(term);        Hits hits = searcher.search(query);

而Query抽象了用户的检索意向信息,可以使用Query的public Query rewrite(IndexReader reader)方法来实现对先前的检索意向信息的修改(重写)。

用户的一次检索,是与一个Weight对应的,当然可以不保存本次检索相关的IndexSearcher检索器的状态信息到一个Weight中,这样的坏处就是Query不能重用,每次都要重新实例化一个。

Weight接口定义了如下的内容:

public interface Weight extends java.io.Serializable {Query getQuery();    // 通过一个Weight可以获取到一个Query实例float getValue();    // Weight相关的Query的权重值float sumOfSquaredWeights() throws IOException;    // 一个Query可以有很多子句(比如一个BooleanQuery可以包含多个TermQuery子句),获取到所有子句的权重值的平方void normalize(float norm);    // 指派查询的标准化因子Scorer scorer(IndexReader reader) throws IOException;   // 根据一个IndexReader,通过Weight获取得分Explanation explain(IndexReader reader, int doc) throws IOException;    // 为编号为doc的Document设置计算得分的描述信息}

2、其次,知道Sort类是为一次检索设定排序方式的。

这些排序的方式是在SortField类中定义的,一共定义了7种,当然包括客户化定制排序方式。

3、再次,知道Explanation类是关于某次检索中,封装了对某个Document的得分计算的描述。

4、接着,知道TopDocs类是关于某次实际的检索出来结果集的信息,包括Hits数量,及其最大得分的信息。TopDocs的子类TopFieldDocs类指定了排序方式(Sort),为Fields进行排序。

5、然后,知道FieldSelector是一个筛选器接口,将某个Document中的满足接受条件的Field返回。在FieldSelector中定义了FieldSelectorResult accept(String fieldName);方法。

6、最后,理解TopDocCollector类的用于IndexSearcher的目的。其实TopDocCollector内部定义了一个collect()方法,该方法可以实现根据Document的得分来排序。TopDocCollector类继承自HitCollector,而HitCollector抽象类定义了实现查询(queries)、排序(sorting)、过滤(filtering)的功能。

现在,可以通过IndexSearcher索引器的源代码来解读它具有哪些功能。其实已经很容易读了,在理解上面6项的基础上。IndexSearcher的源代码实现如下所示:

package org.apache.lucene.search;

import org.apache.lucene.document.Document;import org.apache.lucene.document.FieldSelector;import org.apache.lucene.index.CorruptIndexException;import org.apache.lucene.index.IndexReader;import org.apache.lucene.index.Term;import org.apache.lucene.store.Directory;

import java.io.IOException;import java.util.BitSet;

//    IndexSearcher继承自Searcher抽象类,在Searcher抽象类中定义了一些search()方法,返回Hits。public class IndexSearcher extends Searcher {IndexReader reader;private boolean closeReader;

//    实例化一个IndexSearcher检索器public IndexSearcher(String path) throws CorruptIndexException, IOException {    this(IndexReader.open(path), true);}

public IndexSearcher(Directory directory) throws CorruptIndexException, IOException {    this(IndexReader.open(directory), true);}

public IndexSearcher(IndexReader r) {    this(r, false);}private IndexSearcher(IndexReader r, boolean closeReader) {    reader = r;    this.closeReader = closeReader;}

public IndexReader getIndexReader() {    return reader;}

//    一个检索器与一个IndexReader是密切相关的public void close() throws IOException {    if(closeReader)      reader.close();}

//    获取包含词条term的Document的数量public int docFreq(Term term) throws IOException {    return reader.docFreq(term);}

//    获取编号为i的Documentpublic Document doc(int i) throws CorruptIndexException, IOException {    return reader.document(i);}//   指定了一个筛选器FieldSelector(该筛选器要接受满足条件的某个Document中的Field,将不满足的过滤掉)public Document doc(int i, FieldSelector fieldSelector) throws CorruptIndexException, IOException {     return reader.document(i, fieldSelector);}//     检索得到的最大可能的Document的数量 + 1public int maxDoc() throws IOException {    return reader.maxDoc();}

//    查询的核心方法,返回TopDocs,参数指定Weight、Filter、返回Document的数量public TopDocs search(Weight weight, Filter filter, final int nDocs)       throws IOException {

    if (nDocs <= 0)        throw new IllegalArgumentException("nDocs must be > 0");

    TopDocCollector collector = new TopDocCollector(nDocs);    search(weight, filter, collector);    return collector.topDocs();}

//    查询的方法,返回TopFieldDocspublic TopFieldDocs search(Weight weight, Filter filter, final int nDocs,                             Sort sort)      throws IOException {

    TopFieldDocCollector collector =      new TopFieldDocCollector(reader, sort, nDocs);    search(weight, filter, collector);    return (TopFieldDocs)collector.topDocs();}

//    返回值是void,实际检索的结果集存放在HitCollector中public void search(Weight weight, Filter filter,                     final HitCollector results) throws IOException {    HitCollector collector = results;    if (filter != null) {    // Filter不为null的时候才执行下面代码      final BitSet bits = filter.bits(reader);      collector = new HitCollector() {          public final void collect(int doc, float score) {            if (bits.get(doc)) {                               results.collect(doc, score);            }          }        };    }

    Scorer scorer = weight.scorer(reader);    if (scorer == null)      return;    scorer.score(collector);}

// 在先前创建Query并执行检索的基础上,重新改写这个Query,而不是重新实例化一个Query

public Query rewrite(Query original) throws IOException {    Query query = original;    for (Query rewrittenQuery = query.rewrite(reader); rewrittenQuery != query;         rewrittenQuery = query.rewrite(reader)) {      query = rewrittenQuery;    }    return query;}

public Explanation explain(Weight weight, int doc) throws IOException {    return weight.explain(reader, doc);}}

在检索的时候,首先就是要实例化一个IndexSearcher检索器,而这个过程其实就是使用IndexReader打开一个索引目录。

然后通过提交的Query,就可以使用IndexSearcher的search()方法进行检索了。

从IndexSearcher的源代码来看,每个search()方法都需要一个Query实例。因为只有用户提交查询(根据提交的关键字构造一个Query),才能执行检索。也就是说,在检索中Query是非常重要的。实际上Query对于检索的实现具有很大的灵活性,主要是通过Query抽象类的炉体子类的实现来体现的。

转载请注明原文地址: https://www.6miu.com/read-5052741.html

最新回复(0)