Search in sources :

Example 1 with StaticOperand

use of javax.jcr.query.qom.StaticOperand in project jackrabbit by apache.

the class QueryObjectModelFactoryTest method testFullTextSearchWithBindVariableValue.

/**
     * Test case for {@link QueryObjectModelFactory#fullTextSearch(String, String, StaticOperand)}
     */
public void testFullTextSearchWithBindVariableValue() throws RepositoryException {
    FullTextSearch ftSearch = qf.fullTextSearch(SELECTOR_NAME1, propertyName1, qf.bindVariable(VARIABLE_NAME));
    assertEquals("Wrong selector name", SELECTOR_NAME1, ftSearch.getSelectorName());
    assertEquals("Wrong propertyName", propertyName1, ftSearch.getPropertyName());
    StaticOperand op = ftSearch.getFullTextSearchExpression();
    assertNotNull(op);
    assertTrue("not a BindVariableValue", op instanceof BindVariableValue);
    BindVariableValue value = (BindVariableValue) op;
    assertEquals(VARIABLE_NAME, value.getBindVariableName());
}
Also used : BindVariableValue(javax.jcr.query.qom.BindVariableValue) StaticOperand(javax.jcr.query.qom.StaticOperand) FullTextSearch(javax.jcr.query.qom.FullTextSearch)

Example 2 with StaticOperand

use of javax.jcr.query.qom.StaticOperand in project jackrabbit by apache.

the class Parser method parseStaticOperand.

private StaticOperand parseStaticOperand() throws RepositoryException {
    if (currentTokenType == PLUS) {
        read();
    } else if (currentTokenType == MINUS) {
        read();
        if (currentTokenType != VALUE) {
            throw getSyntaxError("number");
        }
        int valueType = currentValue.getType();
        switch(valueType) {
            case PropertyType.LONG:
                currentValue = valueFactory.createValue(-currentValue.getLong());
                break;
            case PropertyType.DOUBLE:
                currentValue = valueFactory.createValue(-currentValue.getDouble());
                break;
            case PropertyType.BOOLEAN:
                currentValue = valueFactory.createValue(!currentValue.getBoolean());
                break;
            case PropertyType.DECIMAL:
                currentValue = valueFactory.createValue(currentValue.getDecimal().negate());
                break;
            default:
                throw getSyntaxError("Illegal operation: -" + currentValue);
        }
    }
    if (currentTokenType == VALUE) {
        Literal literal = getUncastLiteral(currentValue);
        read();
        return literal;
    } else if (currentTokenType == PARAMETER) {
        read();
        String name = readName();
        if (readIf(":")) {
            name = name + ":" + readName();
        }
        BindVariableValue var = bindVariables.get(name);
        if (var == null) {
            var = factory.bindVariable(name);
            bindVariables.put(name, var);
        }
        return var;
    } else if (readIf("TRUE")) {
        Literal literal = getUncastLiteral(valueFactory.createValue(true));
        return literal;
    } else if (readIf("FALSE")) {
        Literal literal = getUncastLiteral(valueFactory.createValue(false));
        return literal;
    } else if (readIf("CAST")) {
        read("(");
        StaticOperand op = parseStaticOperand();
        if (!(op instanceof Literal)) {
            throw getSyntaxError("literal");
        }
        Literal literal = (Literal) op;
        Value value = literal.getLiteralValue();
        read("AS");
        value = parseCastAs(value);
        read(")");
        // CastLiteral
        literal = factory.literal(value);
        return literal;
    } else {
        throw getSyntaxError("static operand");
    }
}
Also used : BindVariableValue(javax.jcr.query.qom.BindVariableValue) StaticOperand(javax.jcr.query.qom.StaticOperand) Literal(javax.jcr.query.qom.Literal) Value(javax.jcr.Value) PropertyValue(javax.jcr.query.qom.PropertyValue) BindVariableValue(javax.jcr.query.qom.BindVariableValue)

Example 3 with StaticOperand

use of javax.jcr.query.qom.StaticOperand in project jackrabbit by apache.

the class LuceneQueryFactory method mapConstraintToQueryAndFilter.

protected Predicate mapConstraintToQueryAndFilter(QueryPair query, Constraint constraint, Map<String, NodeType> selectorMap, JackrabbitIndexSearcher searcher, IndexReader reader) throws RepositoryException, IOException {
    Predicate filter = Predicate.TRUE;
    if (constraint instanceof And) {
        And and = (And) constraint;
        filter = mapConstraintToQueryAndFilter(query, and.getConstraint1(), selectorMap, searcher, reader);
        Predicate other = mapConstraintToQueryAndFilter(query, and.getConstraint2(), selectorMap, searcher, reader);
        if (filter == Predicate.TRUE) {
            filter = other;
        } else if (other != Predicate.TRUE) {
            filter = Predicates.and(filter, other);
        }
    } else if (constraint instanceof Comparison) {
        Comparison c = (Comparison) constraint;
        Transform transform = new Transform(c.getOperand1());
        DynamicOperand left = transform.operand;
        final String operator = c.getOperator();
        StaticOperand right = c.getOperand2();
        if (left instanceof Length || left instanceof FullTextSearchScore || (((!JCR_OPERATOR_EQUAL_TO.equals(operator) && !JCR_OPERATOR_LIKE.equals(operator)) || transform.transform != TRANSFORM_NONE) && (left instanceof NodeName || left instanceof NodeLocalName))) {
            try {
                int type = PropertyType.UNDEFINED;
                if (left instanceof Length) {
                    type = PropertyType.LONG;
                } else if (left instanceof FullTextSearchScore) {
                    type = PropertyType.DOUBLE;
                }
                final DynamicOperand operand = c.getOperand1();
                final Value value = evaluator.getValue(right, type);
                filter = new RowPredicate() {

                    @Override
                    protected boolean evaluate(Row row) throws RepositoryException {
                        return new ValueComparator().evaluate(operator, evaluator.getValue(operand, row), value);
                    }
                };
            } catch (ValueFormatException e) {
                throw new InvalidQueryException(e);
            }
        } else {
            Query cq = getComparisonQuery(left, transform.transform, operator, right, selectorMap);
            query.subQuery.add(cq, MUST);
        }
    } else if (constraint instanceof DescendantNode) {
        final DescendantNode descendantNode = (DescendantNode) constraint;
        Query context = getNodeIdQuery(UUID, descendantNode.getAncestorPath());
        query.mainQuery = new DescendantSelfAxisQuery(context, query.subQuery, false);
    } else {
        query.subQuery.add(create(constraint, selectorMap, searcher), MUST);
    }
    return filter;
}
Also used : Query(org.apache.lucene.search.Query) BooleanQuery(org.apache.lucene.search.BooleanQuery) DynamicOperand(javax.jcr.query.qom.DynamicOperand) StaticOperand(javax.jcr.query.qom.StaticOperand) NodeName(javax.jcr.query.qom.NodeName) ValueComparator(org.apache.jackrabbit.core.query.lucene.join.ValueComparator) Predicate(org.apache.jackrabbit.commons.predicate.Predicate) RowPredicate(org.apache.jackrabbit.commons.predicate.RowPredicate) NodeLocalName(javax.jcr.query.qom.NodeLocalName) RowPredicate(org.apache.jackrabbit.commons.predicate.RowPredicate) Comparison(javax.jcr.query.qom.Comparison) Length(javax.jcr.query.qom.Length) And(javax.jcr.query.qom.And) DescendantNode(javax.jcr.query.qom.DescendantNode) Value(javax.jcr.Value) PropertyValue(javax.jcr.query.qom.PropertyValue) ValueFormatException(javax.jcr.ValueFormatException) FullTextSearchScore(javax.jcr.query.qom.FullTextSearchScore) SelectorRow(org.apache.jackrabbit.core.query.lucene.join.SelectorRow) Row(javax.jcr.query.Row) InvalidQueryException(javax.jcr.query.InvalidQueryException)

Example 4 with StaticOperand

use of javax.jcr.query.qom.StaticOperand in project jackrabbit by apache.

the class LuceneQueryFactory method getFullTextSearchQuery.

protected Query getFullTextSearchQuery(FullTextSearch fts) throws RepositoryException {
    String field = FieldNames.FULLTEXT;
    String property = fts.getPropertyName();
    if (property != null) {
        Name name = session.getQName(property);
        field = nsMappings.getPrefix(name.getNamespaceURI()) + ":" + FieldNames.FULLTEXT_PREFIX + name.getLocalName();
    }
    StaticOperand expression = fts.getFullTextSearchExpression();
    String query = evaluator.getValue(expression).getString();
    try {
        QueryParser parser = new JackrabbitQueryParser(field, index.getTextAnalyzer(), index.getSynonymProvider(), cache);
        return parser.parse(query);
    } catch (ParseException e) {
        throw new RepositoryException("Invalid full text search expression: " + query, e);
    }
}
Also used : QueryParser(org.apache.lucene.queryParser.QueryParser) StaticOperand(javax.jcr.query.qom.StaticOperand) RepositoryException(javax.jcr.RepositoryException) ParseException(org.apache.lucene.queryParser.ParseException) NodeName(javax.jcr.query.qom.NodeName) NodeLocalName(javax.jcr.query.qom.NodeLocalName) Name(org.apache.jackrabbit.spi.Name)

Example 5 with StaticOperand

use of javax.jcr.query.qom.StaticOperand in project jackrabbit by apache.

the class LuceneQueryFactory method create.

/**
     * Creates a lucene query for the given QOM full text search.
     *
     * @param fts the full text search constraint.
     * @return the lucene query for the given constraint.
     * @throws RepositoryException if an error occurs while creating the query.
     */
public Query create(FullTextSearchImpl fts) throws RepositoryException {
    String fieldname;
    if (fts.getPropertyName() == null) {
        // fulltext on node
        fieldname = FieldNames.FULLTEXT;
    } else {
        // final path element is a property name
        Name propName = fts.getPropertyQName();
        StringBuffer tmp = new StringBuffer();
        tmp.append(nsMappings.getPrefix(propName.getNamespaceURI()));
        tmp.append(":").append(FieldNames.FULLTEXT_PREFIX);
        tmp.append(propName.getLocalName());
        fieldname = tmp.toString();
    }
    QueryParser parser = new JackrabbitQueryParser(fieldname, index.getTextAnalyzer(), index.getSynonymProvider(), cache);
    try {
        StaticOperand expr = fts.getFullTextSearchExpression();
        return parser.parse(evaluator.getValue(expr).getString());
    } catch (ParseException e) {
        throw new RepositoryException(e);
    }
}
Also used : QueryParser(org.apache.lucene.queryParser.QueryParser) StaticOperand(javax.jcr.query.qom.StaticOperand) RepositoryException(javax.jcr.RepositoryException) ParseException(org.apache.lucene.queryParser.ParseException) NodeName(javax.jcr.query.qom.NodeName) NodeLocalName(javax.jcr.query.qom.NodeLocalName) Name(org.apache.jackrabbit.spi.Name)

Aggregations

StaticOperand (javax.jcr.query.qom.StaticOperand)7 NodeLocalName (javax.jcr.query.qom.NodeLocalName)4 NodeName (javax.jcr.query.qom.NodeName)4 Value (javax.jcr.Value)3 BindVariableValue (javax.jcr.query.qom.BindVariableValue)3 PropertyValue (javax.jcr.query.qom.PropertyValue)3 RepositoryException (javax.jcr.RepositoryException)2 FullTextSearch (javax.jcr.query.qom.FullTextSearch)2 FullTextSearchScore (javax.jcr.query.qom.FullTextSearchScore)2 Length (javax.jcr.query.qom.Length)2 Literal (javax.jcr.query.qom.Literal)2 Name (org.apache.jackrabbit.spi.Name)2 ParseException (org.apache.lucene.queryParser.ParseException)2 QueryParser (org.apache.lucene.queryParser.QueryParser)2 UnsupportedRepositoryOperationException (javax.jcr.UnsupportedRepositoryOperationException)1 ValueFormatException (javax.jcr.ValueFormatException)1 InvalidQueryException (javax.jcr.query.InvalidQueryException)1 Row (javax.jcr.query.Row)1 And (javax.jcr.query.qom.And)1 Comparison (javax.jcr.query.qom.Comparison)1