首先说下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;
}