Solr's facet source code interpretation (1) - facet.query

The meaning of solr's facet query is very simple, indicating how many doc matches a query in all the search results. Its realization principle is also very simple.

All facet operations in solr are in facetComponent. In the process method of this class, the facet is delegated to the SimpleFacet class, and then the simpleFacet.getFacetCounts method is called. The facet.query we are talking about is in this class, as follows:

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<>();
	try {
		facetResponse.add("facet_queries", getFacetQueryCounts());
		facetResponse.add("facet_fields", getFacetFieldCounts()); //
		facetResponse.add("facet_ranges", getFacetRangeCounts());
		facetResponse.add("facet_intervals", getFacetIntervalCounts());

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

 It can be found that all facet operations are in this class, including facetQuery, facetField, and facetRange. The most commonly used ones in work are facetQuery and facetField. Let’s take a look at facetQuery first.

public NamedList<Integer> getFacetQueryCounts() throws IOException, SyntaxError {
	NamedList<Integer> res = new SimpleOrderedMap<>();
	String[] facetQs = params.getParams(FacetParams.FACET_QUERY);
	if (null != facetQs && 0 != facetQs.length) {
		for (String q : facetQs) {//Rotate all facet queries
			parseParams(FacetParams.FACET_QUERY, q);//This method is very simple, that is to save some parameters into the properties of this class, such as the following docs,
			// TODO: slight optimization would prevent double-parsing of any localParams
			Query qobj = QParser.getParser(q, null, req).getQuery();//Get the query of facet.query.
			if (qobj == null) {
				res.add(key, 0);
			} else if (params.getBool(GroupParams.GROUP_FACET, false)) {// I didn't read this because I haven't encountered this function in my work. It will likely be added later.
				res.add(key, getGroupedFacetQueryCount(qobj));
			} else {
				res.add(key, searcher.numDocs(qobj, docs));//searcher.numDocs is very simple, just use the docs hit by this qobj and the second parameter docs to take the intersection, and return the number of intersections, the second The first parameter is the set of id's of the hit doc in q and fq in solr's parameters. And this method will use filterCache, so facet.query is very efficient, it only involves the merging of inverted lists, and also uses filterCache.
			}
		}
	}
	return res;
}

 It is so simple to understand the implementation principle of facet.query. His implementation is still very efficient and can be used boldly.

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326078306&siteId=291194637