Search in sources :

Example 1 with LuceneIndexWorker

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

the class RemoveIndex method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    // Get first parameter, this is the document
    final String path = args[0].itemAt(0).getStringValue();
    // Retrieve document from database
    try (final LockedDocument lockedDoc = context.getBroker().getXMLResource(XmldbURI.xmldbUriFor(path), LockMode.READ_LOCK)) {
        // Verify the document actually exists
        if (lockedDoc == null) {
            throw new XPathException("Document " + path + " does not exist.");
        }
        // Retrieve Lucene
        LuceneIndexWorker index = (LuceneIndexWorker) context.getBroker().getIndexController().getWorkerByIndexId(LuceneIndex.ID);
        // Note: code order is important here,
        index.setDocument(lockedDoc.getDocument(), ReindexMode.REMOVE_BINARY);
        index.flush();
    } catch (Exception ex) {
        // PermissionDeniedException
        throw new XPathException(ex);
    }
    // Return nothing [status would be nice]
    return Sequence.EMPTY_SEQUENCE;
}
Also used : XPathException(org.exist.xquery.XPathException) LockedDocument(org.exist.dom.persistent.LockedDocument) LuceneIndexWorker(org.exist.indexing.lucene.LuceneIndexWorker) XPathException(org.exist.xquery.XPathException)

Example 2 with LuceneIndexWorker

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

the class Search method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    NodeImpl report = null;
    try {
        // Only match documents that match these URLs
        List<String> toBeMatchedURIs = new ArrayList<>();
        Sequence pathSeq = getArgumentCount() > 1 ? args[0] : contextSequence;
        if (pathSeq == null)
            return Sequence.EMPTY_SEQUENCE;
        // Get first agument, these are the documents / collections to search in
        for (SequenceIterator i = pathSeq.iterate(); i.hasNext(); ) {
            String path;
            Item item = i.nextItem();
            if (Type.subTypeOf(item.getType(), Type.NODE)) {
                if (((NodeValue) item).isPersistentSet()) {
                    path = ((NodeProxy) item).getOwnerDocument().getURI().toString();
                } else {
                    path = item.getStringValue();
                }
            } else {
                path = item.getStringValue();
            }
            toBeMatchedURIs.add(path);
        }
        // Get second argument, this is the query
        String query;
        if (getArgumentCount() == 1)
            query = args[0].itemAt(0).getStringValue();
        else
            query = args[1].itemAt(0).getStringValue();
        String[] fields = null;
        if (getArgumentCount() == 3) {
            fields = new String[args[2].getItemCount()];
            int j = 0;
            for (SequenceIterator i = args[2].iterate(); i.hasNext(); ) {
                fields[j++] = i.nextItem().getStringValue();
            }
        }
        // Get the lucene worker
        LuceneIndexWorker index = (LuceneIndexWorker) context.getBroker().getIndexController().getWorkerByIndexId(LuceneIndex.ID);
        QueryOptions options = Query.parseOptions(this, contextSequence, null, 4);
        // Perform search
        report = index.search(context, toBeMatchedURIs, query, fields, options);
    } catch (IOException e) {
        throw new XPathException(this, e.getMessage(), e);
    } catch (XPathException ex) {
        // Log and rethrow
        logger.error(ex.getMessage(), ex);
        throw ex;
    }
    // Return list of matching files.
    return report;
}
Also used : NodeImpl(org.exist.dom.memtree.NodeImpl) ArrayList(java.util.ArrayList) IOException(java.io.IOException) LuceneIndexWorker(org.exist.indexing.lucene.LuceneIndexWorker)

Example 3 with LuceneIndexWorker

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

the class Index method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    try {
        // Retrieve Lucene
        LuceneIndexWorker index = (LuceneIndexWorker) context.getBroker().getIndexController().getWorkerByIndexId(LuceneIndex.ID);
        if (isCalledAs("index")) {
            // Get first parameter, this is the document
            String path = args[0].itemAt(0).getStringValue();
            // Retrieve document from database
            try (final LockedDocument lockedDoc = context.getBroker().getXMLResource(XmldbURI.xmldbUriFor(path), LockMode.READ_LOCK)) {
                // Verify the document actually exists
                final DocumentImpl doc = lockedDoc == null ? null : lockedDoc.getDocument();
                if (doc == null) {
                    throw new XPathException(this, "Document " + path + " does not exist.");
                }
                boolean flush = args.length == 2 || args[2].effectiveBooleanValue();
                // Note: code order is important here,
                index.setDocument(doc, ReindexMode.STORE);
                index.setMode(ReindexMode.STORE);
                // Get 'solr' node from second parameter
                NodeValue descriptor = (NodeValue) args[1].itemAt(0);
                // Pas document and index instructions to indexer
                index.indexNonXML(descriptor);
                if (flush) {
                    // Make sure things are written
                    index.writeNonXML();
                }
            }
        } else {
            // "close"
            index.writeNonXML();
        }
    } catch (Exception ex) {
        // PermissionDeniedException
        logger.error(ex.getMessage(), ex);
        throw new XPathException(this, ex);
    }
    // Return nothing [status would be nice]
    return Sequence.EMPTY_SEQUENCE;
}
Also used : NodeValue(org.exist.xquery.value.NodeValue) XPathException(org.exist.xquery.XPathException) LockedDocument(org.exist.dom.persistent.LockedDocument) DocumentImpl(org.exist.dom.persistent.DocumentImpl) LuceneIndexWorker(org.exist.indexing.lucene.LuceneIndexWorker) XPathException(org.exist.xquery.XPathException)

Example 4 with LuceneIndexWorker

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

the class QueryField method preSelect.

public NodeSet preSelect(Sequence contextSequence, boolean useContext) throws XPathException {
    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);
    String field = getArgument(0).eval(contextSequence).getStringValue();
    DocumentSet docs = contextSequence.getDocumentSet();
    Item query = getKey(contextSequence, null);
    QueryOptions options = parseOptions(this, contextSequence, null, 3);
    try {
        if (Type.subTypeOf(query.getType(), Type.ELEMENT))
            preselectResult = index.queryField(getExpressionId(), docs, useContext ? contextSequence.toNodeSet() : null, field, (Element) ((NodeValue) query).getNode(), NodeSet.DESCENDANT, options);
        else
            preselectResult = index.queryField(context, getExpressionId(), docs, useContext ? contextSequence.toNodeSet() : null, field, query.getStringValue(), NodeSet.DESCENDANT, options);
    } catch (IOException e) {
        throw new XPathException(this, "Error while querying full text index: " + e.getMessage(), e);
    }
    LOG.debug("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 : DocumentSet(org.exist.dom.persistent.DocumentSet) IOException(java.io.IOException) LuceneIndexWorker(org.exist.indexing.lucene.LuceneIndexWorker)

Example 5 with LuceneIndexWorker

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

the class GetField method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    XmldbURI uri = XmldbURI.createInternal(args[0].getStringValue());
    String field = args[1].getStringValue();
    try (final LockedDocument lockedDoc = context.getBroker().getXMLResource(uri, LockMode.READ_LOCK)) {
        if (lockedDoc == null) {
            return Sequence.EMPTY_SEQUENCE;
        }
        // Get the lucene worker
        final LuceneIndexWorker index = (LuceneIndexWorker) context.getBroker().getIndexController().getWorkerByIndexId(LuceneIndex.ID);
        final String content = index.getFieldContent(lockedDoc.getDocument().getDocId(), field);
        return content == null ? Sequence.EMPTY_SEQUENCE : new org.exist.xquery.value.StringValue(content);
    } catch (PermissionDeniedException e) {
        throw new XPathException(this, LuceneModule.EXXQDYFT0001, "Permission denied to read document " + args[0].getStringValue());
    } catch (IOException e) {
        throw new XPathException(this, LuceneModule.EXXQDYFT0002, "IO error while reading document " + args[0].getStringValue());
    }
}
Also used : XPathException(org.exist.xquery.XPathException) LockedDocument(org.exist.dom.persistent.LockedDocument) PermissionDeniedException(org.exist.security.PermissionDeniedException) IOException(java.io.IOException) XmldbURI(org.exist.xmldb.XmldbURI) LuceneIndexWorker(org.exist.indexing.lucene.LuceneIndexWorker)

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