Search in sources :

Example 1 with LuceneMatch

use of org.exist.indexing.lucene.LuceneMatch in project exist by eXist-db.

the class Score method eval.

public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    NodeValue nodeValue = (NodeValue) args[0].itemAt(0);
    if (nodeValue.getImplementationType() != NodeValue.PERSISTENT_NODE) {
        return Sequence.EMPTY_SEQUENCE;
    }
    NodeProxy proxy = (NodeProxy) nodeValue;
    Match match = proxy.getMatches();
    float score = 0.0f;
    while (match != null) {
        if (match.getIndexId().equals(LuceneIndex.ID)) {
            float currentScore = ((LuceneMatch) match).getScore();
            score += currentScore;
        }
        match = match.getNextMatch();
    }
    return new FloatValue(score);
}
Also used : LuceneMatch(org.exist.indexing.lucene.LuceneMatch) NodeProxy(org.exist.dom.persistent.NodeProxy) Match(org.exist.dom.persistent.Match) LuceneMatch(org.exist.indexing.lucene.LuceneMatch)

Example 2 with LuceneMatch

use of org.exist.indexing.lucene.LuceneMatch 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)

Aggregations

Match (org.exist.dom.persistent.Match)2 NodeProxy (org.exist.dom.persistent.NodeProxy)2 LuceneMatch (org.exist.indexing.lucene.LuceneMatch)2 IOException (java.io.IOException)1 IdentityHashMap (java.util.IdentityHashMap)1 Query (org.apache.lucene.search.Query)1 MapType (org.exist.xquery.functions.map.MapType)1