Search in sources :

Example 1 with EmptyNodeSet

use of org.exist.dom.persistent.EmptyNodeSet in project exist by eXist-db.

the class NGramSearch method fixedStringSearch.

public NodeSet fixedStringSearch(final NGramIndexWorker index, final DocumentSet docs, final List<QName> qnames, final String query, final NodeSet nodeSet, final int axis) throws XPathException {
    String[] ngrams = NGramSearch.getDistinctNGrams(query, index.getN());
    // Nothing to search for? The find nothing.
    if (ngrams.length == 0)
        return new EmptyNodeSet();
    String firstNgramm = ngrams[0];
    LOG.trace("First NGRAM: {}", firstNgramm);
    NodeSet result = index.search(getExpressionId(), docs, qnames, firstNgramm, firstNgramm, context, nodeSet, axis);
    for (int i = 1; i < ngrams.length; i++) {
        String ngram = ngrams[i];
        int len = ngram.codePointCount(0, ngram.length());
        int fillSize = index.getN() - len;
        String filledNgram = ngram;
        // ngrams lead to a considerable performance loss.
        if (fillSize > 0) {
            String filler = ngrams[i - 1];
            StringBuilder buf = new StringBuilder();
            int pos = filler.offsetByCodePoints(0, len);
            for (int j = 0; j < fillSize; j++) {
                int codepoint = filler.codePointAt(pos);
                pos += Character.charCount(codepoint);
                buf.appendCodePoint(codepoint);
            }
            buf.append(ngram);
            filledNgram = buf.toString();
            LOG.debug("Filled: {}", filledNgram);
        }
        NodeSet nodes = index.search(getExpressionId(), docs, qnames, filledNgram, ngram, context, nodeSet, axis);
        final NodeSet nodesContainingFirstINgrams = result;
        result = NodeSets.transformNodes(nodes, proxy -> Optional.ofNullable(nodesContainingFirstINgrams.get(proxy)).map(before -> getContinuousMatches(before, proxy)).orElse(null));
    }
    return result;
}
Also used : NodeSet(org.exist.dom.persistent.NodeSet) EmptyNodeSet(org.exist.dom.persistent.EmptyNodeSet) Match(org.exist.dom.persistent.Match) EvaluatableExpression(org.exist.xquery.modules.ngram.query.EvaluatableExpression) java.util(java.util) QName(org.exist.dom.QName) NodeProxy(org.exist.dom.persistent.NodeProxy) org.exist.xquery.value(org.exist.xquery.value) Wildcard(org.exist.xquery.modules.ngram.query.Wildcard) NodeSet(org.exist.dom.persistent.NodeSet) EmptyExpression(org.exist.xquery.modules.ngram.query.EmptyExpression) org.exist.xquery(org.exist.xquery) NodeProxies(org.exist.xquery.modules.ngram.utils.NodeProxies) NGramIndex(org.exist.indexing.ngram.NGramIndex) Matcher(java.util.regex.Matcher) NodeSets(org.exist.xquery.modules.ngram.utils.NodeSets) ElementValue(org.exist.storage.ElementValue) Error(org.exist.xquery.util.Error) DocumentSet(org.exist.dom.persistent.DocumentSet) AlternativeStrings(org.exist.xquery.modules.ngram.query.AlternativeStrings) StartAnchor(org.exist.xquery.modules.ngram.query.StartAnchor) EmptyNodeSet(org.exist.dom.persistent.EmptyNodeSet) NGramIndexWorker(org.exist.indexing.ngram.NGramIndexWorker) Logger(org.apache.logging.log4j.Logger) FixedString(org.exist.xquery.modules.ngram.query.FixedString) EndAnchor(org.exist.xquery.modules.ngram.query.EndAnchor) WildcardedExpressionSequence(org.exist.xquery.modules.ngram.query.WildcardedExpressionSequence) Pattern(java.util.regex.Pattern) WildcardedExpression(org.exist.xquery.modules.ngram.query.WildcardedExpression) LogManager(org.apache.logging.log4j.LogManager) EmptyNodeSet(org.exist.dom.persistent.EmptyNodeSet) FixedString(org.exist.xquery.modules.ngram.query.FixedString)

Example 2 with EmptyNodeSet

use of org.exist.dom.persistent.EmptyNodeSet in project exist by eXist-db.

the class WildcardedExpressionSequence method eval.

@Override
public NodeSet eval(final NGramIndexWorker index, final DocumentSet docs, final List<QName> qnames, final NodeSet nodeSet, final int axis, final int expressionId) throws XPathException {
    boolean startAnchorPresent = false;
    if (!expressions.isEmpty() && expressions.get(0) instanceof StartAnchor) {
        startAnchorPresent = true;
        expressions.remove(0);
    }
    Wildcard leadingWildcard = null;
    if (!expressions.isEmpty() && expressions.get(0) instanceof Wildcard)
        leadingWildcard = (Wildcard) expressions.remove(0);
    boolean endAnchorPresent = false;
    if (!expressions.isEmpty() && expressions.get(expressions.size() - 1) instanceof EndAnchor) {
        endAnchorPresent = true;
        expressions.remove(expressions.size() - 1);
    }
    Wildcard trailingWildcard = null;
    if (!expressions.isEmpty() && expressions.get(expressions.size() - 1) instanceof Wildcard)
        trailingWildcard = (Wildcard) expressions.remove(expressions.size() - 1);
    while (expressions.size() >= 3) {
        formEvaluatableTriples(expressionId);
    }
    if (expressions.isEmpty())
        return new EmptyNodeSet();
    if (expressions.size() != 1 || !(expressions.get(0) instanceof EvaluatableExpression)) {
        // Should not happen.
        LOG.error("Expression {} could not be evaluated", toString());
        throw new XPathException("Could not evaluate wildcarded query.");
    }
    LOG.trace("Evaluating expression {}", toString());
    NodeSet result = ((EvaluatableExpression) expressions.get(0)).eval(index, docs, qnames, nodeSet, axis, expressionId);
    if (leadingWildcard != null)
        result = expandMatchesBackward(leadingWildcard, result, expressionId);
    if (startAnchorPresent)
        result = NodeSets.getNodesMatchingAtStart(result, expressionId);
    if (trailingWildcard != null)
        result = expandMatchesForward(trailingWildcard, result, expressionId);
    if (endAnchorPresent)
        result = NodeSets.getNodesMatchingAtEnd(result, expressionId);
    return result;
}
Also used : EmptyNodeSet(org.exist.dom.persistent.EmptyNodeSet) NodeSet(org.exist.dom.persistent.NodeSet) XPathException(org.exist.xquery.XPathException) EmptyNodeSet(org.exist.dom.persistent.EmptyNodeSet)

Aggregations

EmptyNodeSet (org.exist.dom.persistent.EmptyNodeSet)2 NodeSet (org.exist.dom.persistent.NodeSet)2 java.util (java.util)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 LogManager (org.apache.logging.log4j.LogManager)1 Logger (org.apache.logging.log4j.Logger)1 QName (org.exist.dom.QName)1 DocumentSet (org.exist.dom.persistent.DocumentSet)1 Match (org.exist.dom.persistent.Match)1 NodeProxy (org.exist.dom.persistent.NodeProxy)1 NGramIndex (org.exist.indexing.ngram.NGramIndex)1 NGramIndexWorker (org.exist.indexing.ngram.NGramIndexWorker)1 ElementValue (org.exist.storage.ElementValue)1 org.exist.xquery (org.exist.xquery)1 XPathException (org.exist.xquery.XPathException)1 AlternativeStrings (org.exist.xquery.modules.ngram.query.AlternativeStrings)1 EmptyExpression (org.exist.xquery.modules.ngram.query.EmptyExpression)1 EndAnchor (org.exist.xquery.modules.ngram.query.EndAnchor)1 EvaluatableExpression (org.exist.xquery.modules.ngram.query.EvaluatableExpression)1