Search in sources :

Example 11 with NamespaceException

use of javax.jcr.NamespaceException in project jackrabbit by apache.

the class LuceneQueryBuilder method visit.

public Object visit(TextsearchQueryNode node, Object data) {
    try {
        Path relPath = node.getRelativePath();
        String fieldname;
        if (relPath == null || !node.getReferencesProperty()) {
            // fulltext on node
            fieldname = FieldNames.FULLTEXT;
        } else {
            // final path element is a property name
            Name propName = relPath.getName();
            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, analyzer, synonymProvider, cache);
        Query context = parser.parse(node.getQuery());
        if (relPath != null && (!node.getReferencesProperty() || relPath.getLength() > 1)) {
            // text search on some child axis
            Path.Element[] elements = relPath.getElements();
            for (int i = elements.length - 1; i >= 0; i--) {
                Name name = null;
                if (!elements[i].getName().equals(RelationQueryNode.STAR_NAME_TEST)) {
                    name = elements[i].getName();
                }
                // if path references node that's elements.length - 1
                if (name != null && ((node.getReferencesProperty() && i == elements.length - 2) || (!node.getReferencesProperty() && i == elements.length - 1))) {
                    Query q = new NameQuery(name, indexFormatVersion, nsMappings);
                    BooleanQuery and = new BooleanQuery();
                    and.add(q, Occur.MUST);
                    and.add(context, Occur.MUST);
                    context = and;
                } else if ((node.getReferencesProperty() && i < elements.length - 2) || (!node.getReferencesProperty() && i < elements.length - 1)) {
                    // otherwise do a parent axis step
                    context = new ParentAxisQuery(context, name, indexFormatVersion, nsMappings);
                }
            }
            // finally select parent
            context = new ParentAxisQuery(context, null, indexFormatVersion, nsMappings);
        }
        return context;
    } catch (NamespaceException e) {
        exceptions.add(e);
    } catch (ParseException e) {
        exceptions.add(e);
    }
    return null;
}
Also used : Path(org.apache.jackrabbit.spi.Path) Name(org.apache.jackrabbit.spi.Name) QueryParser(org.apache.lucene.queryParser.QueryParser) NamespaceException(javax.jcr.NamespaceException) ParseException(org.apache.lucene.queryParser.ParseException)

Example 12 with NamespaceException

use of javax.jcr.NamespaceException in project jackrabbit by apache.

the class LuceneQueryBuilder method getStringValues.

/**
     * Returns an array of String values to be used as a term to lookup the search index
     * for a String <code>literal</code> of a certain property name. This method
     * will lookup the <code>propertyName</code> in the node type registry
     * trying to find out the {@link javax.jcr.PropertyType}s.
     * If no property type is found looking up node type information, this
     * method will guess the property type.
     *
     * @param propertyName the name of the property in the relation.
     * @param literal      the String literal in the relation.
     * @return the String values to use as term for the query.
     */
private String[] getStringValues(Name propertyName, String literal) {
    PropertyTypeRegistry.TypeMapping[] types = propRegistry.getPropertyTypes(propertyName);
    List<String> values = new ArrayList<String>();
    for (PropertyTypeRegistry.TypeMapping type : types) {
        switch(type.type) {
            case PropertyType.NAME:
                // try to translate name
                try {
                    Name n = session.getQName(literal);
                    values.add(nsMappings.translateName(n));
                    log.debug("Coerced " + literal + " into NAME.");
                } catch (NameException e) {
                    log.debug("Unable to coerce '" + literal + "' into a NAME: " + e.toString());
                } catch (NamespaceException e) {
                    log.debug("Unable to coerce '" + literal + "' into a NAME: " + e.toString());
                }
                break;
            case PropertyType.PATH:
                // try to translate path
                try {
                    Path p = session.getQPath(literal);
                    values.add(resolver.getJCRPath(p));
                    log.debug("Coerced " + literal + " into PATH.");
                } catch (NameException e) {
                    log.debug("Unable to coerce '" + literal + "' into a PATH: " + e.toString());
                } catch (NamespaceException e) {
                    log.debug("Unable to coerce '" + literal + "' into a PATH: " + e.toString());
                }
                break;
            case PropertyType.DATE:
                // try to parse date
                Calendar c = ISO8601.parse(literal);
                if (c != null) {
                    values.add(DateField.timeToString(c.getTimeInMillis()));
                    log.debug("Coerced " + literal + " into DATE.");
                } else {
                    log.debug("Unable to coerce '" + literal + "' into a DATE.");
                }
                break;
            case PropertyType.DOUBLE:
                // try to parse double
                try {
                    double d = Double.parseDouble(literal);
                    values.add(DoubleField.doubleToString(d));
                    log.debug("Coerced " + literal + " into DOUBLE.");
                } catch (NumberFormatException e) {
                    log.debug("Unable to coerce '" + literal + "' into a DOUBLE: " + e.toString());
                }
                break;
            case PropertyType.LONG:
                // try to parse long
                try {
                    long l = Long.parseLong(literal);
                    values.add(LongField.longToString(l));
                    log.debug("Coerced " + literal + " into LONG.");
                } catch (NumberFormatException e) {
                    log.debug("Unable to coerce '" + literal + "' into a LONG: " + e.toString());
                }
                break;
            case PropertyType.DECIMAL:
                // try to parse decimal
                try {
                    BigDecimal d = new BigDecimal(literal);
                    values.add(DecimalField.decimalToString(d));
                    log.debug("Coerced " + literal + " into DECIMAL.");
                } catch (NumberFormatException e) {
                    log.debug("Unable to coerce '" + literal + "' into a DECIMAL: " + e.toString());
                }
                break;
            case PropertyType.URI:
            // fall through... TODO: correct?
            case PropertyType.STRING:
                values.add(literal);
                log.debug("Using literal " + literal + " as is.");
                break;
        }
    }
    if (values.size() == 0) {
        // use literal as is then try to guess other types
        values.add(literal);
        // try to guess property type
        if (literal.indexOf('/') > -1) {
            // might be a path
            try {
                values.add(resolver.getJCRPath(session.getQPath(literal)));
                log.debug("Coerced " + literal + " into PATH.");
            } catch (Exception e) {
            // not a path
            }
        }
        if (XMLChar.isValidName(literal)) {
            // might be a name
            try {
                Name n = session.getQName(literal);
                values.add(nsMappings.translateName(n));
                log.debug("Coerced " + literal + " into NAME.");
            } catch (Exception e) {
            // not a name
            }
        }
        if (literal.indexOf(':') > -1) {
            // is it a date?
            Calendar c = ISO8601.parse(literal);
            if (c != null) {
                values.add(DateField.timeToString(c.getTimeInMillis()));
                log.debug("Coerced " + literal + " into DATE.");
            }
        } else {
            // long or double are possible at this point
            try {
                values.add(LongField.longToString(Long.parseLong(literal)));
                log.debug("Coerced " + literal + " into LONG.");
            } catch (NumberFormatException e) {
                // try double
                try {
                    values.add(DoubleField.doubleToString(Double.parseDouble(literal)));
                    log.debug("Coerced " + literal + " into DOUBLE.");
                } catch (NumberFormatException e1) {
                // not a double
                }
            }
        }
    }
    // if still no values use literal as is
    if (values.size() == 0) {
        values.add(literal);
        log.debug("Using literal " + literal + " as is.");
    }
    return values.toArray(new String[values.size()]);
}
Also used : Path(org.apache.jackrabbit.spi.Path) PropertyTypeRegistry(org.apache.jackrabbit.core.query.PropertyTypeRegistry) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal) NameException(org.apache.jackrabbit.spi.commons.conversion.NameException) ParseException(org.apache.lucene.queryParser.ParseException) RepositoryException(javax.jcr.RepositoryException) NamespaceException(javax.jcr.NamespaceException) InvalidQueryException(javax.jcr.query.InvalidQueryException) Name(org.apache.jackrabbit.spi.Name) NameException(org.apache.jackrabbit.spi.commons.conversion.NameException) NamespaceException(javax.jcr.NamespaceException)

Example 13 with NamespaceException

use of javax.jcr.NamespaceException in project jackrabbit by apache.

the class LuceneQueryBuilder method visit.

public Object visit(ExactQueryNode node, Object data) {
    String field = "";
    String value = "";
    try {
        field = resolver.getJCRName(node.getPropertyName());
        value = resolver.getJCRName(node.getValue());
    } catch (NamespaceException e) {
    // will never happen, prefixes are created when unknown
    }
    return new JackrabbitTermQuery(new Term(FieldNames.PROPERTIES, FieldNames.createNamedValue(field, value)));
}
Also used : NamespaceException(javax.jcr.NamespaceException) Term(org.apache.lucene.index.Term)

Example 14 with NamespaceException

use of javax.jcr.NamespaceException in project jackrabbit by apache.

the class NodeIndexer method addMVPName.

/**
     * Adds a {@link FieldNames#MVP} field to <code>doc</code> with the resolved
     * <code>name</code> using the internal search index namespace mapping.
     *
     * @param doc  the lucene document.
     * @param name the name of the multi-value property.
     */
protected void addMVPName(Document doc, Name name) {
    try {
        String propName = resolver.getJCRName(name);
        doc.add(new Field(FieldNames.MVP, false, propName, Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS, Field.TermVector.NO));
    } catch (NamespaceException e) {
    // will never happen, prefixes are created dynamically
    }
}
Also used : Field(org.apache.lucene.document.Field) NamespaceException(javax.jcr.NamespaceException)

Example 15 with NamespaceException

use of javax.jcr.NamespaceException in project jackrabbit by apache.

the class NodeIndexer method createDoc.

/**
     * Creates a lucene Document.
     *
     * @return the lucene Document with the index layout.
     * @throws RepositoryException if an error occurs while reading property
     *                             values from the <code>ItemStateProvider</code>.
     */
public Document createDoc() throws RepositoryException {
    doNotUseInExcerpt.clear();
    Document doc = new Document();
    doc.setBoost(getNodeBoost());
    // special fields
    // UUID
    doc.add(new IDField(node.getNodeId()));
    try {
        // parent UUID
        if (node.getParentId() == null) {
            // root node
            Field parent = new Field(FieldNames.PARENT, false, "", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS, Field.TermVector.NO);
            parent.setIndexOptions(FieldInfo.IndexOptions.DOCS_ONLY);
            doc.add(parent);
            addNodeName(doc, "", "");
        } else if (node.getSharedSet().isEmpty()) {
            addParentChildRelation(doc, node.getParentId());
        } else {
            // shareable node
            for (NodeId id : node.getSharedSet()) {
                addParentChildRelation(doc, id);
            }
            // mark shareable nodes
            doc.add(new Field(FieldNames.SHAREABLE_NODE, false, "", Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS, Field.TermVector.NO));
        }
    } catch (NoSuchItemStateException e) {
        throwRepositoryException(e);
    } catch (ItemStateException e) {
        throwRepositoryException(e);
    } catch (NamespaceException e) {
    // will never happen, because this.mappings will dynamically add
    // unknown uri<->prefix mappings
    }
    Set<Name> props = node.getPropertyNames();
    for (Name propName : props) {
        if (isIndexed(propName)) {
            PropertyId id = new PropertyId(node.getNodeId(), propName);
            try {
                PropertyState propState = (PropertyState) stateProvider.getItemState(id);
                // beginning with V2
                if (indexFormatVersion.getVersion() >= IndexFormatVersion.V2.getVersion()) {
                    addPropertyName(doc, propState.getName());
                }
                InternalValue[] values = propState.getValues();
                for (InternalValue value : values) {
                    addValue(doc, value, propState.getName());
                }
                if (values.length > 1) {
                    // real multi-valued
                    addMVPName(doc, propState.getName());
                }
            } catch (NoSuchItemStateException e) {
                throwRepositoryException(e);
            } catch (ItemStateException e) {
                throwRepositoryException(e);
            }
        }
    }
    // now add fields that are not used in excerpt (must go at the end)
    for (Fieldable field : doNotUseInExcerpt) {
        doc.add(field);
    }
    return doc;
}
Also used : Document(org.apache.lucene.document.Document) InternalValue(org.apache.jackrabbit.core.value.InternalValue) NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException) Name(org.apache.jackrabbit.spi.Name) PropertyId(org.apache.jackrabbit.core.id.PropertyId) PropertyState(org.apache.jackrabbit.core.state.PropertyState) Field(org.apache.lucene.document.Field) NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) Fieldable(org.apache.lucene.document.Fieldable) NodeId(org.apache.jackrabbit.core.id.NodeId) NamespaceException(javax.jcr.NamespaceException)

Aggregations

NamespaceException (javax.jcr.NamespaceException)56 Name (org.apache.jackrabbit.spi.Name)26 NameException (org.apache.jackrabbit.spi.commons.conversion.NameException)17 Path (org.apache.jackrabbit.spi.Path)13 InvalidQueryException (javax.jcr.query.InvalidQueryException)10 RepositoryException (javax.jcr.RepositoryException)7 PathQueryNode (org.apache.jackrabbit.spi.commons.query.PathQueryNode)7 NamespaceRegistry (javax.jcr.NamespaceRegistry)6 IllegalNameException (org.apache.jackrabbit.spi.commons.conversion.IllegalNameException)6 LocationStepQueryNode (org.apache.jackrabbit.spi.commons.query.LocationStepQueryNode)6 IOException (java.io.IOException)5 Session (javax.jcr.Session)5 DerefQueryNode (org.apache.jackrabbit.spi.commons.query.DerefQueryNode)5 PropertyFunctionQueryNode (org.apache.jackrabbit.spi.commons.query.PropertyFunctionQueryNode)5 SAXException (org.xml.sax.SAXException)5 InvalidSerializedDataException (javax.jcr.InvalidSerializedDataException)4 NodeTypeQueryNode (org.apache.jackrabbit.spi.commons.query.NodeTypeQueryNode)4 NotQueryNode (org.apache.jackrabbit.spi.commons.query.NotQueryNode)4 OrderQueryNode (org.apache.jackrabbit.spi.commons.query.OrderQueryNode)4 QueryNode (org.apache.jackrabbit.spi.commons.query.QueryNode)4