query never returns, closes #1725.

This commit is contained in:
Shay Banon 2012-02-22 01:50:22 +02:00
parent 7ff12a5bd9
commit 31b793591e
2 changed files with 107 additions and 72 deletions

View File

@ -19,9 +19,7 @@
package org.apache.lucene.search;
import org.apache.lucene.index.IndexReader;
import java.io.IOException;
import org.elasticsearch.common.lucene.search.NotDeletedFilter;
/**
*
@ -29,81 +27,21 @@ import java.io.IOException;
// LUCENE MONITOR: Against ConstantScoreQuery, basically added logic in the doc iterator to take deletions into account
// So it can basically be cached safely even with a reader that changes deletions but remain with teh same cache key
// See more: https://issues.apache.org/jira/browse/LUCENE-2468
// TODO Lucene 4.0 won't need this, since live docs are "and'ed" while scoring
public class DeletionAwareConstantScoreQuery extends ConstantScoreQuery {
private final Filter actualFilter;
public DeletionAwareConstantScoreQuery(Filter filter) {
super(filter);
super(new NotDeletedFilter(filter));
this.actualFilter = filter;
}
// trick so any external systems still think that its the actual filter we use, and not the
// deleted filter
@Override
public Weight createWeight(Searcher searcher) throws IOException {
return new DeletionConstantWeight(searcher);
}
protected class DeletionConstantWeight extends ConstantWeight {
private final Similarity similarity;
public DeletionConstantWeight(Searcher searcher) throws IOException {
super(searcher);
similarity = getSimilarity(searcher);
}
@Override
public Scorer scorer(IndexReader reader, boolean scoreDocsInOrder, boolean topScorer) throws IOException {
final DocIdSet dis = filter.getDocIdSet(reader);
if (dis == null)
return null;
DocIdSetIterator disi = dis.iterator();
if (disi == null) {
return null;
}
return new DeletionConstantScorer(reader, similarity, disi, this);
}
}
protected class DeletionConstantScorer extends ConstantScorer {
private final IndexReader reader;
private int doc = -1;
public DeletionConstantScorer(IndexReader reader, Similarity similarity, DocIdSetIterator docIdSetIterator, Weight w) throws IOException {
super(similarity, docIdSetIterator, w);
this.reader = reader;
}
@Override
public int nextDoc() throws IOException {
while ((doc = docIdSetIterator.nextDoc()) != NO_MORE_DOCS) {
if (!reader.isDeleted(doc)) {
return doc;
}
}
return doc;
}
@Override
public int docID() {
return doc;
}
@Override
public int advance(int target) throws IOException {
doc = docIdSetIterator.advance(target);
if (doc != NO_MORE_DOCS) {
if (!reader.isDeleted(doc)) {
return doc;
} else {
while ((doc = docIdSetIterator.nextDoc()) != NO_MORE_DOCS) {
if (!reader.isDeleted(doc)) {
return doc;
}
}
return doc;
}
}
return doc;
}
public Filter getFilter() {
return this.actualFilter;
}
}

View File

@ -0,0 +1,97 @@
/*
* Licensed to ElasticSearch and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. ElasticSearch licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.common.lucene.search;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.DocIdSet;
import org.apache.lucene.search.DocIdSetIterator;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.FilteredDocIdSetIterator;
import java.io.IOException;
/**
* A filter that filters out deleted documents.
*/
public class NotDeletedFilter extends Filter {
private final Filter filter;
public NotDeletedFilter(Filter filter) {
this.filter = filter;
}
@Override
public DocIdSet getDocIdSet(IndexReader reader) throws IOException {
DocIdSet docIdSet = filter.getDocIdSet(reader);
if (docIdSet == null) {
return null;
}
if (!reader.hasDeletions()) {
return docIdSet;
}
return new NotDeletedDocIdSet(docIdSet, reader);
}
public Filter filter() {
return this.filter;
}
@Override
public String toString() {
return "NotDeleted(" + filter + ")";
}
static class NotDeletedDocIdSet extends DocIdSet {
private final DocIdSet innerSet;
private final IndexReader reader;
NotDeletedDocIdSet(DocIdSet innerSet, IndexReader reader) {
this.innerSet = innerSet;
this.reader = reader;
}
@Override
public DocIdSetIterator iterator() throws IOException {
DocIdSetIterator iterator = innerSet.iterator();
if (iterator == null) {
return null;
}
return new NotDeletedDocIdSetIterator(iterator, reader);
}
}
static class NotDeletedDocIdSetIterator extends FilteredDocIdSetIterator {
private final IndexReader reader;
NotDeletedDocIdSetIterator(DocIdSetIterator innerIter, IndexReader reader) {
super(innerIter);
this.reader = reader;
}
@Override
protected boolean match(int doc) throws IOException {
return !reader.isDeleted(doc);
}
}
}