Search in sources :

Example 6 with Match

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

the class Facets method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    final String dimension = args[1].getStringValue();
    int count = Integer.MAX_VALUE;
    if (getArgumentCount() == 3 && args[2].hasOne()) {
        count = ((IntegerValue) args[2].itemAt(0)).getInt();
    }
    String[] paths = null;
    if (getArgumentCount() == 4 && !args[3].isEmpty()) {
        paths = new String[args[3].getItemCount()];
        int j = 0;
        for (SequenceIterator i = args[3].unorderedIterator(); i.hasNext(); j++) {
            paths[j] = i.nextItem().getStringValue();
        }
    }
    // Find all lucene queries referenced from the input sequence and remember
    // the first match for each. Every query will have its own facets attached,
    // so we have to merge them below.
    final Map<Query, LuceneMatch> luceneQueries = new IdentityHashMap<>();
    for (final SequenceIterator i = args[0].unorderedIterator(); i.hasNext(); ) {
        final NodeValue nv = (NodeValue) i.nextItem();
        if (nv.getImplementationType() == NodeValue.PERSISTENT_NODE) {
            final NodeProxy proxy = (NodeProxy) nv;
            Match match = proxy.getMatches();
            while (match != null) {
                if (match.getIndexId().equals(LuceneIndex.ID)) {
                    final LuceneMatch luceneMatch = (LuceneMatch) match;
                    luceneQueries.putIfAbsent(luceneMatch.getQuery(), luceneMatch);
                }
                match = match.getNextMatch();
            }
        }
    }
    // Iterate the found queries/matches and collect facets for each
    final IMap<AtomicValue, Sequence> map = newLinearMap(null);
    for (LuceneMatch match : luceneQueries.values()) {
        try {
            addFacetsToMap(map, dimension, count, paths, match);
        } catch (IOException e) {
            throw new XPathException(this, LuceneModule.EXXQDYFT0002, e.getMessage());
        }
    }
    return new MapType(context, map.forked(), Type.STRING);
}
Also used : Query(org.apache.lucene.search.Query) IdentityHashMap(java.util.IdentityHashMap) IOException(java.io.IOException) NodeProxy(org.exist.dom.persistent.NodeProxy) MapType(org.exist.xquery.functions.map.MapType) Match(org.exist.dom.persistent.Match) LuceneMatch(org.exist.indexing.lucene.LuceneMatch) LuceneMatch(org.exist.indexing.lucene.LuceneMatch)

Example 7 with Match

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

the class AddMatch method eval.

@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
    if (args[0].isEmpty()) {
        return args[0];
    }
    final NodeValue nv = (NodeValue) args[0].itemAt(0);
    if (!nv.isPersistentSet()) {
        return nv;
    }
    final NodeProxy node = (NodeProxy) nv;
    final int thisLevel = node.getNodeId().getTreeLevel();
    String matchStr = null;
    NodeId nodeId = null;
    try {
        for (final XMLStreamReader reader = context.getBroker().getXMLStreamReader(node, true); reader.hasNext(); ) {
            final int status = reader.next();
            if (status == XMLStreamConstants.CHARACTERS) {
                matchStr = reader.getText();
                nodeId = (NodeId) reader.getProperty(ExtendedXMLStreamReader.PROPERTY_NODE_ID);
                break;
            }
            final NodeId otherId = (NodeId) reader.getProperty(ExtendedXMLStreamReader.PROPERTY_NODE_ID);
            final int otherLevel = otherId.getTreeLevel();
            if (status == XMLStreamConstants.END_ELEMENT && otherLevel == thisLevel) {
                // exit-for
                break;
            }
        }
    } catch (IOException | XMLStreamException e) {
        throw new XPathException(this, ErrorCodes.FOER0000, "Exception caught while reading document");
    }
    if (nodeId != null) {
        Match match = new NGramMatch(getContextId(), node.getNodeId(), matchStr);
        match.addOffset(0, matchStr.length());
        node.addMatch(match);
    }
    return node;
}
Also used : NodeValue(org.exist.xquery.value.NodeValue) XMLStreamReader(javax.xml.stream.XMLStreamReader) ExtendedXMLStreamReader(org.exist.stax.ExtendedXMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) XPathException(org.exist.xquery.XPathException) NodeId(org.exist.numbering.NodeId) NGramMatch(org.exist.indexing.ngram.NGramMatch) IOException(java.io.IOException) NodeProxy(org.exist.dom.persistent.NodeProxy) Match(org.exist.dom.persistent.Match) NGramMatch(org.exist.indexing.ngram.NGramMatch)

Example 8 with Match

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

the class NodeProxies method transformOwnMatches.

/**
 * Applies a supplied function to all matches with the supplied expression id on the supplied NodeProxy and returns
 * the NodeProxy with the modified matches if at least one match with the supplied expression id was not transformed
 * to null or null otherwise.
 *
 * @param node
 *            the NodeProxy to modify
 * @param transform
 *            the function to apply to all matches with the supplied expression id
 * @param ownExpressionId
 *            the expression id of the matches to be transformed
 * @return the modified node if at least one match with the supplied expression id was not transformed to null or
 *         null otherwise
 */
public static NodeProxy transformOwnMatches(final NodeProxy node, final Function<Match, Match> transform, int ownExpressionId) {
    Match m = node.getMatches();
    node.setMatches(null);
    boolean ownMatch = false;
    while (m != null) {
        if (m.getContextId() != ownExpressionId) {
            node.addMatch(m);
        } else {
            final Match nm = transform.apply(m);
            if (nm != null) {
                node.addMatch(nm);
                ownMatch = true;
            }
        }
        m = m.getNextMatch();
    }
    return ownMatch ? node : null;
}
Also used : Match(org.exist.dom.persistent.Match)

Example 9 with Match

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

the class NGramIndexWorker method getMatchListener.

public MatchListener getMatchListener(final DBBroker broker, final NodeProxy proxy, final NGramMatchCallback callback) {
    boolean needToFilter = false;
    Match nextMatch = proxy.getMatches();
    while (nextMatch != null) {
        if (nextMatch.getIndexId().equals(org.exist.indexing.ngram.NGramIndex.ID)) {
            needToFilter = true;
            break;
        }
        nextMatch = nextMatch.getNextMatch();
    }
    if (!needToFilter) {
        return null;
    }
    if (matchListener == null) {
        matchListener = new NGramMatchListener(broker, proxy);
    } else {
        matchListener.reset(broker, proxy);
    }
    matchListener.setMatchCallback(callback);
    return matchListener;
}
Also used : Match(org.exist.dom.persistent.Match)

Example 10 with Match

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

the class WildcardedExpressionTriple method getMatchingNode.

private NodeProxy getMatchingNode(NodeProxy headNode, NodeProxy tailNode, final int expressionId) {
    NodeProxy result = null;
    Match match = null;
    boolean found = false;
    for (Match headMatch = headNode.getMatches(); headMatch != null && !found; headMatch = headMatch.getNextMatch()) {
        for (Match tailMatch = tailNode.getMatches(); tailMatch != null && !found; tailMatch = tailMatch.getNextMatch()) {
            match = headMatch.followedBy(tailMatch, wildcard.getMinimumLength(), wildcard.getMaximumLength());
            found = (match != null);
        }
    }
    if (found) {
        // Remove own (partial) matches and add new complete match
        NodeProxies.filterMatches(tailNode, a -> a.getContextId() != expressionId);
        tailNode.addMatch(match);
        result = tailNode;
    }
    return result;
}
Also used : NodeProxy(org.exist.dom.persistent.NodeProxy) Match(org.exist.dom.persistent.Match)

Aggregations

Match (org.exist.dom.persistent.Match)12 NodeProxy (org.exist.dom.persistent.NodeProxy)7 IOException (java.io.IOException)3 Query (org.apache.lucene.search.Query)2 NodeSet (org.exist.dom.persistent.NodeSet)2 LuceneMatch (org.exist.indexing.lucene.LuceneMatch)2 java.util (java.util)1 IdentityHashMap (java.util.IdentityHashMap)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 LogManager (org.apache.logging.log4j.LogManager)1 Logger (org.apache.logging.log4j.Logger)1 PhraseQuery (org.apache.lucene.search.PhraseQuery)1 QName (org.exist.dom.QName)1 DocumentSet (org.exist.dom.persistent.DocumentSet)1 EmptyNodeSet (org.exist.dom.persistent.EmptyNodeSet)1 NewArrayNodeSet (org.exist.dom.persistent.NewArrayNodeSet)1 NGramIndex (org.exist.indexing.ngram.NGramIndex)1