LUCENE-8472: Always rewrite soft-deletes retention query

This change ensures that we always rewrite the soft-deletes merge
retention policy before passing it into `createWeight` as some Query
does not support `createWeight` directly.
This commit is contained in:
Nhat Nguyen 2018-09-03 11:13:42 -04:00
parent a1ec716e10
commit b8dfb7e911
3 changed files with 30 additions and 1 deletions

View File

@ -312,6 +312,8 @@ Bug Fixes:
* LUCENE-8384: Fix missing advance docValues generation while handling docValues
update in PendingSoftDeletes. (Simon Willnauer, Nhat Nguyen)
* LUCENE-8472: Always rewrite the soft-deletes merge retention query. (Adrien Grand, Nhat Nguyen)
======================= Lucene 7.4.0 =======================
Upgrading

View File

@ -124,7 +124,7 @@ public final class SoftDeletesRetentionMergePolicy extends OneMergeWrappingMerge
private static Scorer getScorer(Query query, CodecReader reader) throws IOException {
IndexSearcher s = new IndexSearcher(reader);
s.setQueryCache(null);
Weight weight = s.createWeight(query, ScoreMode.COMPLETE_NO_SCORES, 1.0f);
Weight weight = s.createWeight(s.rewrite(query), ScoreMode.COMPLETE_NO_SCORES, 1.0f);
return weight.scorer(reader.getContext());
}

View File

@ -40,6 +40,7 @@ import org.apache.lucene.search.DocValuesFieldExistsQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.PrefixQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.SearcherFactory;
import org.apache.lucene.search.SearcherManager;
@ -678,6 +679,32 @@ public class TestSoftDeletesRetentionMergePolicy extends LuceneTestCase {
IOUtils.close(writer, dir);
}
public void testRewriteRetentionQuery() throws Exception {
Directory dir = newDirectory();
IndexWriterConfig config = newIndexWriterConfig().setSoftDeletesField("soft_deletes")
.setMergePolicy(new SoftDeletesRetentionMergePolicy("soft_deletes",
() -> new PrefixQuery(new Term("id", "foo")), newMergePolicy()));
IndexWriter writer = new IndexWriter(dir, config);
Document d = new Document();
d.add(new StringField("id", "foo-1", Field.Store.YES));
writer.addDocument(d);
d = new Document();
d.add(new StringField("id", "foo-2", Field.Store.YES));
writer.softUpdateDocument(new Term("id", "foo-1"), d, new NumericDocValuesField("soft_deletes", 1));
d = new Document();
d.add(new StringField("id", "bar-1", Field.Store.YES));
writer.addDocument(d);
d.add(new StringField("id", "bar-2", Field.Store.YES));
writer.softUpdateDocument(new Term("id", "bar-1"), d, new NumericDocValuesField("soft_deletes", 1));
writer.forceMerge(1);
assertEquals(2, writer.numDocs()); // foo-2, bar-2
assertEquals(3, writer.maxDoc()); // foo-1, foo-2, bar-2
IOUtils.close(writer, dir);
}
static void doUpdate(Term doc, IndexWriter writer, Field... fields) throws IOException {
long seqId = -1;
do { // retry if we just committing a merge