Package org.sindice.siren.search.node

Examples of org.sindice.siren.search.node.TwigQuery


  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");
View Full Code Here


    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*");
  }
View Full Code Here

    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 } ]"));
  }
View Full Code Here

  public static class TwigQueryBuilder extends NodeQueryBuilder {

    protected TwigQuery twq;

    private TwigQueryBuilder(final int rootLevel, final NodeQueryBuilder builder) {
      twq = new TwigQuery(rootLevel);
      twq.addRoot(builder.getNodeQuery());
    }
View Full Code Here

      twq = new TwigQuery(rootLevel);
      twq.addRoot(builder.getNodeQuery());
    }

    private TwigQueryBuilder(final int rootLevel) {
      twq = new TwigQuery(rootLevel);
    }
View Full Code Here

  @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
    if (twigNode.hasRoot()) {
      final String rootExpr = twigNode.getRoot().toString();
      final String field = twigNode.getField().toString();
      query.addRoot((NodeQuery) keywordParser.parse(rootExpr, field));
    }

    // process child and descendant queries
    try {
      processChildren(children, query);
View Full Code Here

  public Query build(final QueryNode queryNode)
  throws QueryNodeException {
    final TwigQueryNode tqn = (TwigQueryNode) queryNode;
    final QueryNode root = tqn.getRoot();
    final QueryNode child = tqn.getChild();
    final TwigQuery twigQuery;
    final int rootLevel = tqn.getRootLevel();

    if (root == null && child == null) {
      throw new QueryNodeException(new MessageImpl(QueryParserMessages.EMPTY_MESSAGE));
    }
    if (tqn.getChildren().size() != 2) {
      throw new IllegalArgumentException("A TwigQueryNode cannot have more " +
          "than 2 children:\n" + tqn.getChildren().toString());
    }
    if (child instanceof WildcardNodeQueryNode &&
        root instanceof WildcardNodeQueryNode) {
      throw new QueryNodeException(new MessageImpl("Twig with both root and " +
          "child empty is not allowed."));
    }
    // Build the root operand
    if (root instanceof WildcardNodeQueryNode) { // Empty root query
      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,
        "Unable to get the root of the Twig query"));
      }
    }
    if (!(child instanceof WildcardNodeQueryNode)) {
      // Build the child operand
      final Object v = child.getTag(QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID);
      if (v instanceof ArrayQuery) { // array of children nodes
        final ArrayQueryNode aqn = (ArrayQueryNode) child;
        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));
        }
      } else if (v instanceof Query) {
        final NodeQuery valQuery = (NodeQuery) v;
        twigQuery.addChild(valQuery, Occur.MUST);
      } else {
        throw new QueryNodeException(new MessageImpl(QueryParserMessages.INVALID_SYNTAX,
          "Unexpected class of a Twig Query clause: " + v == null ? "null" : v.getClass().getName()));
      }
    }
View Full Code Here

      if (v instanceof Query) {
        if (v instanceof ArrayQuery) {
          /*
           * Nested array query. It is transformed as a TwigQuery with empty root
           */
          final TwigQuery twigQuery = new TwigQuery();
          for (final Query qn : ((ArrayQuery) v).getElements()) {
            final NodeQuery valQuery = (NodeQuery) qn;
            twigQuery.addChild(valQuery, NodeQueryBuilderUtil.getModifierValue(child, NodeBooleanClause.Occur.MUST));
          }
          arrayQuery.addElement(twigQuery);
        } else {
          arrayQuery.addElement((NodeQuery) v);
        }
View Full Code Here

TOP

Related Classes of org.sindice.siren.search.node.TwigQuery

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.