Search in sources :

Example 6 with LuceneIndexWorker

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

the class Query method eval.

public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
    if (contextItem != null)
        contextSequence = contextItem.toSequence();
    if (contextSequence != null && !contextSequence.isPersistentSet())
        // in-memory docs won't have an index
        return Sequence.EMPTY_SEQUENCE;
    NodeSet result;
    if (preselectResult == null) {
        long start = System.currentTimeMillis();
        Sequence input = getArgument(0).eval(contextSequence);
        if (!(input instanceof VirtualNodeSet) && input.isEmpty())
            result = NodeSet.EMPTY_SET;
        else {
            NodeSet inNodes = input.toNodeSet();
            DocumentSet docs = inNodes.getDocumentSet();
            LuceneIndexWorker index = (LuceneIndexWorker) context.getBroker().getIndexController().getWorkerByIndexId(LuceneIndex.ID);
            Item key = getKey(contextSequence, contextItem);
            List<QName> qnames = null;
            if (contextQName != null) {
                qnames = new ArrayList<>(1);
                qnames.add(contextQName);
            }
            QueryOptions options = parseOptions(this, contextSequence, contextItem, 3);
            try {
                if (key != null && Type.subTypeOf(key.getType(), Type.ELEMENT)) {
                    final Element queryXML = (Element) ((NodeValue) key).getNode();
                    result = index.query(getExpressionId(), docs, inNodes, qnames, queryXML, NodeSet.ANCESTOR, options);
                } else {
                    final String query = key == null ? null : key.getStringValue();
                    result = index.query(getExpressionId(), docs, inNodes, qnames, query, NodeSet.ANCESTOR, options);
                }
            } catch (IOException | org.apache.lucene.queryparser.classic.ParseException e) {
                throw new XPathException(this, e.getMessage());
            }
        }
        if (context.getProfiler().traceFunctions()) {
            context.getProfiler().traceIndexUsage(context, "lucene", this, PerformanceStats.BASIC_INDEX, System.currentTimeMillis() - start);
        }
    } else {
        // DW: contextSequence can be null
        contextStep.setPreloadedData(contextSequence.getDocumentSet(), preselectResult);
        result = getArgument(0).eval(contextSequence).toNodeSet();
    }
    return result;
}
Also used : NodeSet(org.exist.dom.persistent.NodeSet) VirtualNodeSet(org.exist.dom.persistent.VirtualNodeSet) QName(org.exist.dom.QName) Element(org.w3c.dom.Element) IOException(java.io.IOException) LuceneIndexWorker(org.exist.indexing.lucene.LuceneIndexWorker) VirtualNodeSet(org.exist.dom.persistent.VirtualNodeSet) DocumentSet(org.exist.dom.persistent.DocumentSet)

Example 7 with LuceneIndexWorker

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

the class LuceneIndexKeys method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    final String fieldName = args[0].getStringValue();
    final DocumentSet docs = contextSequence == null ? context.getStaticallyKnownDocuments() : contextSequence.getDocumentSet();
    final Map<String, Object> hints = new HashMap<>();
    if (!args[1].isEmpty()) {
        hints.put(OrderedValuesIndex.START_VALUE, args[1].getStringValue());
    }
    IntegerValue max = null;
    if (args[3].hasOne()) {
        max = ((IntegerValue) args[3].itemAt(0));
    }
    if (max != null && max.getInt() > -1) {
        hints.put(IndexWorker.VALUE_COUNT, max);
    }
    final Sequence result = new ValueSequence();
    try (final FunctionReference ref = (FunctionReference) args[2].itemAt(0)) {
        final LuceneIndexWorker index = (LuceneIndexWorker) context.getBroker().getIndexController().getWorkerByIndexId(LuceneIndex.ID);
        final Occurrences[] occur = index.scanIndexByField(fieldName, docs, hints);
        final Sequence[] params = new Sequence[2];
        final ValueSequence data = new ValueSequence();
        for (int j = 0; j < occur.length; j++) {
            params[0] = new StringValue(occur[j].getTerm().toString());
            data.add(new IntegerValue(occur[j].getOccurrences(), Type.UNSIGNED_INT));
            data.add(new IntegerValue(occur[j].getDocuments(), Type.UNSIGNED_INT));
            data.add(new IntegerValue(j + 1, Type.UNSIGNED_INT));
            params[1] = data;
            result.addAll(ref.evalFunction(Sequence.EMPTY_SEQUENCE, null, params));
            data.clear();
        }
    }
    return result;
}
Also used : HashMap(java.util.HashMap) Occurrences(org.exist.util.Occurrences) LuceneIndexWorker(org.exist.indexing.lucene.LuceneIndexWorker) DocumentSet(org.exist.dom.persistent.DocumentSet)

Example 8 with LuceneIndexWorker

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

the class Optimize method eval.

public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    if (!context.getSubject().hasDbaRole())
        throw new XPathException(this, "user has to be a member of the dba group to call " + "the optimize function. Calling user was " + context.getSubject().getName());
    LuceneIndexWorker index = (LuceneIndexWorker) context.getBroker().getIndexController().getWorkerByIndexId(LuceneIndex.ID);
    index.optimize();
    return Sequence.EMPTY_SEQUENCE;
}
Also used : LuceneIndexWorker(org.exist.indexing.lucene.LuceneIndexWorker)

Example 9 with LuceneIndexWorker

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

the class QueryField method eval.

public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
    if (contextItem != null)
        contextSequence = contextItem.toSequence();
    NodeSet result;
    if (preselectResult == null) {
        long start = System.currentTimeMillis();
        String field = getArgument(0).eval(contextSequence).getStringValue();
        Item query = getKey(contextSequence, null);
        DocumentSet docs = null;
        if (contextSequence == null)
            docs = context.getStaticallyKnownDocuments();
        else
            docs = contextSequence.getDocumentSet();
        NodeSet contextSet = null;
        if (contextSequence != null)
            contextSet = contextSequence.toNodeSet();
        LuceneIndexWorker index = (LuceneIndexWorker) context.getBroker().getIndexController().getWorkerByIndexId(LuceneIndex.ID);
        QueryOptions options = parseOptions(this, contextSequence, contextItem, 3);
        try {
            if (Type.subTypeOf(query.getType(), Type.ELEMENT))
                result = index.queryField(getExpressionId(), docs, contextSet, field, (Element) ((NodeValue) query).getNode(), NodeSet.ANCESTOR, options);
            else
                result = index.queryField(context, getExpressionId(), docs, contextSet, field, query.getStringValue(), NodeSet.ANCESTOR, options);
        } catch (IOException e) {
            throw new XPathException(this, e.getMessage());
        }
        if (context.getProfiler().traceFunctions()) {
            context.getProfiler().traceIndexUsage(context, "lucene", this, PerformanceStats.BASIC_INDEX, System.currentTimeMillis() - start);
        }
    } else {
        result = preselectResult.selectAncestorDescendant(contextSequence.toNodeSet(), NodeSet.DESCENDANT, true, getContextId(), true);
    }
    return result;
}
Also used : NodeSet(org.exist.dom.persistent.NodeSet) DocumentSet(org.exist.dom.persistent.DocumentSet) IOException(java.io.IOException) LuceneIndexWorker(org.exist.indexing.lucene.LuceneIndexWorker)

Example 10 with LuceneIndexWorker

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

the class Query method preSelect.

public NodeSet preSelect(Sequence contextSequence, boolean useContext) throws XPathException {
    // guard against an empty contextSequence
    if (contextSequence == null || !contextSequence.isPersistentSet()) {
        // in-memory docs won't have an index
        return NodeSet.EMPTY_SET;
    }
    long start = System.currentTimeMillis();
    // the expression can be called multiple times, so we need to clear the previous preselectResult
    preselectResult = null;
    LuceneIndexWorker index = (LuceneIndexWorker) context.getBroker().getIndexController().getWorkerByIndexId(LuceneIndex.ID);
    DocumentSet docs = contextSequence.getDocumentSet();
    Item key = getKey(contextSequence, null);
    List<QName> qnames = new ArrayList<>(1);
    qnames.add(contextQName);
    QueryOptions options = parseOptions(this, contextSequence, null, 3);
    try {
        if (key != null && Type.subTypeOf(key.getType(), Type.ELEMENT)) {
            final Element queryXML = key == null ? null : (Element) ((NodeValue) key).getNode();
            preselectResult = index.query(getExpressionId(), docs, useContext ? contextSequence.toNodeSet() : null, qnames, queryXML, NodeSet.DESCENDANT, options);
        } else {
            final String query = key == null ? null : key.getStringValue();
            preselectResult = index.query(getExpressionId(), docs, useContext ? contextSequence.toNodeSet() : null, qnames, query, NodeSet.DESCENDANT, options);
        }
    } catch (IOException | org.apache.lucene.queryparser.classic.ParseException e) {
        throw new XPathException(this, "Error while querying full text index: " + e.getMessage(), e);
    }
    LOG.trace("Lucene query took {}", System.currentTimeMillis() - start);
    if (context.getProfiler().traceFunctions()) {
        context.getProfiler().traceIndexUsage(context, "lucene", this, PerformanceStats.OPTIMIZED_INDEX, System.currentTimeMillis() - start);
    }
    return preselectResult;
}
Also used : QName(org.exist.dom.QName) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) IOException(java.io.IOException) LuceneIndexWorker(org.exist.indexing.lucene.LuceneIndexWorker) DocumentSet(org.exist.dom.persistent.DocumentSet)

Aggregations

LuceneIndexWorker (org.exist.indexing.lucene.LuceneIndexWorker)10 IOException (java.io.IOException)6 DocumentSet (org.exist.dom.persistent.DocumentSet)5 LockedDocument (org.exist.dom.persistent.LockedDocument)3 XPathException (org.exist.xquery.XPathException)3 ArrayList (java.util.ArrayList)2 QName (org.exist.dom.QName)2 NodeSet (org.exist.dom.persistent.NodeSet)2 Element (org.w3c.dom.Element)2 HashMap (java.util.HashMap)1 NodeImpl (org.exist.dom.memtree.NodeImpl)1 DocumentImpl (org.exist.dom.persistent.DocumentImpl)1 VirtualNodeSet (org.exist.dom.persistent.VirtualNodeSet)1 PermissionDeniedException (org.exist.security.PermissionDeniedException)1 Occurrences (org.exist.util.Occurrences)1 XmldbURI (org.exist.xmldb.XmldbURI)1 NodeValue (org.exist.xquery.value.NodeValue)1