/** * 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); }
private TwigQueryBuilder(final int rootLevel) { twq = new TwigQuery(rootLevel); }
/** * Process the child and descendant queries */ protected final 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, this.getModifierValue(child)); } // Append descendant queries else if (child instanceof DescendantQueryNode) { // A descendant node must always have a level constraint if (!child.containsTag(LevelPropertyParser.LEVEL_PROPERTY)) { 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, this.getModifierValue(child)); } else { throw new IllegalArgumentException("Invalid QueryNode received: " + child.getClass().getSimpleName()); } } }
@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()); }
@Override public Query toQuery(final boolean proxy) throws QueryNodeException { final com.sindicetech.siren.search.node.TwigQuery query = new com.sindicetech.siren.search.node.TwigQuery(); query.addChild((NodeQuery) clause.getQuery().toQuery(false), clause.getNodeBooleanOccur()); query.addDescendant(level, (NodeQuery) clause.getQuery().toQuery(false), clause.getNodeBooleanOccur()); query.setLevelConstraint(this.getLevel()); query.setNodeConstraint(this.getLowerBound(), this.getUpperBound()); query.setBoost(this.getBoost());
@Test public void testLevelAndNodeConstraintPropagation() throws IOException { final TwigQuery tq1 = new TwigQuery(2); tq1.addDescendant(2, new NodeTermQuery(new Term("field", "aaa")), NodeBooleanClause.Occur.MUST); tq1.setNodeConstraint(1,2); final TwigQuery tq2 = new TwigQuery(2); tq2.addChild(new NodeTermQuery(new Term("field", "bbb")), NodeBooleanClause.Occur.MUST); final BooleanSpanQuery bsq1 = new BooleanSpanQuery(0, true); bsq1.add(new NodeSpanQuery(tq1), NodeBooleanClause.Occur.MUST); bsq1.add(new NodeSpanQuery(tq2), NodeBooleanClause.Occur.MUST); // Level constraints applied on the twig must not be modified assertEquals(2, tq1.getLevelConstraint()); assertEquals(4, tq1.clauses().get(0).getQuery().getLevelConstraint()); assertEquals(2, tq2.getLevelConstraint()); assertEquals(3, tq2.clauses().get(0).getQuery().getLevelConstraint()); // Node constraints applied on the twig must not be modified assertEquals(1, tq1.getNodeConstraint()[0]); assertEquals(2, tq1.getNodeConstraint()[1]); // Constraints should not be modified after a rewrite final BooleanSpanQuery rewritten = (BooleanSpanQuery) bsq1.rewrite(reader); NodeSpanQuery nodeSpanQuery = (NodeSpanQuery) rewritten.getClauses()[0].getQuery(); assertEquals(2, nodeSpanQuery.getQuery().getLevelConstraint()); assertEquals(1, nodeSpanQuery.getQuery().getNodeConstraint()[0]); assertEquals(2, nodeSpanQuery.getQuery().getNodeConstraint()[1]); nodeSpanQuery = (NodeSpanQuery) rewritten.getClauses()[1].getQuery(); assertEquals(2, nodeSpanQuery.getQuery().getLevelConstraint()); }
twigQuery = new TwigQuery(rootLevel); } else { final Object attQuery = root.getTag(QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID); if (attQuery != null) { twigQuery = new TwigQuery(rootLevel); twigQuery.addRoot((NodeQuery) attQuery); } else { throw new QueryNodeException(new MessageImpl(QueryParserMessages.INVALID_SYNTAX, 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,
@Test public void testRewriteEmptyRoot() throws IOException { TwigQuery tq = new TwigQuery(2); tq.setBoost(0.5f); tq.addDescendant(2, ntq, Occur.MUST); NodeQuery q = (NodeQuery) tq.rewrite(null); assertTrue(q instanceof AncestorFilterQuery); assertEquals(2, q.getLevelConstraint()); assertEquals(tq.getBoost(), q.getBoost(), 0); tq.addDescendant(2, new NodeTermQuery(new Term("field", "value")), Occur.MUST); q = (NodeQuery) tq.rewrite(null); assertSame(tq, q); tq = new TwigQuery(2); tq.setNodeConstraint(3); tq.addDescendant(2, new NodeTermQuery(new Term("field", "value")), Occur.MUST); q = (NodeQuery) tq.rewrite(null); assertTrue(q instanceof AncestorFilterQuery); assertEquals(3, q.lowerBound);
if (this.getBoost() != 1.0f) { // incorporate boost if (query == root) { // if rewrite was no-op query.setBoost(this.getBoost() * query.getBoost()); if (this.getBoost() != 1.0f) { query.setBoost(this.getBoost()); final TwigQuery clone = (TwigQuery) this.clone(); clone.root = ((TwigQuery) clone.root).getRoot(); clone.setAncestorPointer(ancestor); return clone.rewrite(reader); clone = this.rewriteRoot(clone, reader); clone = this.rewriteClauses(clone, reader); clone.setAncestorPointer(ancestor); return clone;
@Test public void testRewriteEmptyClauses() throws IOException { final TwigQuery tq = new TwigQuery(2); tq.addRoot(new NodeTermQuery(new Term("field", "value"))); tq.setBoost(0.5f); // it must be rewritten into the root query final NodeQuery q = (NodeQuery) tq.rewrite(null); assertTrue(q instanceof NodeTermQuery); assertEquals(2, q.getLevelConstraint()); assertEquals(tq.getBoost(), q.getBoost(), 0); assertSame(tq.ancestor, q.ancestor); }
@Override public TwigQuery build(final QueryNode queryNode) throws QueryNodeException { final TwigQueryNode twigNode = (TwigQueryNode) queryNode; final List<QueryNode> children = twigNode.getChildren(); final TwigQuery query = new TwigQuery(); // check if the node has a level constraint if (twigNode.getTag(LevelPropertyParser.LEVEL_PROPERTY) != null) { query.setLevelConstraint((Integer) twigNode.getTag(LevelPropertyParser.LEVEL_PROPERTY)); } // check if the node has a node range constraint if (twigNode.getTag(RangePropertyParser.RANGE_PROPERTY) != null) { final int[] range = (int[]) twigNode.getTag(RangePropertyParser.RANGE_PROPERTY); query.setNodeConstraint(range[0], range[1]); } // process root query this.processRoot(twigNode, query); // process child and descendant queries try { this.processChildren(children, query); } catch (final TooManyClauses ex) { throw new QueryNodeException(new MessageImpl( QueryParserMessages.TOO_MANY_BOOLEAN_CLAUSES, BooleanQuery.getMaxClauseCount(), twigNode.toQueryString(new EscapeQuerySyntaxImpl())), ex); } return query; }
@Test public void testAsTwigDescendant() throws Exception { this.addDocuments( doc(token("aa", node(1)), token("aaa", node(1,1)), token("bbb", node(1,3))), doc(token("aa", node(1)), token("aaa", node(1,1,1)), token("bbb", node(1,1,3))), doc(token("bb", node(1)), token("aaa", node(1,1,1)), token("bbb", node(1,1,3))) ); NodeSpanQuery term1 = new NodeSpanQuery(ntq("aaa").getQuery()); NodeSpanQuery term2 = new NodeSpanQuery(ntq("bbb").getQuery()); BooleanSpanQuery bsq = new BooleanSpanQuery(1, true); bsq.add(term1, NodeBooleanClause.Occur.MUST); bsq.add(term2, NodeBooleanClause.Occur.MUST); TwigQuery twig = new TwigQuery(); twig.addRoot(ntq("aa").getQuery()); twig.addDescendant(2, bsq, NodeBooleanClause.Occur.MUST); Query query = new LuceneProxyNodeQuery(twig); TopDocs hits = searcher.search(query, 100); assertEquals(1, hits.totalHits); }
LuceneProxyNodeQuery q = (LuceneProxyNodeQuery) output; com.sindicetech.siren.search.node.TwigQuery t = (com.sindicetech.siren.search.node.TwigQuery) q.getNodeQuery(); com.sindicetech.siren.search.node.NodeQuery root = t.getRoot(); q = (LuceneProxyNodeQuery) output; t = (com.sindicetech.siren.search.node.TwigQuery) q.getNodeQuery(); root = t.getRoot(); com.sindicetech.siren.search.node.NodeQuery clause = t.getClauses()[0].getQuery(); assertTrue(clause instanceof com.sindicetech.siren.search.node.TwigQuery); root = ((com.sindicetech.siren.search.node.TwigQuery) clause).getRoot(); com.sindicetech.siren.search.node.NodeQuery node = ((com.sindicetech.siren.search.node.TwigQuery) clause).getClauses()[0].getQuery(); assertTrue(node instanceof DatatypedNodeQuery); datatype = ((DatatypedNodeQuery) node).getDatatype();