@Override protected NodeBooleanQuery getTopLevelQuery() { return new NodeBooleanQuery(); }
/** * Adds a clause to a boolean query. * * @throws TooManyClauses * if the new number of clauses exceeds the maximum clause number * @see #getMaxClauseCount() */ public void add(final NodeQuery query, final NodeBooleanClause.Occur occur) { this.add(new NodeBooleanClause(query, occur)); }
/** * Returns an iterator on the clauses in this query. It implements the * {@link Iterable} interface to make it possible to do: * <pre>for (SirenBooleanClause clause : booleanQuery) {}</pre> */ public final Iterator<NodeBooleanClause> iterator() { return this.clauses().iterator(); }
private NodeBooleanQueryBuilder(final BooleanBag[] clauses) { nbq = new NodeBooleanQuery(); for (final BooleanBag bag : clauses) { for (final NodeBooleanClause clause : bag.toNodeBooleanClauses()) { nbq.add(clause); } } }
@Test public void testSetLevelConstraint() { final NodeTermQuery ntq = new NodeTermQuery(new Term("field", "value")); final NodeBooleanQuery bq = new NodeBooleanQuery(); bq.add(ntq, Occur.MUST); bq.setLevelConstraint(3); assertEquals(3, bq.getLevelConstraint()); // node queries in node boolean clauses must have been updated assertEquals(3, ntq.getLevelConstraint()); final NodeTermQuery ntq2 = new NodeTermQuery(new Term("field", "value")); bq.add(ntq2, Occur.MUST); // new clause must have been updated assertEquals(3, ntq2.getLevelConstraint()); }
@Test public void testSetAncestor() { final NodeTermQuery ntq = new NodeTermQuery(new Term("field", "value")); final NodeBooleanQuery bq1 = new NodeBooleanQuery(); bq1.add(ntq, Occur.MUST); final NodeBooleanQuery bq2 = new NodeBooleanQuery(); bq1.setAncestorPointer(bq2); assertSame(bq2, bq1.ancestor); // node queries in node boolean clauses must have been updated assertSame(bq2, ntq.ancestor); final NodeTermQuery ntq2 = new NodeTermQuery(new Term("field", "value")); bq1.add(ntq2, Occur.MUST); // new clause must have been updated assertSame(bq2, ntq2.ancestor); }
@Test public void testSetNodeConstraint() { final NodeTermQuery ntq = new NodeTermQuery(new Term("field", "value")); final NodeBooleanQuery bq = new NodeBooleanQuery(); bq.add(ntq, Occur.MUST); bq.setNodeConstraint(2,6); assertEquals(2, bq.lowerBound); assertEquals(6, bq.upperBound); // node queries in node boolean clauses must have been updated assertEquals(2, ntq.lowerBound); assertEquals(6, ntq.upperBound); final NodeTermQuery ntq2 = new NodeTermQuery(new Term("field", "value")); bq.add(ntq2, Occur.MUST); // new clause must have been updated assertEquals(2, ntq2.lowerBound); assertEquals(6, ntq2.upperBound); }
@Override public Query rewrite(final IndexReader reader) throws IOException { if (terms.isEmpty()) { final NodeBooleanQuery bq = new NodeBooleanQuery(); bq.setBoost(this.getBoost()); return bq; } else if (terms.size() == 1) { final NodeTermQuery tq = new NodeTermQuery(terms.get(0)); tq.setBoost(this.getBoost()); return tq; } else { return super.rewrite(reader); } }
/** This test should not be here, but it tests the fuzzy query rewrite mode (TOP_TERMS_SCORING_BOOLEAN_REWRITE) * with constant score and checks, that only the lower end of terms is put into the range */ public void testTopTermsRewrite() throws Exception { this.addDocuments(new String[]{"</computera>", "</computerb>", "</computerc>", "</computerd>", "</computere>", "</computerf>", "</computerg>", "</computerh>", "</computeri>", "</computerj>", "</computerk>"}); final NodeTermRangeQuery query = NodeTermRangeQuery.newStringRange(DEFAULT_TEST_FIELD, "/computerb", "/computerj", true, true); this.checkBooleanTerms(query, "/computerb", "/computerc", "/computerd", "/computere", "/computerf", "/computerg", "/computerh", "/computeri", "/computerj"); final int savedClauseCount = NodeBooleanQuery.getMaxClauseCount(); try { NodeBooleanQuery.setMaxClauseCount(3); this.checkBooleanTerms(query, "/computerb", "/computerc", "/computerd"); } finally { NodeBooleanQuery.setMaxClauseCount(savedClauseCount); } }
@Override protected int getMaxSize() { return NodeBooleanQuery.getMaxClauseCount(); }
/** Returns true iff <code>o</code> is equal to this. */ @Override public boolean equals(final Object o) { if (!(o instanceof NodeBooleanQuery)) return false; final NodeBooleanQuery other = (NodeBooleanQuery) o; return (this.getBoost() == other.getBoost()) && this.clauses.equals(other.clauses) && this.levelConstraint == other.levelConstraint && this.lowerBound == other.lowerBound && this.upperBound == other.upperBound; }
@Test public void testEquality() throws Exception { final NodeBooleanQuery bq1 = new NodeBooleanQuery(); bq1.add(new NodeTermQuery(new Term("field", "value1")), NodeBooleanClause.Occur.SHOULD); bq1.add(new NodeTermQuery(new Term("field", "value2")), NodeBooleanClause.Occur.SHOULD); final NodeBooleanQuery bq2 = new NodeBooleanQuery(); bq2.add(new NodeTermQuery(new Term("field", "value1")), NodeBooleanClause.Occur.SHOULD); bq2.add(new NodeTermQuery(new Term("field", "value2")), NodeBooleanClause.Occur.SHOULD); assertEquals(bq2, bq1); }
@Override protected int getMaxSize() { return NodeBooleanQuery.getMaxClauseCount(); }
/** Returns a hash code value for this object. */ @Override public int hashCode() { return Float.floatToIntBits(this.getBoost()) ^ clauses.hashCode() ^ levelConstraint ^ upperBound ^ lowerBound; }
final NodeBooleanQueryNode booleanNode = (NodeBooleanQueryNode) queryNode; final List<QueryNode> children = booleanNode.getChildren(); final NodeBooleanQuery bq = new NodeBooleanQuery(); mod = child; bq.add((NodeQuery) obj, NodeQueryBuilderUtil.getModifierValue(mod, NodeBooleanClause.Occur.SHOULD));
@Override protected void addClause(final NodeBooleanQuery topLevel, final Term term, final int docFreq, final float boost /*ignored*/, final TermContext states) { topLevel.add(new NodeTermQuery(term, states), Occur.SHOULD); }
@Override protected NodeBooleanQuery getTopLevelQuery() { return new NodeBooleanQuery(); }
@Override public Query rewrite(final IndexReader reader, final MultiNodeTermQuery query) throws IOException { final NodeBooleanQuery bq = SCORING_BOOLEAN_QUERY_REWRITE.rewrite(reader, query); // TODO: if empty boolean query return NullQuery? if (bq.clauses().isEmpty()) { return bq; } // strip the scores off final Query result = new NodeConstantScoreQuery(bq); result.setBoost(query.getBoost()); return result; }
@Override public void normalize(final float norm, float topLevelBoost) { // incorporate boost topLevelBoost *= NodeBooleanQuery.this.getBoost(); for (final Weight w : weights) { // normalize all clauses, (even if prohibited in case of side affects) w.normalize(norm, topLevelBoost); } }