Search in sources :

Example 81 with AddUpdateCommand

use of org.apache.solr.update.AddUpdateCommand in project lucene-solr by apache.

the class CloneFieldUpdateProcessorFactory method getInstance.

@Override
public final UpdateRequestProcessor getInstance(SolrQueryRequest req, SolrQueryResponse rsp, UpdateRequestProcessor next) {
    final FieldNameSelector srcSelector = getSourceSelector();
    return new UpdateRequestProcessor(next) {

        @Override
        public void processAdd(AddUpdateCommand cmd) throws IOException {
            final SolrInputDocument doc = cmd.getSolrInputDocument();
            // destination may be regex replace string, which can cause multiple output fields.
            Map<String, SolrInputField> destMap = new HashMap<>();
            // preserve initial values and boost (if any)
            for (final String fname : doc.getFieldNames()) {
                if (!srcSelector.shouldMutate(fname))
                    continue;
                Collection<Object> srcFieldValues = doc.getFieldValues(fname);
                if (srcFieldValues == null || srcFieldValues.isEmpty())
                    continue;
                String resolvedDest = dest;
                if (pattern != null) {
                    Matcher matcher = pattern.matcher(fname);
                    if (matcher.find()) {
                        resolvedDest = matcher.replaceAll(dest);
                    } else {
                        log.debug("CloneFieldUpdateProcessor.srcSelector.shouldMutate(\"{}\") returned true, " + "but replacement pattern did not match, field skipped.", fname);
                        continue;
                    }
                }
                SolrInputField destField;
                if (doc.containsKey(resolvedDest)) {
                    destField = doc.getField(resolvedDest);
                } else {
                    SolrInputField targetField = destMap.get(resolvedDest);
                    if (targetField == null) {
                        destField = new SolrInputField(resolvedDest);
                    } else {
                        destField = targetField;
                    }
                }
                for (Object val : srcFieldValues) {
                    destField.addValue(val);
                }
                // put it in map to avoid concurrent modification...
                destMap.put(resolvedDest, destField);
            }
            for (String dest : destMap.keySet()) {
                doc.put(dest, destMap.get(dest));
            }
            super.processAdd(cmd);
        }
    };
}
Also used : FieldNameSelector(org.apache.solr.update.processor.FieldMutatingUpdateProcessor.FieldNameSelector) SolrInputDocument(org.apache.solr.common.SolrInputDocument) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) SolrInputField(org.apache.solr.common.SolrInputField) AddUpdateCommand(org.apache.solr.update.AddUpdateCommand)

Example 82 with AddUpdateCommand

use of org.apache.solr.update.AddUpdateCommand in project SearchServices by Alfresco.

the class AlfrescoSolrUtils method addStoreRoot.

/**
 * Add a store to root.
 * @param core
 * @param dataModel
 * @param rootNodeRef
 * @param txid
 * @param dbid
 * @param acltxid
 * @param aclid
 * @throws IOException
 */
public static void addStoreRoot(SolrCore core, AlfrescoSolrDataModel dataModel, NodeRef rootNodeRef, int txid, int dbid, int acltxid, int aclid) throws IOException {
    SolrServletRequest solrQueryRequest = null;
    try {
        solrQueryRequest = new SolrServletRequest(core, null);
        AddUpdateCommand addDocCmd = new AddUpdateCommand(solrQueryRequest);
        addDocCmd.overwrite = true;
        addDocCmd.solrDoc = createDocument(dataModel, new Long(txid), new Long(dbid), rootNodeRef, ContentModel.TYPE_STOREROOT, new QName[] { ContentModel.ASPECT_ROOT }, null, null, new Long(aclid), new String[] { "/" }, "system", null, null);
        core.getUpdateHandler().addDoc(addDocCmd);
        addAcl(solrQueryRequest, core, dataModel, acltxid, aclid, 0, 0);
        AddUpdateCommand txCmd = new AddUpdateCommand(solrQueryRequest);
        txCmd.overwrite = true;
        SolrInputDocument input = new SolrInputDocument();
        String id = AlfrescoSolrDataModel.getTransactionDocumentId(new Long(txid));
        input.addField(FIELD_SOLR4_ID, id);
        input.addField(FIELD_VERSION, "0");
        input.addField(FIELD_TXID, txid);
        input.addField(FIELD_INTXID, txid);
        input.addField(FIELD_TXCOMMITTIME, (new Date()).getTime());
        input.addField(FIELD_DOC_TYPE, SolrInformationServer.DOC_TYPE_TX);
        txCmd.solrDoc = input;
        core.getUpdateHandler().addDoc(txCmd);
        core.getUpdateHandler().commit(new CommitUpdateCommand(solrQueryRequest, false));
    } finally {
        solrQueryRequest.close();
    }
}
Also used : SolrServletRequest(org.alfresco.solr.AbstractAlfrescoSolrTests.SolrServletRequest) SolrInputDocument(org.apache.solr.common.SolrInputDocument) QName(org.alfresco.service.namespace.QName) AtomicLong(java.util.concurrent.atomic.AtomicLong) CommitUpdateCommand(org.apache.solr.update.CommitUpdateCommand) AddUpdateCommand(org.apache.solr.update.AddUpdateCommand) Date(java.util.Date)

Example 83 with AddUpdateCommand

use of org.apache.solr.update.AddUpdateCommand in project SearchServices by Alfresco.

the class SolrInformationServer method indexNode.

@Override
public void indexNode(Node node, boolean overwrite) throws IOException, AuthenticationException, JSONException {
    SolrQueryRequest request = null;
    UpdateRequestProcessor processor = null;
    try {
        request = getLocalSolrQueryRequest();
        processor = this.core.getUpdateProcessingChain(null).createProcessor(request, new SolrQueryResponse());
        long start = System.nanoTime();
        if ((node.getStatus() == SolrApiNodeStatus.DELETED) || (node.getStatus() == SolrApiNodeStatus.NON_SHARD_DELETED) || (node.getStatus() == SolrApiNodeStatus.NON_SHARD_UPDATED) || (node.getStatus() == SolrApiNodeStatus.UNKNOWN)) {
            // fix up any secondary paths
            NodeMetaDataParameters nmdp = new NodeMetaDataParameters();
            nmdp.setFromNodeId(node.getId());
            nmdp.setToNodeId(node.getId());
            List<NodeMetaData> nodeMetaDatas;
            if ((node.getStatus() == SolrApiNodeStatus.DELETED) || (node.getStatus() == SolrApiNodeStatus.NON_SHARD_DELETED) || (node.getStatus() == SolrApiNodeStatus.NON_SHARD_UPDATED)) {
                // Fake the empty node metadata for this parent deleted node
                NodeMetaData nodeMetaData = createDeletedNodeMetaData(node);
                nodeMetaDatas = Collections.singletonList(nodeMetaData);
            } else {
                nodeMetaDatas = repositoryClient.getNodesMetaData(nmdp, Integer.MAX_VALUE);
            }
            NodeMetaData nodeMetaData = null;
            if (!nodeMetaDatas.isEmpty()) {
                nodeMetaData = nodeMetaDatas.get(0);
                if (!(nodeMetaData.getTxnId() > node.getTxnId())) {
                    if (node.getStatus() == SolrApiNodeStatus.DELETED) {
                        try {
                            // Lock the node to ensure that no other trackers work with this node until this code completes.
                            if (!spinLock(nodeMetaData.getId(), 120000)) {
                                // We haven't acquired the lock in over 2 minutes. This really shouldn't happen unless something has gone wrong.
                                throw new Exception("Unable to acquire lock on nodeId:" + nodeMetaData.getId());
                            }
                            solrContentStore.removeDocFromContentStore(nodeMetaData);
                        } finally {
                            unlock(nodeMetaData.getId());
                        }
                    }
                }
            // else, the node has moved on to a later transaction, and it will be indexed later
            }
            if (log.isDebugEnabled()) {
                log.debug(".. deleting");
            }
            deleteNode(processor, request, node);
        }
        if ((node.getStatus() == SolrApiNodeStatus.UPDATED) || (node.getStatus() == SolrApiNodeStatus.UNKNOWN) || (node.getStatus() == SolrApiNodeStatus.NON_SHARD_UPDATED)) {
            log.info(".. updating");
            long nodeId = node.getId();
            try {
                if (!spinLock(nodeId, 120000)) {
                    // We haven't acquired the lock in over 2 minutes. This really shouldn't happen unless something has gone wrong.
                    throw new Exception("Unable to acquire lock on nodeId:" + nodeId);
                }
                NodeMetaDataParameters nmdp = new NodeMetaDataParameters();
                nmdp.setFromNodeId(node.getId());
                nmdp.setToNodeId(node.getId());
                List<NodeMetaData> nodeMetaDatas = repositoryClient.getNodesMetaData(nmdp, Integer.MAX_VALUE);
                AddUpdateCommand addDocCmd = new AddUpdateCommand(request);
                addDocCmd.overwrite = overwrite;
                if (!nodeMetaDatas.isEmpty()) {
                    NodeMetaData nodeMetaData = nodeMetaDatas.get(0);
                    if (!(nodeMetaData.getTxnId() > node.getTxnId())) {
                    /*
                        if (mayHaveChildren(nodeMetaData))
                        {
                            cascadeUpdate(nodeMetaData, overwrite, request, processor);
                        }
            				 */
                    }
                    if (node.getTxnId() == Long.MAX_VALUE) {
                        // This is a re-index. We need to clear the txnId from the pr
                        this.cleanContentCache.remove(nodeMetaData.getTxnId());
                    }
                    if ((node.getStatus() == SolrApiNodeStatus.UPDATED) || (node.getStatus() == SolrApiNodeStatus.UNKNOWN)) {
                        // check index control
                        Map<QName, PropertyValue> properties = nodeMetaData.getProperties();
                        StringPropertyValue pValue = (StringPropertyValue) properties.get(ContentModel.PROP_IS_INDEXED);
                        if (pValue != null) {
                            Boolean isIndexed = Boolean.valueOf(pValue.getValue());
                            if (!isIndexed.booleanValue()) {
                                if (log.isDebugEnabled()) {
                                    log.debug(".. clearing unindexed");
                                }
                                deleteNode(processor, request, node);
                                SolrInputDocument doc = createNewDoc(nodeMetaData, DOC_TYPE_UNINDEXED_NODE);
                                solrContentStore.storeDocOnSolrContentStore(nodeMetaData, doc);
                                addDocCmd.solrDoc = doc;
                                processor.processAdd(addDocCmd);
                                long end = System.nanoTime();
                                this.trackerStats.addNodeTime(end - start);
                                return;
                            }
                        }
                        // Make sure any unindexed or error doc is removed.
                        if (log.isDebugEnabled()) {
                            log.debug(".. deleting node " + node.getId());
                        }
                        deleteNode(processor, request, node);
                        SolrInputDocument doc = createNewDoc(nodeMetaData, DOC_TYPE_NODE);
                        addToNewDocAndCache(nodeMetaData, doc);
                        addDocCmd.solrDoc = doc;
                        processor.processAdd(addDocCmd);
                    }
                }
            // Ends checking for a nodeMetaData
            } finally {
                unlock(nodeId);
            }
        }
        // Ends checking for updated or unknown node status
        long end = System.nanoTime();
        this.trackerStats.addNodeTime(end - start);
    } catch (Exception e) {
        log.warn("Node index failed and skipped for " + node.getId() + " in Tx " + node.getTxnId(), e);
        if (processor == null) {
            if (request == null) {
                request = getLocalSolrQueryRequest();
            }
            processor = this.core.getUpdateProcessingChain(null).createProcessor(request, new SolrQueryResponse());
        }
        if (log.isDebugEnabled()) {
            log.debug(".. deleting on exception");
        }
        deleteNode(processor, request, node);
        AddUpdateCommand addDocCmd = new AddUpdateCommand(request);
        addDocCmd.overwrite = overwrite;
        SolrInputDocument doc = new SolrInputDocument();
        doc.addField(FIELD_SOLR4_ID, PREFIX_ERROR + node.getId());
        doc.addField(FIELD_VERSION, "0");
        doc.addField(FIELD_DBID, node.getId());
        doc.addField(FIELD_INTXID, node.getTxnId());
        doc.addField(FIELD_EXCEPTION_MESSAGE, e.getMessage());
        doc.addField(FIELD_DOC_TYPE, DOC_TYPE_ERROR_NODE);
        StringWriter stringWriter = new StringWriter(4096);
        PrintWriter printWriter = new PrintWriter(stringWriter, true);
        try {
            e.printStackTrace(printWriter);
            String stack = stringWriter.toString();
            doc.addField(FIELD_EXCEPTION_STACK, stack.length() < 32766 ? stack : stack.substring(0, 32765));
        } finally {
            printWriter.close();
        }
        addDocCmd.solrDoc = doc;
        processor.processAdd(addDocCmd);
    } finally {
        if (processor != null) {
            processor.finish();
        }
        if (request != null) {
            request.close();
        }
    }
}
Also used : StringPropertyValue(org.alfresco.solr.client.StringPropertyValue) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) NodeMetaData(org.alfresco.solr.client.NodeMetaData) QName(org.alfresco.service.namespace.QName) StringPropertyValue(org.alfresco.solr.client.StringPropertyValue) MLTextPropertyValue(org.alfresco.solr.client.MLTextPropertyValue) ContentPropertyValue(org.alfresco.solr.client.ContentPropertyValue) MultiPropertyValue(org.alfresco.solr.client.MultiPropertyValue) PropertyValue(org.alfresco.solr.client.PropertyValue) JSONException(org.json.JSONException) AuthenticationException(org.alfresco.httpclient.AuthenticationException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrInputDocument(org.apache.solr.common.SolrInputDocument) StringWriter(java.io.StringWriter) UpdateRequestProcessor(org.apache.solr.update.processor.UpdateRequestProcessor) NodeMetaDataParameters(org.alfresco.solr.client.NodeMetaDataParameters) AddUpdateCommand(org.apache.solr.update.AddUpdateCommand) PrintWriter(java.io.PrintWriter)

Example 84 with AddUpdateCommand

use of org.apache.solr.update.AddUpdateCommand in project SearchServices by Alfresco.

the class SolrInformationServer method indexAclTransaction.

@Override
public void indexAclTransaction(AclChangeSet changeSet, boolean overwrite) throws IOException {
    canUpdate();
    SolrQueryRequest request = null;
    UpdateRequestProcessor processor = null;
    try {
        request = getLocalSolrQueryRequest();
        processor = this.core.getUpdateProcessingChain(null).createProcessor(request, new SolrQueryResponse());
        AddUpdateCommand cmd = new AddUpdateCommand(request);
        cmd.overwrite = overwrite;
        SolrInputDocument input = new SolrInputDocument();
        input.addField(FIELD_SOLR4_ID, AlfrescoSolrDataModel.getAclChangeSetDocumentId(changeSet.getId()));
        input.addField(FIELD_VERSION, "0");
        input.addField(FIELD_ACLTXID, changeSet.getId());
        input.addField(FIELD_INACLTXID, changeSet.getId());
        input.addField(FIELD_ACLTXCOMMITTIME, changeSet.getCommitTimeMs());
        input.addField(FIELD_DOC_TYPE, DOC_TYPE_ACL_TX);
        cmd.solrDoc = input;
        processor.processAdd(cmd);
        putAclTransactionState(processor, request, changeSet);
    } finally {
        if (processor != null) {
            processor.finish();
        }
        if (request != null) {
            request.close();
        }
    }
}
Also used : LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) SolrInputDocument(org.apache.solr.common.SolrInputDocument) UpdateRequestProcessor(org.apache.solr.update.processor.UpdateRequestProcessor) AddUpdateCommand(org.apache.solr.update.AddUpdateCommand)

Example 85 with AddUpdateCommand

use of org.apache.solr.update.AddUpdateCommand in project SearchServices by Alfresco.

the class SolrInformationServer method putAclTransactionState.

public void putAclTransactionState(UpdateRequestProcessor processor, SolrQueryRequest request, AclChangeSet changeSet) throws IOException {
    String version;
    SolrDocument aclState = getState(request, "TRACKER!STATE!ACLTX");
    if (aclState != null) {
        long aclTxCommitTime = this.getFieldValueLong(aclState, FIELD_S_ACLTXCOMMITTIME);
        long aclTxId = this.getFieldValueLong(aclState, FIELD_S_ACLTXID);
        // Acl change sets are ordered by commit time and tie-broken by id
        if (changeSet.getCommitTimeMs() > aclTxCommitTime || changeSet.getCommitTimeMs() == aclTxCommitTime && changeSet.getId() > aclTxId) {
            // Uses optimistic concurrency
            version = this.getFieldValueString(aclState, FIELD_VERSION);
        } else {
            // Should not update in this case
            version = null;
        }
    } else {
        version = "0";
    }
    if (version != null) {
        AddUpdateCommand cmd = new AddUpdateCommand(request);
        cmd.overwrite = true;
        SolrInputDocument input = new SolrInputDocument();
        input.addField(FIELD_SOLR4_ID, "TRACKER!STATE!ACLTX");
        input.addField(FIELD_VERSION, version);
        input.addField(FIELD_S_ACLTXID, changeSet.getId());
        input.addField(FIELD_S_INACLTXID, changeSet.getId());
        input.addField(FIELD_S_ACLTXCOMMITTIME, changeSet.getCommitTimeMs());
        input.addField(FIELD_DOC_TYPE, DOC_TYPE_STATE);
        cmd.solrDoc = input;
        processor.processAdd(cmd);
    }
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrDocument(org.apache.solr.common.SolrDocument) AddUpdateCommand(org.apache.solr.update.AddUpdateCommand)

Aggregations

AddUpdateCommand (org.apache.solr.update.AddUpdateCommand)87 SolrInputDocument (org.apache.solr.common.SolrInputDocument)59 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)41 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)37 Test (org.junit.Test)34 LocalSolrQueryRequest (org.apache.solr.request.LocalSolrQueryRequest)23 BufferingRequestProcessor (org.apache.solr.update.processor.BufferingRequestProcessor)19 ContentStreamBase (org.apache.solr.common.util.ContentStreamBase)17 SolrInputField (org.apache.solr.common.SolrInputField)14 UpdateRequestProcessor (org.apache.solr.update.processor.UpdateRequestProcessor)14 ArrayList (java.util.ArrayList)12 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)11 JsonLoader (org.apache.solr.handler.loader.JsonLoader)11 IOException (java.io.IOException)8 SkipExistingDocumentsUpdateProcessor (org.apache.solr.update.processor.SkipExistingDocumentsProcessorFactory.SkipExistingDocumentsUpdateProcessor)8 SolrException (org.apache.solr.common.SolrException)7 CommitUpdateCommand (org.apache.solr.update.CommitUpdateCommand)7 DeleteUpdateCommand (org.apache.solr.update.DeleteUpdateCommand)7 SolrCore (org.apache.solr.core.SolrCore)6 NamedList (org.apache.solr.common.util.NamedList)5