SpanQuantifierQuery,
update SimpleSpanQuery for single span
diff --git a/src/main/java/de/ids_mannheim/korap/query/SimpleSpanQuery.java b/src/main/java/de/ids_mannheim/korap/query/SimpleSpanQuery.java
index b053fd4..1e24d7f 100644
--- a/src/main/java/de/ids_mannheim/korap/query/SimpleSpanQuery.java
+++ b/src/main/java/de/ids_mannheim/korap/query/SimpleSpanQuery.java
@@ -8,25 +8,31 @@
import org.apache.lucene.search.Query;
import org.apache.lucene.search.spans.SpanQuery;
-/** An abstract class for a Spanquery having two clauses.
+/** A base class for Spanqueries
*
* @author margaretha
* */
-public abstract class SimpleSpanQuery extends SpanQuery implements Cloneable{
+public abstract class SimpleSpanQuery extends SpanQuery
+ implements Cloneable{
protected SpanQuery firstClause, secondClause;
private String field;
protected boolean collectPayloads;
+ public SimpleSpanQuery(SpanQuery firstClause, boolean collectPayloads) {
+ this.field = firstClause.getField();
+ this.setFirstClause(firstClause);
+ this.collectPayloads = collectPayloads;
+ }
+
public SimpleSpanQuery(SpanQuery firstClause, SpanQuery secondClause,
boolean collectPayloads) {
- this.field = secondClause.getField();
- if (!firstClause.getField().equals(field)){
- throw new IllegalArgumentException("Clauses must have the same field.");
+ this(firstClause,collectPayloads);
+ if (!secondClause.getField().equals(field)){
+ throw new IllegalArgumentException(
+ "Clauses must have the same field.");
}
- this.setFirstClause(firstClause);
this.setSecondClause(secondClause);
- this.collectPayloads = collectPayloads;
}
@Override
@@ -62,25 +68,32 @@
@Override
public void extractTerms(Set<Term> terms) {
firstClause.extractTerms(terms);
- secondClause.extractTerms(terms);
+ if (secondClause != null)
+ secondClause.extractTerms(terms);
};
@Override
public Query rewrite(IndexReader reader) throws IOException {
SimpleSpanQuery clone = null;
- SpanQuery query = (SpanQuery) firstClause.rewrite(reader);
- if (!query.equals(firstClause)) {
- if (clone == null) clone = clone();
- clone.firstClause = query;
- }
- query = (SpanQuery) secondClause.rewrite(reader);
- if (!query.equals(secondClause)) {
- if (clone == null) clone = clone();
- clone.secondClause = query;
+ clone = updateClone(reader, clone, firstClause, 1);
+ if (secondClause != null){
+ clone = updateClone(reader, clone, secondClause, 2);
}
return (clone != null ? clone : this );
}
+ private SimpleSpanQuery updateClone(IndexReader reader, SimpleSpanQuery clone,
+ SpanQuery sq, int clauseNumber) throws IOException{
+ SpanQuery query = (SpanQuery) sq.rewrite(reader);
+ if (!query.equals(sq)) {
+ if (clone == null) clone = clone();
+ if (clauseNumber == 1)
+ clone.firstClause = query;
+ else clone.secondClause = query;
+ }
+ return clone;
+ }
+
public abstract SimpleSpanQuery clone();
}
diff --git a/src/main/java/de/ids_mannheim/korap/query/SpanNextQuery.java b/src/main/java/de/ids_mannheim/korap/query/SpanNextQuery.java
index 6a51062..a15d80d 100644
--- a/src/main/java/de/ids_mannheim/korap/query/SpanNextQuery.java
+++ b/src/main/java/de/ids_mannheim/korap/query/SpanNextQuery.java
@@ -42,8 +42,7 @@
@Override
public Spans getSpans (final AtomicReaderContext context, Bits acceptDocs,
Map<Term,TermContext> termContexts) throws IOException {
- return (Spans) new NextSpans (this, context, acceptDocs,
- termContexts);
+ return (Spans) new NextSpans (this, context, acceptDocs, termContexts);
};
@Override
diff --git a/src/main/java/de/ids_mannheim/korap/query/SpanQuantifierQuery.java b/src/main/java/de/ids_mannheim/korap/query/SpanQuantifierQuery.java
new file mode 100644
index 0000000..85b881b
--- /dev/null
+++ b/src/main/java/de/ids_mannheim/korap/query/SpanQuantifierQuery.java
@@ -0,0 +1,74 @@
+package de.ids_mannheim.korap.query;
+
+import java.io.IOException;
+import java.util.Map;
+
+import org.apache.lucene.index.AtomicReaderContext;
+import org.apache.lucene.index.Term;
+import org.apache.lucene.index.TermContext;
+import org.apache.lucene.search.spans.SpanQuery;
+import org.apache.lucene.search.spans.Spans;
+import org.apache.lucene.util.Bits;
+import org.apache.lucene.util.ToStringUtils;
+
+import de.ids_mannheim.korap.query.spans.QuantifierSpans;
+
+public class SpanQuantifierQuery extends SimpleSpanQuery{
+
+ private int min, max;
+
+ public SpanQuantifierQuery(SpanQuery sq, int min, int max,
+ boolean collectPayloads) {
+ super(sq, collectPayloads);
+ this.min = min;
+ this.max = max;
+ }
+
+ @Override
+ public SimpleSpanQuery clone() {
+ SpanQuantifierQuery sq = new SpanQuantifierQuery(
+ (SpanQuery) this.firstClause.clone(),
+ this.min,
+ this.max,
+ this.collectPayloads);
+ sq.setBoost(getBoost());
+ return sq;
+ }
+
+ @Override
+ public Spans getSpans(AtomicReaderContext context, Bits acceptDocs,
+ Map<Term, TermContext> termContexts) throws IOException {
+ return new QuantifierSpans(this, context, acceptDocs, termContexts);
+ }
+
+ @Override
+ public String toString(String field) {
+ StringBuilder sb = new StringBuilder();
+ sb.append("spanQuantifier(");
+ sb.append(firstClause.toString(field));
+ sb.append("[");
+ sb.append(min);
+ sb.append(":");
+ sb.append(max);
+ sb.append("])");
+ sb.append(ToStringUtils.boost(getBoost()));
+ return sb.toString();
+ }
+
+ public int getMin() {
+ return min;
+ }
+
+ public void setMin(int min) {
+ this.min = min;
+ }
+
+ public int getMax() {
+ return max;
+ }
+
+ public void setMax(int max) {
+ this.max = max;
+ }
+
+}