Added rewrite methods to all SpanQuery subclasses that nest SpanQuerys

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@289556 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Erik Hatcher 2005-09-16 14:17:32 +00:00
parent b32b4b2235
commit f32d7e7786
5 changed files with 83 additions and 2 deletions

View File

@ -145,6 +145,7 @@ New features
20. Added a new class MatchAllDocsQuery that matches all documents.
(John Wang via Daniel Naber, bug #34946)
API Changes
1. Several methods and fields have been deprecated. The API documentation
@ -165,6 +166,10 @@ API Changes
4. Add a serializable Parameter Class to standardize parameter enum
classes in BooleanClause and Field. (Christoph)
5. Added rewrite methods to all SpanQuery subclasses that nest other SpanQuerys.
This allows custom SpanQuery subclasses that rewrite (for term expansion, for
example) to nest within the built-in SpanQuery classes successfully.
Bug fixes
1. The JSP demo page (src/jsp/results.jsp) now properly closes the

View File

@ -21,6 +21,7 @@ import java.io.IOException;
import java.util.Collection;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.Query;
/** Matches spans near the beginning of a field. */
public class SpanFirstQuery extends SpanQuery {
@ -87,4 +88,19 @@ public class SpanFirstQuery extends SpanQuery {
};
}
public Query rewrite(IndexReader reader) throws IOException {
SpanFirstQuery clone = null;
SpanQuery rewritten = (SpanQuery) match.rewrite(reader);
if (rewritten != match) {
clone = (SpanFirstQuery) this.clone();
clone.match = rewritten;
}
if (clone != null) {
return clone; // some clauses rewrote
} else {
return this; // no clauses rewrote
}
}
}

View File

@ -25,6 +25,7 @@ import java.util.Iterator;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.Query;
/** Matches spans which are near one another. One can specify <i>slop</i>, the
* maximum number of intervening unmatched positions, as well as whether
@ -110,6 +111,24 @@ public class SpanNearQuery extends SpanQuery {
return new NearSpans(this, reader);
}
public Query rewrite(IndexReader reader) throws IOException {
SpanNearQuery clone = null;
for (int i = 0 ; i < clauses.size(); i++) {
SpanQuery c = (SpanQuery)clauses.get(i);
SpanQuery query = (SpanQuery) c.rewrite(reader);
if (query != c) { // clause rewrote: must clone
if (clone == null)
clone = (SpanNearQuery) this.clone();
clone.clauses.set(i,query);
}
}
if (clone != null) {
return clone; // some clauses rewrote
} else {
return this; // no clauses rewrote
}
}
/** Returns true iff <code>o</code> is equal to this. */
public boolean equals(Object o) {
if (this == o) return true;

View File

@ -21,6 +21,7 @@ import java.io.IOException;
import java.util.Collection;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.Query;
/** Removes matches which overlap with another SpanQuery. */
public class SpanNotQuery extends SpanQuery {
@ -127,4 +128,25 @@ public class SpanNotQuery extends SpanQuery {
};
}
public Query rewrite(IndexReader reader) throws IOException {
SpanNotQuery clone = null;
SpanQuery rewrittenInclude = (SpanQuery) include.rewrite(reader);
if (rewrittenInclude != include) {
clone = (SpanNotQuery) this.clone();
clone.include = rewrittenInclude;
}
SpanQuery rewrittenExclude = (SpanQuery) exclude.rewrite(reader);
if (rewrittenExclude != include) {
if (clone == null) clone = (SpanNotQuery) this.clone();
clone.exclude = rewrittenExclude;
}
if (clone != null) {
return clone; // some clauses rewrote
} else {
return this; // no clauses rewrote
}
}
}

View File

@ -25,6 +25,7 @@ import java.util.Iterator;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.util.PriorityQueue;
import org.apache.lucene.search.Query;
/** Matches the union of its clauses.*/
public class SpanOrQuery extends SpanQuery {
@ -64,6 +65,24 @@ public class SpanOrQuery extends SpanQuery {
return terms;
}
public Query rewrite(IndexReader reader) throws IOException {
SpanOrQuery clone = null;
for (int i = 0 ; i < clauses.size(); i++) {
SpanQuery c = (SpanQuery)clauses.get(i);
SpanQuery query = (SpanQuery) c.rewrite(reader);
if (query != c) { // clause rewrote: must clone
if (clone == null)
clone = (SpanOrQuery) this.clone();
clone.clauses.set(i,query);
}
}
if (clone != null) {
return clone; // some clauses rewrote
} else {
return this; // no clauses rewrote
}
}
public String toString(String field) {
StringBuffer buffer = new StringBuffer();
buffer.append("spanOr([");