22
22
import org .apache .lucene .document .Document ;
23
23
import org .apache .lucene .document .Field .Store ;
24
24
import org .apache .lucene .document .NumericDocValuesField ;
25
+ import org .apache .lucene .document .SortedSetDocValuesField ;
25
26
import org .apache .lucene .document .StringField ;
26
27
import org .apache .lucene .index .DirectoryReader ;
27
28
import org .apache .lucene .index .IndexReader ;
35
36
import org .apache .lucene .search .BooleanQuery ;
36
37
import org .apache .lucene .search .Collector ;
37
38
import org .apache .lucene .search .ConstantScoreQuery ;
39
+ import org .apache .lucene .search .DocValuesFieldExistsQuery ;
38
40
import org .apache .lucene .search .FieldComparator ;
39
41
import org .apache .lucene .search .FieldDoc ;
40
42
import org .apache .lucene .search .FilterCollector ;
50
52
import org .apache .lucene .search .TotalHitCountCollector ;
51
53
import org .apache .lucene .search .Weight ;
52
54
import org .apache .lucene .store .Directory ;
55
+ import org .apache .lucene .util .BytesRef ;
53
56
import org .elasticsearch .action .search .SearchTask ;
54
57
import org .elasticsearch .common .settings .Settings ;
55
58
import org .elasticsearch .index .query .ParsedQuery ;
@@ -92,18 +95,20 @@ public void tearDown() throws Exception {
92
95
closeShards (indexShard );
93
96
}
94
97
95
- private void countTestCase (Query query , IndexReader reader , boolean shouldCollect ) throws Exception {
98
+ private void countTestCase (Query query , IndexReader reader , boolean shouldCollectSearch , boolean shouldCollectCount ) throws Exception {
96
99
TestSearchContext context = new TestSearchContext (null , indexShard );
97
100
context .parsedQuery (new ParsedQuery (query ));
98
101
context .setSize (0 );
99
102
context .setTask (new SearchTask (123L , "" , "" , "" , null , Collections .emptyMap ()));
100
103
101
- final IndexSearcher searcher = shouldCollect ? new IndexSearcher (reader ) :
104
+ final IndexSearcher searcher = shouldCollectSearch ? new IndexSearcher (reader ) :
102
105
getAssertingEarlyTerminationSearcher (reader , 0 );
103
106
104
107
final boolean rescore = QueryPhase .execute (context , searcher , checkCancelled -> {});
105
108
assertFalse (rescore );
106
- assertEquals (searcher .count (query ), context .queryResult ().topDocs ().topDocs .totalHits .value );
109
+ IndexSearcher countSearcher = shouldCollectCount ? new IndexSearcher (reader ) :
110
+ getAssertingEarlyTerminationSearcher (reader , 0 );
111
+ assertEquals (countSearcher .count (query ), context .queryResult ().topDocs ().topDocs .totalHits .value );
107
112
}
108
113
109
114
private void countTestCase (boolean withDeletions ) throws Exception {
@@ -115,9 +120,11 @@ private void countTestCase(boolean withDeletions) throws Exception {
115
120
Document doc = new Document ();
116
121
if (randomBoolean ()) {
117
122
doc .add (new StringField ("foo" , "bar" , Store .NO ));
123
+ doc .add (new SortedSetDocValuesField ("foo" , new BytesRef ("bar" )));
118
124
}
119
125
if (randomBoolean ()) {
120
- doc .add (new StringField ("foo" , "baz" , Store .NO ));
126
+ doc .add (new StringField ("foo" , "bar" , Store .NO ));
127
+ doc .add (new SortedSetDocValuesField ("foo" , new BytesRef ("bar" )));
121
128
}
122
129
if (withDeletions && (rarely () || i == 0 )) {
123
130
doc .add (new StringField ("delete" , "yes" , Store .NO ));
@@ -132,16 +139,18 @@ private void countTestCase(boolean withDeletions) throws Exception {
132
139
Query matchAllCsq = new ConstantScoreQuery (matchAll );
133
140
Query tq = new TermQuery (new Term ("foo" , "bar" ));
134
141
Query tCsq = new ConstantScoreQuery (tq );
142
+ Query dvfeq = new DocValuesFieldExistsQuery ("foo" );
135
143
BooleanQuery bq = new BooleanQuery .Builder ()
136
144
.add (matchAll , Occur .SHOULD )
137
145
.add (tq , Occur .MUST )
138
146
.build ();
139
147
140
- countTestCase (matchAll , reader , false );
141
- countTestCase (matchAllCsq , reader , false );
142
- countTestCase (tq , reader , withDeletions );
143
- countTestCase (tCsq , reader , withDeletions );
144
- countTestCase (bq , reader , true );
148
+ countTestCase (matchAll , reader , false , false );
149
+ countTestCase (matchAllCsq , reader , false , false );
150
+ countTestCase (tq , reader , withDeletions , withDeletions );
151
+ countTestCase (tCsq , reader , withDeletions , withDeletions );
152
+ countTestCase (dvfeq , reader , withDeletions , true );
153
+ countTestCase (bq , reader , true , true );
145
154
reader .close ();
146
155
w .close ();
147
156
dir .close ();
@@ -541,6 +550,7 @@ public void testIndexSortScrollOptimization() throws Exception {
541
550
542
551
private static IndexSearcher getAssertingEarlyTerminationSearcher (IndexReader reader , int size ) {
543
552
return new IndexSearcher (reader ) {
553
+ @ Override
544
554
protected void search (List <LeafReaderContext > leaves , Weight weight , Collector collector ) throws IOException {
545
555
final Collector in = new AssertingEarlyTerminationFilterCollector (collector , size );
546
556
super .search (leaves , weight , in );
0 commit comments