Search in sources :

Example 21 with IndexReaderContext

use of org.apache.lucene.index.IndexReaderContext in project lucene-solr by apache.

the class ExactStatsCache method returnLocalStats.

@Override
public void returnLocalStats(ResponseBuilder rb, SolrIndexSearcher searcher) {
    Query q = rb.getQuery();
    try {
        HashSet<Term> terms = new HashSet<>();
        searcher.createNormalizedWeight(q, true).extractTerms(terms);
        IndexReaderContext context = searcher.getTopReaderContext();
        HashMap<String, TermStats> statsMap = new HashMap<>();
        HashMap<String, CollectionStats> colMap = new HashMap<>();
        for (Term t : terms) {
            TermContext termContext = TermContext.build(context, t);
            TermStatistics tst = searcher.localTermStatistics(t, termContext);
            if (tst.docFreq() == 0) {
                // skip terms that are not present here
                continue;
            }
            statsMap.put(t.toString(), new TermStats(t.field(), tst));
            rb.rsp.add(TERMS_KEY, t.toString());
            if (!colMap.containsKey(t.field())) {
                // collection stats for this field
                colMap.put(t.field(), new CollectionStats(searcher.localCollectionStatistics(t.field())));
            }
        }
        if (statsMap.size() != 0 && colMap.size() != 0) {
            //Don't add empty keys
            String termStatsString = StatsUtil.termStatsMapToString(statsMap);
            rb.rsp.add(TERM_STATS_KEY, termStatsString);
            String colStatsString = StatsUtil.colStatsMapToString(colMap);
            rb.rsp.add(COL_STATS_KEY, colStatsString);
            if (LOG.isDebugEnabled()) {
                LOG.debug("termStats=" + termStatsString + ", collectionStats=" + colStatsString + ", terms=" + terms + ", numDocs=" + searcher.maxDoc());
            }
        }
    } catch (IOException e) {
        LOG.error("Error collecting local stats, query='" + q.toString() + "'", e);
        throw new SolrException(ErrorCode.SERVER_ERROR, "Error collecting local stats.", e);
    }
}
Also used : Query(org.apache.lucene.search.Query) HashMap(java.util.HashMap) Term(org.apache.lucene.index.Term) IOException(java.io.IOException) TermStatistics(org.apache.lucene.search.TermStatistics) IndexReaderContext(org.apache.lucene.index.IndexReaderContext) TermContext(org.apache.lucene.index.TermContext) SolrException(org.apache.solr.common.SolrException) HashSet(java.util.HashSet)

Aggregations

IndexReaderContext (org.apache.lucene.index.IndexReaderContext)21 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)10 Weight (org.apache.lucene.search.Weight)8 Scorer (org.apache.lucene.search.Scorer)7 TermContext (org.apache.lucene.index.TermContext)6 IndexSearcher (org.apache.lucene.search.IndexSearcher)6 IndexReader (org.apache.lucene.index.IndexReader)4 Term (org.apache.lucene.index.Term)4 Query (org.apache.lucene.search.Query)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 CompositeReaderContext (org.apache.lucene.index.CompositeReaderContext)3 DocIdSetIterator (org.apache.lucene.search.DocIdSetIterator)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Document (org.apache.lucene.document.Document)2 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)2 MatchingDocs (org.apache.lucene.facet.FacetsCollector.MatchingDocs)2 LeafReader (org.apache.lucene.index.LeafReader)2