/** * Adds a clause to a tuple 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) { super.addChild(query, occur); }
public TwigQueryBuilder with(final TwigChildBuilder child) { twq.addChild(child.nbq, Occur.MUST); return this; }
public TwigQueryBuilder without(final TwigChildBuilder child) { twq.addChild(child.nbq, Occur.MUST_NOT); return this; }
public TwigQueryBuilder optional(final TwigChildBuilder child) { twq.addChild(child.nbq, Occur.SHOULD); return this; }
/** * Adds a clause to a tuple query. * * @throws TooManyClauses * if the new number of clauses exceeds the maximum clause number * @see #getMaxClauseCount() */ public void add(final NodeBooleanClause clause) { super.addChild(clause.getQuery(), clause.getOccur()); }
public TwigQueryBuilder optional(final NodeQueryBuilder nq) { twq.addChild(nq.getNodeQuery(), Occur.SHOULD); return this; }
public TwigQueryBuilder without(final NodeQueryBuilder nq) { twq.addChild(nq.getNodeQuery(), Occur.MUST_NOT); return this; }
public TwigQueryBuilder with(final NodeQueryBuilder nq) { twq.addChild(nq.getNodeQuery(), Occur.MUST); return this; }
private static void processChildren(final List<QueryNode> children, final TwigQuery query) { for (final QueryNode child : children) { final Object obj = child.getTag(QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID); final NodeQuery nodeQuery = (NodeQuery) obj; // Append child queries if (child instanceof ChildQueryNode) { query.addChild(nodeQuery, getModifierValue(child)); } // Append descendant queries else if (child instanceof DescendantQueryNode) { // A descendant node must always have a level constraint if (child.getTag(LevelPropertyParser.LEVEL_PROPERTY) == null) { throw new IllegalArgumentException("Invalid DescendantQueryNode received: no level constraint defined"); } // set level constraint final int nodeLevel = (Integer) child.getTag(LevelPropertyParser.LEVEL_PROPERTY); // add descendant query query.addDescendant(nodeLevel, nodeQuery, getModifierValue(child)); } else { throw new IllegalArgumentException("Invalid QueryNode received: " + child.getClass().getSimpleName()); } } }
@Test public void testPrefixQuery() throws Exception { final Query ntq = new LuceneProxyNodeQuery( new NodePrefixQuery(new Term(SirenTestCase.DEFAULT_TEST_FIELD, "lit")) ); this._assertSirenQuery(ntq, "lit*"); final TwigQuery twq = new TwigQuery(1); twq.addChild(new NodePrefixQuery(new Term(SirenTestCase.DEFAULT_TEST_FIELD, "lit")), NodeBooleanClause.Occur.MUST); this._assertSirenQuery(new LuceneProxyNodeQuery(twq), "* : lit*"); }
final List<Query> children = ((ArrayQuery) v).getElements(); for (int i = 0; i < children.size(); i++) { twigQuery.addChild((NodeQuery) children.get(i), NodeQueryBuilderUtil.getModifierValue(aqn.getChildren().get(i), NodeBooleanClause.Occur.MUST)); twigQuery.addChild(valQuery, Occur.MUST); } else { throw new QueryNodeException(new MessageImpl(QueryParserMessages.INVALID_SYNTAX,
for (final Query qn : ((ArrayQuery) v).getElements()) { final NodeQuery valQuery = (NodeQuery) qn; twigQuery.addChild(valQuery, NodeQueryBuilderUtil.getModifierValue(child, NodeBooleanClause.Occur.MUST));
@Test public void testFuzzyQuery2() throws Exception { final NodeQuery q1 = new NodeFuzzyQuery(new Term(SirenTestCase.DEFAULT_TEST_FIELD, "michel")); this._assertSirenQuery(new LuceneProxyNodeQuery(q1), "michel~"); final TwigQuery q2 = new TwigQuery(1); q2.addChild(q1, NodeBooleanClause.Occur.MUST); this._assertSirenQuery(new LuceneProxyNodeQuery(q2), "* : michel~"); final int numEdits = FuzzyQuery.floatToEdits(0.8f, "michel".codePointCount(0, "michel".length())); final NodeQuery q3 = new NodeFuzzyQuery(new Term(SirenTestCase.DEFAULT_TEST_FIELD, "michel"), numEdits); this._assertSirenQuery(new LuceneProxyNodeQuery(q3), "michel~0.8"); // first tilde is escaped, not the second one final NodeQuery q4 = new NodeFuzzyQuery(new Term(SirenTestCase.DEFAULT_TEST_FIELD, "http://sw.deri.org/~aida")); this._assertSirenQuery(new LuceneProxyNodeQuery(q4), "'http://sw.deri.org/~aida'~"); }
@Test public void testRewriteTwigQueryRoot() throws IOException { // if the root node of a twig query is another twig query, then they must // be merged final TwigQuery tq2 = new TwigQuery(2); tq2.addRoot(new NodeTermQuery(new Term("root2", "root2"))); tq2.addChild(new NodeTermQuery(new Term("child2", "child2")), Occur.MUST); tq2.setBoost(0.5f); final TwigQuery tq1 = new TwigQuery(); tq1.addRoot(tq2); tq1.addChild(new NodeTermQuery(new Term("child1", "child1")), Occur.MUST); tq1.setBoost(0.5f); final NodeQuery q = (NodeQuery) tq1.rewrite(null); assertTrue(q instanceof TwigQuery); final TwigQuery tq = (TwigQuery) q; // level and boosst must be the same than for tq1 assertEquals(1, tq.getLevelConstraint()); assertEquals(tq.getBoost(), tq1.getBoost(), 0); // root must not be the same than for tq2 (it has been cloned) assertNotSame(tq.getRoot(), tq2.getRoot()); // root must be equal to the root of tq2 assertEquals(tq.getRoot(), tq2.getRoot()); // clauses must be merged assertEquals(2, tq.clauses().size()); for (final NodeBooleanClause clause : tq.clauses()) { assertEquals(2, clause.getQuery().getLevelConstraint()); assertSame(tq.getRoot(), clause.getQuery().ancestor); } }
@Test public void testSetAncestorPointer() { final NodeTermQuery term = new NodeTermQuery(new Term("field", "value")); final TwigQuery tq1 = new TwigQuery(); tq1.addDescendant(2, term, Occur.MUST); // ancestor of term query must be the root of the twig assertSame(tq1.getRoot(), term.ancestor); // ancestor of the twig must be null assertNull(tq1.ancestor); final TwigQuery tq2 = new TwigQuery(); tq2.addChild(tq1, Occur.MUST); // ancestor of tq1 and of its root must have been updated assertSame(tq2.getRoot(), tq1.ancestor); assertSame(tq2.getRoot(), tq1.getRoot().ancestor); // ancestor of tq1's descendant must have not changed assertEquals(4, tq1.clauses().get(0).getQuery().getLevelConstraint()); final TwigQuery tq3 = new TwigQuery(3); tq3.addRoot(tq2); // ancestor of tq2 and of its root must be the ancestor of tq3 assertSame(tq3.ancestor, tq2.ancestor); assertSame(tq2.ancestor, tq2.getRoot().ancestor); }
@Test public void testSetLevelConstraint() { final TwigQuery tq1 = new TwigQuery(2); tq1.addDescendant(2, new NodeTermQuery(new Term("field", "value")), Occur.MUST); assertEquals(2, tq1.getLevelConstraint()); // Descendant node level must be relative to the twig level assertEquals(4, tq1.clauses().get(0).getQuery().getLevelConstraint()); tq1.setLevelConstraint(3); assertEquals(3, tq1.getLevelConstraint()); // level of descendant node must have been updated assertEquals(5, tq1.clauses().get(0).getQuery().getLevelConstraint()); final TwigQuery tq2 = new TwigQuery(); tq2.addChild(tq1, Occur.MUST); // level of tq1 must have been updated assertEquals(2, tq1.getLevelConstraint()); // level of descendant node must have been updated assertEquals(4, tq1.clauses().get(0).getQuery().getLevelConstraint()); final TwigQuery tq3 = new TwigQuery(3); tq3.addRoot(tq2); // level of tq2 must have been updated assertEquals(3, tq2.getLevelConstraint()); // level of tq1 must have been updated assertEquals(4, tq1.getLevelConstraint()); // level of descendant node must have been updated assertEquals(6, tq1.clauses().get(0).getQuery().getLevelConstraint()); }
query.addChild((NodeQuery) clause.getQuery().toQuery(false),clause.getNodeBooleanOccur());
@Test public void testRangeQueries() throws Exception { NodeQuery q = new NodeTermRangeQuery(SirenTestCase.DEFAULT_TEST_FIELD, new BytesRef("a"), new BytesRef("b"), true, true); this._assertSirenQuery(new LuceneProxyNodeQuery(q), "[ a TO b ]"); q = new NodeTermRangeQuery(SirenTestCase.DEFAULT_TEST_FIELD, new BytesRef("a"), new BytesRef("b"), false, true); this._assertSirenQuery(new LuceneProxyNodeQuery(q), "{ a TO b ]"); q = new NodeTermRangeQuery(SirenTestCase.DEFAULT_TEST_FIELD, new BytesRef("a"), new BytesRef("b"), true, false); this._assertSirenQuery(new LuceneProxyNodeQuery(q), "[ a TO b }"); q = new NodeTermRangeQuery(SirenTestCase.DEFAULT_TEST_FIELD, new BytesRef("a"), new BytesRef("b"), false, false); this._assertSirenQuery(new LuceneProxyNodeQuery(q), "{ a TO b }"); final TwigQuery twq1 = new TwigQuery(1); twq1.addChild(q, NodeBooleanClause.Occur.MUST); // TODO parsing the output of #toString of twq1 is not possible because of GH-52 assertEquals(new LuceneProxyNodeQuery(twq1), this.parse(null, "* : { a TO b }")); final TwigQuery twq2 = new TwigQuery(1); twq2.addChild(new NodeTermRangeQuery(SirenTestCase.DEFAULT_TEST_FIELD, new BytesRef("a"), new BytesRef("b"), true, true), NodeBooleanClause.Occur.MUST); twq2.addChild(q, NodeBooleanClause.Occur.MUST); assertEquals(new LuceneProxyNodeQuery(twq2), this.parse(null, "* : [ [ a TO b ], { a TO b } ]")); }