LUCENE-1599: Add clone support for SpanQuerys. SpanRegexQuery counts on this functionality and does not work correctly without it.

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@791280 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Mark Robert Miller 2009-07-05 17:16:16 +00:00
parent 21fba4ee7b
commit e04abc52e7
5 changed files with 114 additions and 15 deletions

View File

@ -349,6 +349,10 @@ Bug fixes
16. LUCENE-1681: Fix infinite loop caused by a call to DocValues methods
getMinValue, getMaxValue, getAverageValue. (Simon Willnauer via Mark Miller)
17. LUCENE-1599: Add clone support for SpanQuerys. SpanRegexQuery counts
on this functionality and does not work correctly without it.
(Mark Miller)
New features
1. LUCENE-1411: Added expert API to open an IndexWriter on a prior

View File

@ -17,32 +17,46 @@ package org.apache.lucene.search.regex;
* limitations under the License.
*/
import java.io.IOException;
import junit.framework.TestCase;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.spans.SpanTermQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MultiSearcher;
import org.apache.lucene.search.spans.SpanFirstQuery;
import org.apache.lucene.search.spans.SpanNearQuery;
import org.apache.lucene.search.spans.SpanQuery;
import org.apache.lucene.search.spans.SpanFirstQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.store.RAMDirectory;
public class TestSpanRegexQuery extends TestCase {
Directory indexStoreA = new RAMDirectory();
Directory indexStoreB = new RAMDirectory();
public void testSpanRegex() throws Exception {
RAMDirectory directory = new RAMDirectory();
IndexWriter writer = new IndexWriter(directory, new SimpleAnalyzer(), true);
Document doc = new Document();
// doc.add(new Field("field", "the quick brown fox jumps over the lazy dog", Field.Store.NO, Field.Index.ANALYZED));
// writer.addDocument(doc);
// doc = new Document();
doc.add(new Field("field", "auto update", Field.Store.NO, Field.Index.ANALYZED));
// doc.add(new Field("field", "the quick brown fox jumps over the lazy dog",
// Field.Store.NO, Field.Index.ANALYZED));
// writer.addDocument(doc);
// doc = new Document();
doc.add(new Field("field", "auto update", Field.Store.NO,
Field.Index.ANALYZED));
writer.addDocument(doc);
doc = new Document();
doc.add(new Field("field", "first auto update", Field.Store.NO, Field.Index.ANALYZED));
doc.add(new Field("field", "first auto update", Field.Store.NO,
Field.Index.ANALYZED));
writer.addDocument(doc);
writer.optimize();
writer.close();
@ -50,8 +64,63 @@ public class TestSpanRegexQuery extends TestCase {
IndexSearcher searcher = new IndexSearcher(directory);
SpanRegexQuery srq = new SpanRegexQuery(new Term("field", "aut.*"));
SpanFirstQuery sfq = new SpanFirstQuery(srq, 1);
// SpanNearQuery query = new SpanNearQuery(new SpanQuery[] {srq, stq}, 6, true);
// SpanNearQuery query = new SpanNearQuery(new SpanQuery[] {srq, stq}, 6,
// true);
Hits hits = searcher.search(sfq);
assertEquals(1, hits.length());
}
public void testSpanRegexBug() throws CorruptIndexException, IOException {
createRAMDirectories();
SpanRegexQuery srq = new SpanRegexQuery(new Term("field", "a.*"));
SpanRegexQuery stq = new SpanRegexQuery(new Term("field", "b.*"));
SpanNearQuery query = new SpanNearQuery(new SpanQuery[] { srq, stq }, 6,
true);
// 1. Search the same store which works
IndexSearcher[] arrSearcher = new IndexSearcher[2];
arrSearcher[0] = new IndexSearcher(indexStoreA);
arrSearcher[1] = new IndexSearcher(indexStoreB);
MultiSearcher searcher = new MultiSearcher(arrSearcher);
Hits hits = searcher.search(query);
arrSearcher[0].close();
arrSearcher[1].close();
// Will fail here
// We expect 2 but only one matched
// The rewriter function only write it once on the first IndexSearcher
// So it's using term: a1 b1 to search on the second IndexSearcher
// As a result, it won't match the document in the second IndexSearcher
assertEquals(2, hits.length());
indexStoreA.close();
indexStoreB.close();
}
private void createRAMDirectories() throws CorruptIndexException,
LockObtainFailedException, IOException {
// creating a document to store
Document lDoc = new Document();
lDoc.add(new Field("field", "a1 b1", Field.Store.NO,
Field.Index.ANALYZED_NO_NORMS));
// creating a document to store
Document lDoc2 = new Document();
lDoc2.add(new Field("field", "a2 b2", Field.Store.NO,
Field.Index.ANALYZED_NO_NORMS));
// creating first index writer
IndexWriter writerA = new IndexWriter(indexStoreA, new StandardAnalyzer(),
true, IndexWriter.MaxFieldLength.LIMITED);
writerA.addDocument(lDoc);
writerA.optimize();
writerA.close();
// creating second index writer
IndexWriter writerB = new IndexWriter(indexStoreB, new StandardAnalyzer(),
true, IndexWriter.MaxFieldLength.LIMITED);
writerB.addDocument(lDoc2);
writerB.optimize();
writerB.close();
}
}

View File

@ -33,7 +33,7 @@ import org.apache.lucene.util.ToStringUtils;
/** Matches spans which are near one another. One can specify <i>slop</i>, the
* maximum number of intervening unmatched positions, as well as whether
* matches are required to be in-order. */
public class SpanNearQuery extends SpanQuery {
public class SpanNearQuery extends SpanQuery implements Cloneable {
private List clauses;
private int slop;
private boolean inOrder;
@ -152,6 +152,17 @@ public class SpanNearQuery extends SpanQuery {
}
}
public Object clone() {
int sz = clauses.size();
SpanQuery[] newClauses = new SpanQuery[sz];
for (int i = 0; i < sz; i++) {
SpanQuery clause = (SpanQuery) clauses.get(i);
newClauses[i] = (SpanQuery) clause.clone();
}
return new SpanNearQuery(newClauses, slop, inOrder);
}
/** Returns true iff <code>o</code> is equal to this. */
public boolean equals(Object o) {
if (this == o) return true;

View File

@ -27,7 +27,7 @@ import java.util.Collection;
import java.util.Set;
/** Removes matches which overlap with another SpanQuery. */
public class SpanNotQuery extends SpanQuery {
public class SpanNotQuery extends SpanQuery implements Cloneable {
private SpanQuery include;
private SpanQuery exclude;
@ -68,6 +68,9 @@ public class SpanNotQuery extends SpanQuery {
return buffer.toString();
}
public Object clone() {
return new SpanNotQuery((SpanQuery)include.clone(),(SpanQuery) exclude.clone());
}
public Spans getSpans(final IndexReader reader) throws IOException {
return new PayloadSpans() {

View File

@ -31,7 +31,7 @@ import org.apache.lucene.util.ToStringUtils;
import org.apache.lucene.search.Query;
/** Matches the union of its clauses.*/
public class SpanOrQuery extends SpanQuery {
public class SpanOrQuery extends SpanQuery implements Cloneable {
private List clauses;
private String field;
@ -80,6 +80,18 @@ public class SpanOrQuery extends SpanQuery {
}
}
public Object clone() {
int sz = clauses.size();
SpanQuery[] newClauses = new SpanQuery[sz];
for (int i = 0; i < sz; i++) {
SpanQuery clause = (SpanQuery) clauses.get(i);
newClauses[i] = (SpanQuery) clause.clone();
}
SpanOrQuery soq = new SpanOrQuery(newClauses);
return soq;
}
public Query rewrite(IndexReader reader) throws IOException {
SpanOrQuery clone = null;
for (int i = 0 ; i < clauses.size(); i++) {