solr4 facet 源码流程分析

这篇博客详细分析了Solr4中Facet组件的工作流程,从SearchHandler开始,经过QueryComponent和FacetComponent,重点讲解了FacetComponent的process方法及SimpleFacets类在统计字段值过程中的作用。通过代码示例展示了如何根据不同的facet参数获取相应的查询统计数据。

首先说下solr facet流程中会牵扯到类:

1,SearchHandler

2,QueryComponent

3,FacetComponent

4,SolrIndexSearcher


1,SearchHandler说明

请求处理起始,会调用请求中所有的component,不仅仅是query、facet。

2,QueryComponent 3,FacetComponent

都是查询处理组件,所以代码流程都相同,主要方法如下:

prepare、process、distributedProcess

4,SolrIndexSearcher

具体的执行查询的searcher

对于有facet的请求跟没有facet的请求的流程在此执行是不一致的,分别走两个不同的方法,如下:

getDocListAndSetNC

getDocListNC


3,FacetComponent中的主体处理方法肯定就是process了,代码如下:

  /**
   * Actually run the query
   */
  @Override
  public void process(ResponseBuilder rb) throws IOException
  {
    if (rb.doFacets) {
      SolrParams params = rb.req.getParams();

     //SimpleFacets这个对象很关键,在这里进行初始化,并在创建时将getDocListAndSetNC获取的set集合作为变量放入
      SimpleFacets f = new SimpleFacets(rb.req,
              rb.getResults().docSet,
              params,
              rb );

      NamedList<Object> counts = f.getFacetCounts();//这里就是获取的某个字段的统计数量值了。
      String[] pivots = params.getParams( FacetParams.FACET_PIVOT );
      if( pivots != null && pivots.length > 0 ) {
        PivotFacetHelper pivotHelper = new PivotFacetHelper(rb.req,
            rb.getResults().docSet,
            params,
            rb );
        NamedList v = pivotHelper.process(pivots);
        if( v != null ) {
          counts.add( PIVOT_KEY, v );
        }
      }
      
      // TODO ???? add this directly to the response, or to the builder?
      rb.rsp.add( "facet_counts", counts );
    }
  }


后面可以看下SimpleFacets的getFacetCounts代码,比较一目了然,

说明:根据不同的facet的参数返回不同的查询统计数据

public NamedList<Object> getFacetCounts() {

    // if someone called this method, benefit of the doubt: assume true
    if (!params.getBool(FacetParams.FACET,true))
      return null;

    facetResponse = new SimpleOrderedMap<Object>();
    try {
      facetResponse.add("facet_queries", getFacetQueryCounts());
      facetResponse.add("facet_fields", getFacetFieldCounts());
      facetResponse.add("facet_dates", getFacetDateCounts());
      facetResponse.add("facet_ranges", getFacetRangeCounts());

    } catch (IOException e) {
      throw new SolrException(ErrorCode.SERVER_ERROR, e);
    } catch (SyntaxError e) {
      throw new SolrException(ErrorCode.BAD_REQUEST, e);
    }
    return facetResponse;
  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值