|
27 | 27 | import org.elasticsearch.common.collect.List; |
28 | 28 | import org.elasticsearch.common.lease.Releasable; |
29 | 29 | import org.elasticsearch.common.lease.Releasables; |
30 | | -import org.elasticsearch.common.util.BigArrays; |
31 | 30 | import org.elasticsearch.common.util.LongArray; |
32 | | -import org.elasticsearch.common.util.LongHash; |
33 | 31 | import org.elasticsearch.index.fielddata.FieldData; |
34 | 32 | import org.elasticsearch.search.DocValueFormat; |
35 | 33 | import org.elasticsearch.search.aggregations.Aggregator; |
|
42 | 40 | import org.elasticsearch.search.aggregations.LeafBucketCollectorBase; |
43 | 41 | import org.elasticsearch.search.aggregations.bucket.terms.IncludeExclude.LongFilter; |
44 | 42 | import org.elasticsearch.search.aggregations.bucket.terms.LongKeyedBucketOrds.BucketOrdsEnum; |
| 43 | +import org.elasticsearch.search.aggregations.bucket.terms.SignificanceLookup.BackgroundFrequencyForLong; |
45 | 44 | import org.elasticsearch.search.aggregations.bucket.terms.heuristic.SignificanceHeuristic; |
46 | 45 | import org.elasticsearch.search.aggregations.support.ValuesSource; |
47 | 46 | import org.elasticsearch.search.internal.ContextIndexSearcher; |
@@ -469,19 +468,18 @@ DoubleTerms buildEmptyResult() { |
469 | 468 | } |
470 | 469 |
|
471 | 470 | class SignificantLongTermsResults extends ResultStrategy<SignificantLongTerms, SignificantLongTerms.Bucket> { |
472 | | - private final BackgroundFrequencies backgroundFrequencies; |
| 471 | + private final BackgroundFrequencyForLong backgroundFrequencies; |
473 | 472 | private final long supersetSize; |
474 | 473 | private final SignificanceHeuristic significanceHeuristic; |
475 | 474 | private LongArray subsetSizes; |
476 | 475 |
|
477 | 476 | SignificantLongTermsResults( |
478 | | - SignificantTermsAggregatorFactory termsAggFactory, |
| 477 | + SignificanceLookup significanceLookup, |
479 | 478 | SignificanceHeuristic significanceHeuristic, |
480 | 479 | boolean collectsFromSingleBucket |
481 | 480 | ) { |
482 | | - LookupBackgroundFrequencies lookup = new LookupBackgroundFrequencies(termsAggFactory); |
483 | | - backgroundFrequencies = collectsFromSingleBucket ? lookup : new CacheBackgroundFrequencies(lookup, context.bigArrays()); |
484 | | - supersetSize = termsAggFactory.getSupersetNumDocs(); |
| 481 | + backgroundFrequencies = significanceLookup.longLookup(context.bigArrays(), collectsFromSingleBucket); |
| 482 | + supersetSize = significanceLookup.supersetSize(); |
485 | 483 | this.significanceHeuristic = significanceHeuristic; |
486 | 484 | subsetSizes = context.bigArrays().newLongArray(1, true); |
487 | 485 | } |
@@ -588,66 +586,5 @@ public void close() { |
588 | 586 | } |
589 | 587 | } |
590 | 588 |
|
591 | | - /** |
592 | | - * Lookup frequencies for terms. |
593 | | - */ |
594 | | - private interface BackgroundFrequencies extends Releasable { |
595 | | - long freq(long term) throws IOException; |
596 | | - } |
597 | | - |
598 | | - /** |
599 | | - * Lookup frequencies for terms. |
600 | | - */ |
601 | | - private static class LookupBackgroundFrequencies implements BackgroundFrequencies { |
602 | | - // TODO a reference to the factory is weird - probably should be reference to what we need from it. |
603 | | - private final SignificantTermsAggregatorFactory termsAggFactory; |
604 | | - |
605 | | - LookupBackgroundFrequencies(SignificantTermsAggregatorFactory termsAggFactory) { |
606 | | - this.termsAggFactory = termsAggFactory; |
607 | | - } |
608 | 589 |
|
609 | | - @Override |
610 | | - public long freq(long term) throws IOException { |
611 | | - return termsAggFactory.getBackgroundFrequency(term); |
612 | | - } |
613 | | - |
614 | | - @Override |
615 | | - public void close() { |
616 | | - termsAggFactory.close(); |
617 | | - } |
618 | | - } |
619 | | - |
620 | | - /** |
621 | | - * Lookup and cache background frequencies for terms. |
622 | | - */ |
623 | | - private static class CacheBackgroundFrequencies implements BackgroundFrequencies { |
624 | | - private final LookupBackgroundFrequencies lookup; |
625 | | - private final BigArrays bigArrays; |
626 | | - private final LongHash termToPosition; |
627 | | - private LongArray positionToFreq; |
628 | | - |
629 | | - CacheBackgroundFrequencies(LookupBackgroundFrequencies lookup, BigArrays bigArrays) { |
630 | | - this.lookup = lookup; |
631 | | - this.bigArrays = bigArrays; |
632 | | - termToPosition = new LongHash(1, bigArrays); |
633 | | - positionToFreq = bigArrays.newLongArray(1, false); |
634 | | - } |
635 | | - |
636 | | - @Override |
637 | | - public long freq(long term) throws IOException { |
638 | | - long position = termToPosition.add(term); |
639 | | - if (position < 0) { |
640 | | - return positionToFreq.get(-1 - position); |
641 | | - } |
642 | | - long freq = lookup.freq(term); |
643 | | - positionToFreq = bigArrays.grow(positionToFreq, position + 1); |
644 | | - positionToFreq.set(position, freq); |
645 | | - return freq; |
646 | | - } |
647 | | - |
648 | | - @Override |
649 | | - public void close() { |
650 | | - Releasables.close(lookup, termToPosition, positionToFreq); |
651 | | - } |
652 | | - } |
653 | 590 | } |
0 commit comments