21
21
22
22
import org .apache .lucene .document .Document ;
23
23
import org .apache .lucene .document .Field .Store ;
24
+ import org .apache .lucene .document .LatLonDocValuesField ;
25
+ import org .apache .lucene .document .LatLonPoint ;
24
26
import org .apache .lucene .document .NumericDocValuesField ;
27
+ import org .apache .lucene .document .SortedSetDocValuesField ;
25
28
import org .apache .lucene .document .StringField ;
26
29
import org .apache .lucene .index .DirectoryReader ;
27
30
import org .apache .lucene .index .IndexReader ;
35
38
import org .apache .lucene .search .BooleanQuery ;
36
39
import org .apache .lucene .search .Collector ;
37
40
import org .apache .lucene .search .ConstantScoreQuery ;
41
+ import org .apache .lucene .search .DocValuesFieldExistsQuery ;
38
42
import org .apache .lucene .search .FieldComparator ;
39
43
import org .apache .lucene .search .FieldDoc ;
40
44
import org .apache .lucene .search .FilterCollector ;
50
54
import org .apache .lucene .search .TotalHitCountCollector ;
51
55
import org .apache .lucene .search .Weight ;
52
56
import org .apache .lucene .store .Directory ;
57
+ import org .apache .lucene .util .BytesRef ;
53
58
import org .elasticsearch .action .search .SearchTask ;
54
59
import org .elasticsearch .common .settings .Settings ;
55
60
import org .elasticsearch .index .query .ParsedQuery ;
@@ -92,18 +97,20 @@ public void tearDown() throws Exception {
92
97
closeShards (indexShard );
93
98
}
94
99
95
- private void countTestCase (Query query , IndexReader reader , boolean shouldCollect ) throws Exception {
100
+ private void countTestCase (Query query , IndexReader reader , boolean shouldCollectSearch , boolean shouldCollectCount ) throws Exception {
96
101
TestSearchContext context = new TestSearchContext (null , indexShard );
97
102
context .parsedQuery (new ParsedQuery (query ));
98
103
context .setSize (0 );
99
104
context .setTask (new SearchTask (123L , "" , "" , "" , null , Collections .emptyMap ()));
100
105
101
- final IndexSearcher searcher = shouldCollect ? new IndexSearcher (reader ) :
106
+ final IndexSearcher searcher = shouldCollectSearch ? new IndexSearcher (reader ) :
102
107
getAssertingEarlyTerminationSearcher (reader , 0 );
103
108
104
109
final boolean rescore = QueryPhase .execute (context , searcher , checkCancelled -> {});
105
110
assertFalse (rescore );
106
- assertEquals (searcher .count (query ), context .queryResult ().topDocs ().topDocs .totalHits .value );
111
+ IndexSearcher countSearcher = shouldCollectCount ? new IndexSearcher (reader ) :
112
+ getAssertingEarlyTerminationSearcher (reader , 0 );
113
+ assertEquals (countSearcher .count (query ), context .queryResult ().topDocs ().topDocs .totalHits .value );
107
114
}
108
115
109
116
private void countTestCase (boolean withDeletions ) throws Exception {
@@ -115,9 +122,14 @@ private void countTestCase(boolean withDeletions) throws Exception {
115
122
Document doc = new Document ();
116
123
if (randomBoolean ()) {
117
124
doc .add (new StringField ("foo" , "bar" , Store .NO ));
125
+ doc .add (new SortedSetDocValuesField ("foo" , new BytesRef ("bar" )));
126
+ doc .add (new SortedSetDocValuesField ("docValuesOnlyField" , new BytesRef ("bar" )));
127
+ doc .add (new LatLonDocValuesField ("latLonDVField" , 1.0 , 1.0 ));
128
+ doc .add (new LatLonPoint ("latLonDVField" , 1.0 , 1.0 ));
118
129
}
119
130
if (randomBoolean ()) {
120
131
doc .add (new StringField ("foo" , "baz" , Store .NO ));
132
+ doc .add (new SortedSetDocValuesField ("foo" , new BytesRef ("baz" )));
121
133
}
122
134
if (withDeletions && (rarely () || i == 0 )) {
123
135
doc .add (new StringField ("delete" , "yes" , Store .NO ));
@@ -132,16 +144,25 @@ private void countTestCase(boolean withDeletions) throws Exception {
132
144
Query matchAllCsq = new ConstantScoreQuery (matchAll );
133
145
Query tq = new TermQuery (new Term ("foo" , "bar" ));
134
146
Query tCsq = new ConstantScoreQuery (tq );
147
+ Query dvfeq = new DocValuesFieldExistsQuery ("foo" );
148
+ Query dvfeq_points = new DocValuesFieldExistsQuery ("latLonDVField" );
149
+ Query dvfeqCsq = new ConstantScoreQuery (dvfeq );
150
+ // field with doc-values but not indexed will need to collect
151
+ Query dvOnlyfeq = new DocValuesFieldExistsQuery ("docValuesOnlyField" );
135
152
BooleanQuery bq = new BooleanQuery .Builder ()
136
153
.add (matchAll , Occur .SHOULD )
137
154
.add (tq , Occur .MUST )
138
155
.build ();
139
156
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 );
157
+ countTestCase (matchAll , reader , false , false );
158
+ countTestCase (matchAllCsq , reader , false , false );
159
+ countTestCase (tq , reader , withDeletions , withDeletions );
160
+ countTestCase (tCsq , reader , withDeletions , withDeletions );
161
+ countTestCase (dvfeq , reader , withDeletions , true );
162
+ countTestCase (dvfeq_points , reader , withDeletions , true );
163
+ countTestCase (dvfeqCsq , reader , withDeletions , true );
164
+ countTestCase (dvOnlyfeq , reader , true , true );
165
+ countTestCase (bq , reader , true , true );
145
166
reader .close ();
146
167
w .close ();
147
168
dir .close ();
@@ -541,6 +562,7 @@ public void testIndexSortScrollOptimization() throws Exception {
541
562
542
563
private static IndexSearcher getAssertingEarlyTerminationSearcher (IndexReader reader , int size ) {
543
564
return new IndexSearcher (reader ) {
565
+ @ Override
544
566
protected void search (List <LeafReaderContext > leaves , Weight weight , Collector collector ) throws IOException {
545
567
final Collector in = new AssertingEarlyTerminationFilterCollector (collector , size );
546
568
super .search (leaves , weight , in );
0 commit comments