public NotSpanWeight(final IndexSearcher searcher) throws IOException { // pass to child query the node constraints include.setNodeConstraint(lowerBound, upperBound); exclude.setNodeConstraint(lowerBound, upperBound); include.setLevelConstraint(levelConstraint); exclude.setLevelConstraint(levelConstraint); // transfer ancestor pointer to child include.setAncestorPointer(ancestor); exclude.setAncestorPointer(ancestor); this.includeWeight = include.createWeight(searcher); this.excludeWeight = exclude.createWeight(searcher); }
@Override public void extractTerms(final Set<Term> terms) { include.extractTerms(terms); }
public final void addClause(SpanQuery clause) { this.clauses.add(clause); // keep clause synchronised in term of constraint management clause.setLevelConstraint(levelConstraint); clause.setNodeConstraint(lowerBound, upperBound); clause.setAncestorPointer(ancestor); }
@Override public Query rewrite(IndexReader reader) throws IOException { PositionCheckSpanQuery clone = null; SpanQuery rewritten = (SpanQuery) match.rewrite(reader); if (rewritten != match) { clone = (PositionCheckSpanQuery) this.clone(); // transfer constraints rewritten.setNodeConstraint(lowerBound, upperBound); rewritten.setLevelConstraint(levelConstraint); // transfer ancestor pointer rewritten.setAncestorPointer(ancestor); clone.match = rewritten; } if (clone != null) { return clone; // some clauses rewrote } else { return this; // no clauses rewrote } }
SpanQuery query = (SpanQuery) c.rewrite(reader); query = (SpanQuery) query.clone(); // then clone before boost query.setBoost(this.getBoost() * query.getBoost()); query.setNodeConstraint(lowerBound, upperBound); query.setLevelConstraint(levelConstraint); query.setAncestorPointer(ancestor); final SpanQuery query = (SpanQuery) c.rewrite(reader); if (query != c) { // clause rewrote: must clone if (clone == null) { query.setNodeConstraint(lowerBound, upperBound); query.setLevelConstraint(levelConstraint); query.setAncestorPointer(ancestor);
@Override public void setAncestorPointer(final NodeQuery ancestor) { super.setAncestorPointer(ancestor); // keep clauses synchronised for (SpanQuery clause : clauses) { clause.setAncestorPointer(ancestor); } }
@Test public void testSetLevelConstraint() { TermSpanQuery term1 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "aaa")); TermSpanQuery term2 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "bbb")); NotSpanQuery not1 = new NotSpanQuery(term1, term2); not1.setLevelConstraint(3); assertEquals(3, not1.getLevelConstraint()); // Level constraint must have been transferred to the clauses assertEquals(3, not1.getInclude().getLevelConstraint()); assertEquals(3, not1.getExclude().getLevelConstraint()); }
@Override public boolean equals(Object o) { if (!(o instanceof NotSpanQuery)) return false; final NotSpanQuery other = (NotSpanQuery) o; return (this.getBoost() == other.getBoost()) && this.include != null && this.include.equals(other.include) && this.exclude != null && this.exclude.equals(other.exclude) && this.pre == other.pre && this.post == other.post && this.levelConstraint == other.levelConstraint && this.lowerBound == other.lowerBound && this.upperBound == other.upperBound; }
public PositionCheckSpanWeight(final IndexSearcher searcher) throws IOException { this.weight = PositionCheckSpanQuery.this.match.createWeight(searcher); }
@Override public Query rewrite(IndexReader reader) throws IOException { NotSpanQuery clone = null; SpanQuery rewrittenInclude = (SpanQuery) include.rewrite(reader); if (rewrittenInclude != include) { clone = this.clone(); clone.include = rewrittenInclude; } SpanQuery rewrittenExclude = (SpanQuery) exclude.rewrite(reader); if (rewrittenExclude != exclude) { if (clone == null) { clone = this.clone(); } clone.exclude = rewrittenExclude; } if (clone != null) { // transfer constraints clone.setNodeConstraint(lowerBound, upperBound); clone.setLevelConstraint(levelConstraint); // transfer ancestor pointer clone.setAncestorPointer(ancestor); return clone; // some clauses rewrote } else { return this; // no clauses rewrote } }
SpanQuery query = (SpanQuery) c.rewrite(reader); query = (SpanQuery) query.clone(); // then clone before boost query.setBoost(this.getBoost() * query.getBoost()); query.setNodeConstraint(lowerBound, upperBound); query.setLevelConstraint(levelConstraint); query.setAncestorPointer(ancestor); final SpanQuery query = (SpanQuery) c.rewrite(reader); if (query != c) { // clause rewrote: must clone if (clone == null) { query.setNodeConstraint(lowerBound, upperBound); query.setLevelConstraint(levelConstraint); query.setAncestorPointer(ancestor);
@Override public Query rewrite(IndexReader reader) throws IOException { final Query q = query.rewrite(reader); if (!(q instanceof SpanQuery)) { throw new UnsupportedOperationException("You can only use MultiTermSpanQuery with a suitable SpanRewriteMethod."); } if (q != this.query) { // transfer constraints ((SpanQuery) q).setNodeConstraint(lowerBound, upperBound); ((SpanQuery) q).setLevelConstraint(levelConstraint); // transfer ancestor pointer ((SpanQuery) q).setAncestorPointer(ancestor); } return q; }
@Override public void setAncestorPointer(final NodeQuery ancestor) { super.setAncestorPointer(ancestor); // keep clauses synchronised this.include.setAncestorPointer(ancestor); this.exclude.setAncestorPointer(ancestor); }
@Test public void testSetLevelConstraint() { TermSpanQuery term1 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "aaa")); TermSpanQuery term2 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "bbb")); OrSpanQuery or1 = new OrSpanQuery(new SpanQuery[] {term1, term2}); or1.setLevelConstraint(3); assertEquals(3, or1.getLevelConstraint()); // Level constraint must have been transferred to the clauses assertEquals(3, or1.getClauses()[0].getLevelConstraint()); assertEquals(3, or1.getClauses()[1].getLevelConstraint()); NodeSpanQuery term3 = new NodeSpanQuery(ntq("aaa").getQuery()); NodeSpanQuery term4 = new NodeSpanQuery(ntq("bbb").getQuery()); OrSpanQuery or2 = new OrSpanQuery(new SpanQuery[] {term3, term4}); or2.setLevelConstraint(4); OrSpanQuery or3 = new OrSpanQuery(new SpanQuery[] {or1, or2}); or3.setLevelConstraint(6); // Level constraint must have been transferred to the clauses assertEquals(6, or1.getLevelConstraint()); assertEquals(6, or2.getLevelConstraint()); }
@Override public int hashCode() { final int prime = 31; int result = super.hashCode(); result = prime * result + Float.floatToIntBits(this.getBoost()); result = prime * result + query.hashCode(); result = prime * result + lowerBound; result = prime * result + upperBound; result = prime * result + levelConstraint; return result; }
@Override public boolean equals(Object o) { if (!(o instanceof PositionRangeSpanQuery)) return false; final PositionRangeSpanQuery other = (PositionRangeSpanQuery) o; return (this.getBoost() == other.getBoost()) && this.match.equals(other.match) && this.end == other.end && this.start == other.start && this.levelConstraint == other.levelConstraint && this.lowerBound == other.lowerBound && this.upperBound == other.upperBound; }
public OrSpanWeight(final IndexSearcher searcher) throws IOException { weights = new ArrayList<Weight>(clauses.size()); for (int i = 0; i < clauses.size(); i++) { final SpanQuery c = clauses.get(i); // pass to child query the node constraints c.setNodeConstraint(lowerBound, upperBound); c.setLevelConstraint(levelConstraint); // transfer ancestor pointer to child c.setAncestorPointer(ancestor); weights.add(c.createWeight(searcher)); } }
@Override public void setAncestorPointer(final NodeQuery ancestor) { super.setAncestorPointer(ancestor); this.match.setAncestorPointer(ancestor); }