Skip to content

Commit a40e583

Browse files
author
Christoph Büscher
committed
WIP
1 parent 9f130a5 commit a40e583

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

server/src/main/java/org/elasticsearch/search/query/TopDocsCollectorContext.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,18 @@
1919

2020
package org.elasticsearch.search.query;
2121

22+
import org.apache.lucene.index.FieldInfo;
23+
import org.apache.lucene.index.FieldInfos;
24+
import org.apache.lucene.index.IndexOptions;
2225
import org.apache.lucene.index.IndexReader;
2326
import org.apache.lucene.index.LeafReaderContext;
27+
import org.apache.lucene.index.PointValues;
2428
import org.apache.lucene.index.Term;
29+
import org.apache.lucene.index.Terms;
2530
import org.apache.lucene.search.BoostQuery;
2631
import org.apache.lucene.search.Collector;
2732
import org.apache.lucene.search.ConstantScoreQuery;
33+
import org.apache.lucene.search.DocValuesFieldExistsQuery;
2834
import org.apache.lucene.search.FieldDoc;
2935
import org.apache.lucene.search.MatchAllDocsQuery;
3036
import org.apache.lucene.search.MultiCollector;
@@ -125,6 +131,7 @@ private EmptyTopDocsCollectorContext(IndexReader reader, Query query,
125131
}
126132
}
127133

134+
@Override
128135
Collector create(Collector in) {
129136
assert in == null;
130137
return collector;
@@ -357,6 +364,20 @@ static int shortcutTotalHitCount(IndexReader reader, Query query) throws IOExcep
357364
count += context.reader().docFreq(term);
358365
}
359366
return count;
367+
} else if (query.getClass() == DocValuesFieldExistsQuery.class && reader.hasDeletions() == false) {
368+
final String field = ((DocValuesFieldExistsQuery) query).getField();
369+
int count = 0;
370+
for (LeafReaderContext context : reader.leaves()) {
371+
FieldInfos fieldInfos = context.reader().getFieldInfos();
372+
FieldInfo fieldInfo = fieldInfos.fieldInfo(field);
373+
if (fieldInfo.getPointIndexDimensionCount() > 0) {
374+
count += PointValues.getDocCount(context.reader(), field);
375+
} else if (fieldInfo.getIndexOptions() != IndexOptions.NONE) {
376+
Terms terms = context.reader().terms(field);
377+
count += terms.getDocCount();
378+
}
379+
}
380+
return count;
360381
} else {
361382
return -1;
362383
}

server/src/test/java/org/elasticsearch/search/query/QueryPhaseTests.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.lucene.document.Document;
2323
import org.apache.lucene.document.Field.Store;
2424
import org.apache.lucene.document.NumericDocValuesField;
25+
import org.apache.lucene.document.SortedSetDocValuesField;
2526
import org.apache.lucene.document.StringField;
2627
import org.apache.lucene.index.DirectoryReader;
2728
import org.apache.lucene.index.IndexReader;
@@ -35,6 +36,7 @@
3536
import org.apache.lucene.search.BooleanQuery;
3637
import org.apache.lucene.search.Collector;
3738
import org.apache.lucene.search.ConstantScoreQuery;
39+
import org.apache.lucene.search.DocValuesFieldExistsQuery;
3840
import org.apache.lucene.search.FieldComparator;
3941
import org.apache.lucene.search.FieldDoc;
4042
import org.apache.lucene.search.FilterCollector;
@@ -50,6 +52,7 @@
5052
import org.apache.lucene.search.TotalHitCountCollector;
5153
import org.apache.lucene.search.Weight;
5254
import org.apache.lucene.store.Directory;
55+
import org.apache.lucene.util.BytesRef;
5356
import org.elasticsearch.action.search.SearchTask;
5457
import org.elasticsearch.common.settings.Settings;
5558
import org.elasticsearch.index.query.ParsedQuery;
@@ -92,18 +95,20 @@ public void tearDown() throws Exception {
9295
closeShards(indexShard);
9396
}
9497

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 {
9699
TestSearchContext context = new TestSearchContext(null, indexShard);
97100
context.parsedQuery(new ParsedQuery(query));
98101
context.setSize(0);
99102
context.setTask(new SearchTask(123L, "", "", "", null, Collections.emptyMap()));
100103

101-
final IndexSearcher searcher = shouldCollect ? new IndexSearcher(reader) :
104+
final IndexSearcher searcher = shouldCollectSearch ? new IndexSearcher(reader) :
102105
getAssertingEarlyTerminationSearcher(reader, 0);
103106

104107
final boolean rescore = QueryPhase.execute(context, searcher, checkCancelled -> {});
105108
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);
107112
}
108113

109114
private void countTestCase(boolean withDeletions) throws Exception {
@@ -115,9 +120,11 @@ private void countTestCase(boolean withDeletions) throws Exception {
115120
Document doc = new Document();
116121
if (randomBoolean()) {
117122
doc.add(new StringField("foo", "bar", Store.NO));
123+
doc.add(new SortedSetDocValuesField("foo", new BytesRef("bar")));
118124
}
119125
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")));
121128
}
122129
if (withDeletions && (rarely() || i == 0)) {
123130
doc.add(new StringField("delete", "yes", Store.NO));
@@ -132,16 +139,18 @@ private void countTestCase(boolean withDeletions) throws Exception {
132139
Query matchAllCsq = new ConstantScoreQuery(matchAll);
133140
Query tq = new TermQuery(new Term("foo", "bar"));
134141
Query tCsq = new ConstantScoreQuery(tq);
142+
Query dvfeq = new DocValuesFieldExistsQuery("foo");
135143
BooleanQuery bq = new BooleanQuery.Builder()
136144
.add(matchAll, Occur.SHOULD)
137145
.add(tq, Occur.MUST)
138146
.build();
139147

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);
145154
reader.close();
146155
w.close();
147156
dir.close();
@@ -541,6 +550,7 @@ public void testIndexSortScrollOptimization() throws Exception {
541550

542551
private static IndexSearcher getAssertingEarlyTerminationSearcher(IndexReader reader, int size) {
543552
return new IndexSearcher(reader) {
553+
@Override
544554
protected void search(List<LeafReaderContext> leaves, Weight weight, Collector collector) throws IOException {
545555
final Collector in = new AssertingEarlyTerminationFilterCollector(collector, size);
546556
super.search(leaves, weight, in);

0 commit comments

Comments
 (0)