Skip to content

Commit a78d4d9

Browse files
committed
Query: match_all query (also in query_string: *:*) is very slow, closes elastic#451.
1 parent 36e31de commit a78d4d9

File tree

2 files changed

+6
-70
lines changed

2 files changed

+6
-70
lines changed

modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/docset/AllDocSet.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@ private AllDocIdSetIterator(int maxDoc) {
6666
if (++doc < maxDoc) {
6767
return doc;
6868
}
69-
return NO_MORE_DOCS;
69+
return doc = NO_MORE_DOCS;
7070
}
7171

7272
@Override public int advance(int target) throws IOException {
7373
doc = target;
7474
if (doc < maxDoc) {
7575
return doc;
7676
}
77-
return NO_MORE_DOCS;
77+
return doc = NO_MORE_DOCS;
7878
}
7979
}
8080
}

modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/docset/NotDocSet.java

Lines changed: 4 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -19,90 +19,26 @@
1919

2020
package org.elasticsearch.common.lucene.docset;
2121

22-
import org.apache.lucene.search.DocIdSetIterator;
23-
2422
import java.io.IOException;
2523

2624
/**
2725
* @author kimchy (shay.banon)
2826
*/
29-
public class NotDocSet extends DocSet {
27+
public class NotDocSet extends GetDocSet {
3028

3129
private final DocSet set;
3230

33-
private final int max;
34-
3531
public NotDocSet(DocSet set, int max) {
32+
super(max);
3633
this.set = set;
37-
this.max = max;
3834
}
3935

4036
@Override public boolean isCacheable() {
41-
return set.isCacheable();
37+
// if it is cached, create a new doc set for it so it will be fast for advance in iterator
38+
return false;
4239
}
4340

4441
@Override public boolean get(int doc) throws IOException {
4542
return !set.get(doc);
4643
}
47-
48-
@Override public DocIdSetIterator iterator() throws IOException {
49-
return new NotDocIdSetIterator();
50-
}
51-
52-
class NotDocIdSetIterator extends DocIdSetIterator {
53-
int lastReturn = -1;
54-
private DocIdSetIterator it1 = null;
55-
private int innerDocid = -1;
56-
57-
NotDocIdSetIterator() throws IOException {
58-
initialize();
59-
}
60-
61-
private void initialize() throws IOException {
62-
it1 = set.iterator();
63-
64-
try {
65-
if ((innerDocid = it1.nextDoc()) == DocIdSetIterator.NO_MORE_DOCS) it1 = null;
66-
} catch (IOException e) {
67-
e.printStackTrace();
68-
}
69-
}
70-
71-
@Override
72-
public int docID() {
73-
return lastReturn;
74-
}
75-
76-
@Override
77-
public int nextDoc() throws IOException {
78-
return advance(0);
79-
}
80-
81-
@Override
82-
public int advance(int target) throws IOException {
83-
84-
if (lastReturn == DocIdSetIterator.NO_MORE_DOCS) {
85-
return DocIdSetIterator.NO_MORE_DOCS;
86-
}
87-
88-
if (target <= lastReturn) target = lastReturn + 1;
89-
90-
if (it1 != null && innerDocid < target) {
91-
if ((innerDocid = it1.advance(target)) == DocIdSetIterator.NO_MORE_DOCS) {
92-
it1 = null;
93-
}
94-
}
95-
96-
while (it1 != null && innerDocid == target) {
97-
target++;
98-
if (target >= max) {
99-
return (lastReturn = DocIdSetIterator.NO_MORE_DOCS);
100-
}
101-
if ((innerDocid = it1.advance(target)) == DocIdSetIterator.NO_MORE_DOCS) {
102-
it1 = null;
103-
}
104-
}
105-
return (lastReturn = target);
106-
}
107-
}
10844
}

0 commit comments

Comments
 (0)