9
9
package org .elasticsearch .search .aggregations .bucket .terms ;
10
10
11
11
import org .apache .lucene .index .DocValues ;
12
- import org .apache .lucene .index .LeafReader ;
13
12
import org .apache .lucene .index .LeafReaderContext ;
14
13
import org .apache .lucene .index .SortedDocValues ;
15
14
import org .apache .lucene .index .SortedSetDocValues ;
16
15
import org .apache .lucene .util .ArrayUtil ;
17
- import org .apache .lucene .util .Bits ;
18
16
import org .apache .lucene .util .BytesRef ;
19
17
import org .apache .lucene .util .PriorityQueue ;
20
18
import org .elasticsearch .common .io .stream .StreamOutput ;
21
- import org .elasticsearch .common .util .BigArrays ;
22
19
import org .elasticsearch .common .util .LongArray ;
23
20
import org .elasticsearch .common .util .LongHash ;
24
- import org .elasticsearch .core .Nullable ;
25
21
import org .elasticsearch .core .Releasable ;
26
22
import org .elasticsearch .core .Releasables ;
27
23
import org .elasticsearch .search .DocValueFormat ;
@@ -86,8 +82,7 @@ public GlobalOrdinalsStringTermsAggregator(
86
82
SubAggCollectionMode collectionMode ,
87
83
boolean showTermDocCountError ,
88
84
CardinalityUpperBound cardinality ,
89
- Map <String , Object > metadata ,
90
- boolean excludeDeletedDocs
85
+ Map <String , Object > metadata
91
86
) throws IOException {
92
87
super (name , factories , context , parent , order , format , bucketCountThresholds , collectionMode , showTermDocCountError , metadata );
93
88
this .resultStrategy = resultStrategy .apply (this ); // ResultStrategy needs a reference to the Aggregator to do its job.
@@ -96,13 +91,13 @@ public GlobalOrdinalsStringTermsAggregator(
96
91
this .lookupGlobalOrd = values ::lookupOrd ;
97
92
this .acceptedGlobalOrdinals = acceptedOrds ;
98
93
if (remapGlobalOrds ) {
99
- this .collectionStrategy = new RemapGlobalOrds (cardinality , excludeDeletedDocs );
94
+ this .collectionStrategy = new RemapGlobalOrds (cardinality );
100
95
} else {
101
96
this .collectionStrategy = cardinality .map (estimate -> {
102
97
if (estimate > 1 ) {
103
98
throw new AggregationExecutionException ("Dense ords don't know how to collect from many buckets" );
104
99
}
105
- return new DenseGlobalOrds (excludeDeletedDocs );
100
+ return new DenseGlobalOrds ();
106
101
});
107
102
}
108
103
}
@@ -279,8 +274,7 @@ static class LowCardinality extends GlobalOrdinalsStringTermsAggregator {
279
274
boolean remapGlobalOrds ,
280
275
SubAggCollectionMode collectionMode ,
281
276
boolean showTermDocCountError ,
282
- Map <String , Object > metadata ,
283
- boolean excludeDeletedDocs
277
+ Map <String , Object > metadata
284
278
) throws IOException {
285
279
super (
286
280
name ,
@@ -298,8 +292,7 @@ static class LowCardinality extends GlobalOrdinalsStringTermsAggregator {
298
292
collectionMode ,
299
293
showTermDocCountError ,
300
294
CardinalityUpperBound .ONE ,
301
- metadata ,
302
- excludeDeletedDocs
295
+ metadata
303
296
);
304
297
assert factories == null || factories .countAggregators () == 0 ;
305
298
this .segmentDocCounts = context .bigArrays ().newLongArray (1 , true );
@@ -448,13 +441,6 @@ interface BucketInfoConsumer {
448
441
* bucket ordinal.
449
442
*/
450
443
class DenseGlobalOrds extends CollectionStrategy {
451
-
452
- private final boolean excludeDeletedDocs ;
453
-
454
- DenseGlobalOrds (boolean excludeDeletedDocs ) {
455
- this .excludeDeletedDocs = excludeDeletedDocs ;
456
- }
457
-
458
444
@ Override
459
445
String describe () {
460
446
return "dense" ;
@@ -485,14 +471,6 @@ long globalOrdToBucketOrd(long owningBucketOrd, long globalOrd) {
485
471
@ Override
486
472
void forEach (long owningBucketOrd , BucketInfoConsumer consumer ) throws IOException {
487
473
assert owningBucketOrd == 0 ;
488
- if (excludeDeletedDocs ) {
489
- forEachExcludeDeletedDocs (consumer );
490
- } else {
491
- forEachAllowDeletedDocs (consumer );
492
- }
493
- }
494
-
495
- private void forEachAllowDeletedDocs (BucketInfoConsumer consumer ) throws IOException {
496
474
for (long globalOrd = 0 ; globalOrd < valueCount ; globalOrd ++) {
497
475
if (false == acceptedGlobalOrdinals .test (globalOrd )) {
498
476
continue ;
@@ -504,39 +482,6 @@ private void forEachAllowDeletedDocs(BucketInfoConsumer consumer) throws IOExcep
504
482
}
505
483
}
506
484
507
- /**
508
- * Excludes deleted docs in the results by cross-checking with liveDocs.
509
- */
510
- private void forEachExcludeDeletedDocs (BucketInfoConsumer consumer ) throws IOException {
511
- try (LongHash accepted = new LongHash (20 , new BigArrays (null , null , "" ))) {
512
- for (LeafReaderContext ctx : searcher ().getTopReaderContext ().leaves ()) {
513
- LeafReader reader = ctx .reader ();
514
- Bits liveDocs = reader .getLiveDocs ();
515
- SortedSetDocValues globalOrds = null ;
516
- for (int docId = 0 ; docId < reader .maxDoc (); ++docId ) {
517
- if (liveDocs == null || liveDocs .get (docId )) { // document is not deleted
518
- globalOrds = globalOrds == null ? valuesSource .globalOrdinalsValues (ctx ) : globalOrds ;
519
- if (globalOrds .advanceExact (docId )) {
520
- for (long globalOrd = globalOrds .nextOrd (); globalOrd != NO_MORE_ORDS ; globalOrd = globalOrds .nextOrd ()) {
521
- if (accepted .find (globalOrd ) >= 0 ) {
522
- continue ;
523
- }
524
- if (false == acceptedGlobalOrdinals .test (globalOrd )) {
525
- continue ;
526
- }
527
- long docCount = bucketDocCount (globalOrd );
528
- if (bucketCountThresholds .getMinDocCount () == 0 || docCount > 0 ) {
529
- consumer .accept (globalOrd , globalOrd , docCount );
530
- accepted .add (globalOrd );
531
- }
532
- }
533
- }
534
- }
535
- }
536
- }
537
- }
538
- }
539
-
540
485
@ Override
541
486
public void close () {}
542
487
}
@@ -549,11 +494,9 @@ public void close() {}
549
494
*/
550
495
private class RemapGlobalOrds extends CollectionStrategy {
551
496
private final LongKeyedBucketOrds bucketOrds ;
552
- private final boolean excludeDeletedDocs ;
553
497
554
- private RemapGlobalOrds (CardinalityUpperBound cardinality , boolean excludeDeletedDocs ) {
498
+ private RemapGlobalOrds (CardinalityUpperBound cardinality ) {
555
499
bucketOrds = LongKeyedBucketOrds .buildForValueRange (bigArrays (), cardinality , 0 , valueCount - 1 );
556
- this .excludeDeletedDocs = excludeDeletedDocs ;
557
500
}
558
501
559
502
@ Override
@@ -587,20 +530,27 @@ long globalOrdToBucketOrd(long owningBucketOrd, long globalOrd) {
587
530
588
531
@ Override
589
532
void forEach (long owningBucketOrd , BucketInfoConsumer consumer ) throws IOException {
590
- if (excludeDeletedDocs ) {
591
- forEachExcludeDeletedDocs (owningBucketOrd , consumer );
592
- } else {
593
- forEachAllowDeletedDocs (owningBucketOrd , consumer );
594
- }
595
- }
596
-
597
- void forEachAllowDeletedDocs (long owningBucketOrd , BucketInfoConsumer consumer ) throws IOException {
598
533
if (bucketCountThresholds .getMinDocCount () == 0 ) {
599
534
for (long globalOrd = 0 ; globalOrd < valueCount ; globalOrd ++) {
600
535
if (false == acceptedGlobalOrdinals .test (globalOrd )) {
601
536
continue ;
602
537
}
603
- addBucketForMinDocCountZero (owningBucketOrd , globalOrd , consumer , null );
538
+ /*
539
+ * Use `add` instead of `find` here to assign an ordinal
540
+ * even if the global ord wasn't found so we can build
541
+ * sub-aggregations without trouble even though we haven't
542
+ * hit any documents for them. This is wasteful, but
543
+ * settings minDocCount == 0 is wasteful in general.....
544
+ */
545
+ long bucketOrd = bucketOrds .add (owningBucketOrd , globalOrd );
546
+ long docCount ;
547
+ if (bucketOrd < 0 ) {
548
+ bucketOrd = -1 - bucketOrd ;
549
+ docCount = bucketDocCount (bucketOrd );
550
+ } else {
551
+ docCount = 0 ;
552
+ }
553
+ consumer .accept (globalOrd , bucketOrd , docCount );
604
554
}
605
555
} else {
606
556
LongKeyedBucketOrds .BucketOrdsEnum ordsEnum = bucketOrds .ordsEnum (owningBucketOrd );
@@ -613,64 +563,6 @@ void forEachAllowDeletedDocs(long owningBucketOrd, BucketInfoConsumer consumer)
613
563
}
614
564
}
615
565
616
- /**
617
- * Excludes deleted docs in the results by cross-checking with liveDocs.
618
- */
619
- void forEachExcludeDeletedDocs (long owningBucketOrd , BucketInfoConsumer consumer ) throws IOException {
620
- assert bucketCountThresholds .getMinDocCount () == 0 ;
621
- try (LongHash accepted = new LongHash (20 , new BigArrays (null , null , "" ))) {
622
- for (LeafReaderContext ctx : searcher ().getTopReaderContext ().leaves ()) {
623
- LeafReader reader = ctx .reader ();
624
- Bits liveDocs = reader .getLiveDocs ();
625
- SortedSetDocValues globalOrds = null ;
626
- for (int docId = 0 ; docId < reader .maxDoc (); ++docId ) {
627
- if (liveDocs == null || liveDocs .get (docId )) { // document is not deleted
628
- globalOrds = globalOrds == null ? valuesSource .globalOrdinalsValues (ctx ) : globalOrds ;
629
- if (globalOrds .advanceExact (docId )) {
630
- for (long globalOrd = globalOrds .nextOrd (); globalOrd != NO_MORE_ORDS ; globalOrd = globalOrds .nextOrd ()) {
631
- if (accepted .find (globalOrd ) >= 0 ) {
632
- continue ;
633
- }
634
- if (false == acceptedGlobalOrdinals .test (globalOrd )) {
635
- continue ;
636
- }
637
- addBucketForMinDocCountZero (owningBucketOrd , globalOrd , consumer , accepted );
638
- }
639
- }
640
- }
641
- }
642
- }
643
- }
644
- }
645
-
646
- private void addBucketForMinDocCountZero (
647
- long owningBucketOrd ,
648
- long globalOrd ,
649
- BucketInfoConsumer consumer ,
650
- @ Nullable LongHash accepted
651
- ) throws IOException {
652
- /*
653
- * Use `add` instead of `find` here to assign an ordinal
654
- * even if the global ord wasn't found so we can build
655
- * sub-aggregations without trouble even though we haven't
656
- * hit any documents for them. This is wasteful, but
657
- * settings minDocCount == 0 is wasteful in general.....
658
- */
659
- long bucketOrd = bucketOrds .add (owningBucketOrd , globalOrd );
660
- long docCount ;
661
- if (bucketOrd < 0 ) {
662
- bucketOrd = -1 - bucketOrd ;
663
- docCount = bucketDocCount (bucketOrd );
664
- } else {
665
- docCount = 0 ;
666
- }
667
- assert globalOrd >= 0 ;
668
- consumer .accept (globalOrd , bucketOrd , docCount );
669
- if (accepted != null ) {
670
- accepted .add (globalOrd );
671
- }
672
- }
673
-
674
566
@ Override
675
567
public void close () {
676
568
bucketOrds .close ();
0 commit comments