public long getBackgroundFrequency(BytesRef termBytes) { assert termsEnum != null; // having failed to find a field in the index we don't expect any calls for // frequencies long result = 0; try { if (termsEnum.seekExact(termBytes)) { result = termsEnum.docFreq(); } } catch (IOException e) { throw new ElasticsearchException("IOException loading background document frequency info", e); } return result; }
/** * Creates the TermsEnum (if not already created) and must be called before any calls to * getBackgroundFrequency * * @param context The aggregation context * @return The number of documents in the index (after an optional filter might have been applied) */ public long prepareBackground(AggregationContext context) { if (termsEnum != null) { // already prepared - return return termsEnum.getNumDocs(); } SearchContext searchContext = context.searchContext(); IndexReader reader = searchContext.searcher().getIndexReader(); try { if (numberOfAggregatorsCreated == 1) { // Setup a termsEnum for sole use by one aggregator termsEnum = new FilterableTermsEnum(reader, indexedFieldName, PostingsEnum.NONE, filter); } else { // When we have > 1 agg we have possibility of duplicate term frequency lookups // and so use a TermsEnum that caches results of all term lookups termsEnum = new FreqTermsEnum( reader, indexedFieldName, true, false, filter, searchContext.bigArrays()); } } catch (IOException e) { throw new ElasticsearchException("failed to build terms enumeration", e); } return termsEnum.getNumDocs(); }