Example usage for org.apache.solr.update AddUpdateCommand AddUpdateCommand

List of usage examples for org.apache.solr.update AddUpdateCommand AddUpdateCommand

Introduction

In this page you can find the example usage for org.apache.solr.update AddUpdateCommand AddUpdateCommand.

Prototype

AddUpdateCommand

Source Link

Usage

From source file:com.gu.solr.MergeUtils.java

License:Apache License

public static AddUpdateCommand addCommandFor(SolrInputDocument solrDoc) {
    AddUpdateCommand command = new AddUpdateCommand();
    command.solrDoc = solrDoc;/* w  w  w  .j  a  va  2  s  .  c  o m*/
    command.overwriteCommitted = true;

    return command;
}

From source file:org.alfresco.solr.LegacySolrInformationServer.java

License:Open Source License

@Override
public void indexTransaction(Transaction info, boolean overwrite) throws IOException {
    AddUpdateCommand cmd = new AddUpdateCommand();
    cmd.overwriteCommitted = overwrite;//  ww w  .ja va2 s  .  c o  m
    cmd.overwritePending = overwrite;
    SolrInputDocument input = new SolrInputDocument();
    input.addField(QueryConstants.FIELD_ID, "TX-" + info.getId());
    input.addField(QueryConstants.FIELD_TXID, info.getId());
    input.addField(QueryConstants.FIELD_INTXID, info.getId());
    input.addField(QueryConstants.FIELD_TXCOMMITTIME, info.getCommitTimeMs());
    cmd.solrDoc = input;
    cmd.doc = toDocument(cmd.getSolrInputDocument(), core.getSchema(), dataModel);
    core.getUpdateHandler().addDoc(cmd);
}

From source file:org.alfresco.solr.LegacySolrInformationServer.java

License:Open Source License

@Override
public void indexAclTransaction(AclChangeSet changeSet, boolean overwrite) throws IOException {
    AddUpdateCommand cmd = new AddUpdateCommand();
    cmd.overwriteCommitted = overwrite;//from  ww w  .  j  a v  a 2s  .  com
    cmd.overwritePending = overwrite;
    SolrInputDocument input = new SolrInputDocument();
    input.addField(QueryConstants.FIELD_ID, "ACLTX-" + changeSet.getId());
    input.addField(QueryConstants.FIELD_ACLTXID, changeSet.getId());
    input.addField(QueryConstants.FIELD_INACLTXID, changeSet.getId());
    input.addField(QueryConstants.FIELD_ACLTXCOMMITTIME, changeSet.getCommitTimeMs());
    cmd.solrDoc = input;
    cmd.doc = toDocument(cmd.getSolrInputDocument(), core.getSchema(), dataModel);
    core.getUpdateHandler().addDoc(cmd);
}

From source file:org.alfresco.solr.LegacySolrInformationServer.java

License:Open Source License

@Override
public void indexNode(Node node, boolean overwrite) throws IOException, AuthenticationException, JSONException {
    RefCounted<SolrIndexSearcher> refCounted = null;
    try {//  w w  w  .  j a  v  a  2  s .c o m
        long start = System.nanoTime();

        refCounted = core.getSearcher(false, true, null);
        SolrIndexSearcher solrIndexSearcher = refCounted.get();

        if ((node.getStatus() == SolrApiNodeStatus.DELETED)
                || (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) {
                // Fake the empty node metadata for this parent deleted node
                NodeMetaData nodeMetaData = new NodeMetaData();
                nodeMetaData.setId(node.getId());
                nodeMetaData.setType(ContentModel.TYPE_DELETED);
                nodeMetaData.setNodeRef(new NodeRef(node.getNodeRef()));
                nodeMetaData.setTxnId(node.getTxnId());
                nodeMetaDatas = Collections.singletonList(nodeMetaData);
            } else {
                nodeMetaDatas = coreTracker.getNodesMetaData(nmdp, MAX_RESULTS_NODES_META_DATA);
            }
            for (NodeMetaData nodeMetaData : nodeMetaDatas) {
                if (nodeMetaData.getTxnId() > node.getTxnId()) {
                    // the node has moved on to a later transaction
                    // it will be indexed later
                    continue;
                }
                LinkedHashSet<Long> visited = new LinkedHashSet<Long>();
                updateDescendantAuxDocs(nodeMetaData, overwrite, solrIndexSearcher, visited,
                        solrIndexSearcher.getDocSet(skippingDocsQuery));
            }

            log.debug(".. deleting");
            DeleteUpdateCommand docCmd = new DeleteUpdateCommand();
            docCmd.id = "LEAF-" + node.getId();
            docCmd.fromPending = true;
            docCmd.fromCommitted = true;
            core.getUpdateHandler().delete(docCmd);

            docCmd = new DeleteUpdateCommand();
            docCmd.id = "AUX-" + node.getId();
            docCmd.fromPending = true;
            docCmd.fromCommitted = true;
            core.getUpdateHandler().delete(docCmd);

            docCmd = new DeleteUpdateCommand();
            docCmd.id = "UNINDEXED-" + node.getId();
            docCmd.fromPending = true;
            docCmd.fromCommitted = true;
            core.getUpdateHandler().delete(docCmd);

            docCmd = new DeleteUpdateCommand();
            docCmd.id = PREFIX_ERROR + node.getId();
            docCmd.fromPending = true;
            docCmd.fromCommitted = true;
            core.getUpdateHandler().delete(docCmd);

        }

        if ((node.getStatus() == SolrApiNodeStatus.UPDATED)
                || (node.getStatus() == SolrApiNodeStatus.UNKNOWN)) {

            log.info(".. updating");
            NodeMetaDataParameters nmdp = new NodeMetaDataParameters();
            nmdp.setFromNodeId(node.getId());
            nmdp.setToNodeId(node.getId());

            List<NodeMetaData> nodeMetaDatas = coreTracker.getNodesMetaData(nmdp, MAX_RESULTS_NODES_META_DATA);

            AddUpdateCommand leafDocCmd = new AddUpdateCommand();
            leafDocCmd.overwriteCommitted = overwrite;
            leafDocCmd.overwritePending = overwrite;
            AddUpdateCommand auxDocCmd = new AddUpdateCommand();
            auxDocCmd.overwriteCommitted = overwrite;
            auxDocCmd.overwritePending = overwrite;

            ArrayList<Reader> toClose = new ArrayList<Reader>();
            ArrayList<File> toDelete = new ArrayList<File>();

            for (NodeMetaData nodeMetaData : nodeMetaDatas) {
                if (nodeMetaData.getTxnId() > node.getTxnId()) {
                    // the node has moved on to a later transaction
                    // it will be indexed later
                    continue;
                }

                if (mayHaveChildren(nodeMetaData)) {
                    log.info(".. checking for path change");
                    BooleanQuery bQuery = new BooleanQuery();
                    bQuery.add(new TermQuery(
                            new Term(QueryConstants.FIELD_DBID, NumericEncoder.encode(nodeMetaData.getId()))),
                            Occur.MUST);
                    bQuery.add(new TermQuery(new Term(QueryConstants.FIELD_PARENT_ASSOC_CRC,
                            NumericEncoder.encode(nodeMetaData.getParentAssocsCrc()))), Occur.MUST);
                    DocSet docSet = solrIndexSearcher.getDocSet(bQuery);
                    if (docSet.size() > 0) {
                        log.debug("... found aux match");
                    } else {
                        docSet = solrIndexSearcher.getDocSet(new TermQuery(new Term(QueryConstants.FIELD_DBID,
                                NumericEncoder.encode(nodeMetaData.getId()))));
                        if (docSet.size() > 0) {
                            log.debug("... cascade updating aux doc");
                            LinkedHashSet<Long> visited = new LinkedHashSet<Long>();
                            updateDescendantAuxDocs(nodeMetaData, overwrite, solrIndexSearcher, visited,
                                    solrIndexSearcher.getDocSet(skippingDocsQuery));
                        } else {
                            log.debug("... no aux doc");
                        }
                    }
                }

                Map<QName, PropertyValue> properties = nodeMetaData.getProperties();

                // check index control

                if (properties.containsKey(ContentModel.PROP_IS_INDEXED)) {
                    StringPropertyValue pValue = (StringPropertyValue) properties
                            .get(ContentModel.PROP_IS_INDEXED);
                    if (pValue != null) {
                        Boolean isIndexed = Boolean.valueOf(pValue.getValue());
                        if ((isIndexed != null) && (isIndexed.booleanValue() == false)) {
                            DeleteUpdateCommand docCmd = new DeleteUpdateCommand();
                            docCmd.id = "LEAF-" + node.getId();
                            docCmd.fromPending = true;
                            docCmd.fromCommitted = true;
                            core.getUpdateHandler().delete(docCmd);

                            docCmd = new DeleteUpdateCommand();
                            docCmd.id = "AUX-" + node.getId();
                            docCmd.fromPending = true;
                            docCmd.fromCommitted = true;
                            core.getUpdateHandler().delete(docCmd);

                            docCmd = new DeleteUpdateCommand();
                            docCmd.id = PREFIX_ERROR + node.getId();
                            docCmd.fromPending = true;
                            docCmd.fromCommitted = true;
                            core.getUpdateHandler().delete(docCmd);

                            SolrInputDocument doc = new SolrInputDocument();
                            doc.addField(QueryConstants.FIELD_ID, "UNINDEXED-" + nodeMetaData.getId());
                            doc.addField(QueryConstants.FIELD_DBID, nodeMetaData.getId());
                            doc.addField(QueryConstants.FIELD_LID, nodeMetaData.getNodeRef());
                            doc.addField(QueryConstants.FIELD_INTXID, nodeMetaData.getTxnId());

                            leafDocCmd.solrDoc = doc;
                            leafDocCmd.doc = toDocument(leafDocCmd.getSolrInputDocument(), core.getSchema(),
                                    dataModel);

                            if (leafDocCmd.doc != null && recordUnindexedNodes) {
                                core.getUpdateHandler().addDoc(leafDocCmd);
                            }

                            long end = System.nanoTime();
                            coreTracker.getTrackerStats().addNodeTime(end - start);
                            return;
                        }
                    }
                }

                boolean isContentIndexedForNode = true;
                if (properties.containsKey(ContentModel.PROP_IS_CONTENT_INDEXED)) {
                    StringPropertyValue pValue = (StringPropertyValue) properties
                            .get(ContentModel.PROP_IS_CONTENT_INDEXED);
                    if (pValue != null) {
                        Boolean isIndexed = Boolean.valueOf(pValue.getValue());
                        if ((isIndexed != null) && (isIndexed.booleanValue() == false)) {
                            isContentIndexedForNode = false;
                        }
                    }
                }

                // Make sure any unindexed doc is removed.
                DeleteUpdateCommand docCmd = new DeleteUpdateCommand();
                docCmd.id = "UNINDEXED-" + node.getId();
                docCmd.fromPending = true;
                docCmd.fromCommitted = true;
                core.getUpdateHandler().delete(docCmd);

                docCmd = new DeleteUpdateCommand();
                docCmd.id = PREFIX_ERROR + node.getId();
                docCmd.fromPending = true;
                docCmd.fromCommitted = true;
                core.getUpdateHandler().delete(docCmd);

                SolrInputDocument doc = new SolrInputDocument();
                doc.addField(QueryConstants.FIELD_ID, "LEAF-" + nodeMetaData.getId());
                doc.addField(QueryConstants.FIELD_DBID, nodeMetaData.getId());
                doc.addField(QueryConstants.FIELD_LID, nodeMetaData.getNodeRef());
                doc.addField(QueryConstants.FIELD_INTXID, nodeMetaData.getTxnId());

                for (QName propertyQname : properties.keySet()) {
                    if (dataModel.isIndexedOrStored(propertyQname)) {
                        PropertyValue value = properties.get(propertyQname);
                        if (value != null) {
                            if (value instanceof ContentPropertyValue) {
                                addContentPropertyToDoc(doc, toClose, toDelete, nodeMetaData, propertyQname,
                                        (ContentPropertyValue) value, isContentIndexedForNode);
                            } else if (value instanceof MLTextPropertyValue) {
                                addMLTextPropertyToDoc(doc, propertyQname, (MLTextPropertyValue) value);
                            } else if (value instanceof MultiPropertyValue) {
                                MultiPropertyValue typedValue = (MultiPropertyValue) value;
                                for (PropertyValue singleValue : typedValue.getValues()) {
                                    if (singleValue instanceof ContentPropertyValue) {
                                        addContentPropertyToDoc(doc, toClose, toDelete, nodeMetaData,
                                                propertyQname, (ContentPropertyValue) singleValue,
                                                isContentIndexedForNode);
                                    } else if (singleValue instanceof MLTextPropertyValue) {
                                        addMLTextPropertyToDoc(doc, propertyQname,
                                                (MLTextPropertyValue) singleValue);

                                    } else if (singleValue instanceof StringPropertyValue) {
                                        addStringPropertyToDoc(doc, propertyQname,
                                                (StringPropertyValue) singleValue, properties);
                                    }
                                }
                            } else if (value instanceof StringPropertyValue) {
                                addStringPropertyToDoc(doc, propertyQname, (StringPropertyValue) value,
                                        properties);
                            }

                        }
                    }
                }
                doc.addField(QueryConstants.FIELD_TYPE, nodeMetaData.getType().toString());
                for (QName aspect : nodeMetaData.getAspects()) {
                    doc.addField(QueryConstants.FIELD_ASPECT, aspect.toString());
                }
                doc.addField(QueryConstants.FIELD_ISNODE, "T");
                doc.addField(QueryConstants.FIELD_FTSSTATUS, "Clean");
                // TODO: Place holder to test tenant queries
                String tenant = nodeMetaData.getTenantDomain();
                if (tenant.length() > 0) {
                    doc.addField(QueryConstants.FIELD_TENANT, nodeMetaData.getTenantDomain());
                } else {
                    doc.addField(QueryConstants.FIELD_TENANT, "_DEFAULT_");
                }

                leafDocCmd.solrDoc = doc;
                leafDocCmd.doc = toDocument(leafDocCmd.getSolrInputDocument(), core.getSchema(), dataModel);

                SolrInputDocument aux = createAuxDoc(nodeMetaData);
                auxDocCmd.solrDoc = aux;
                auxDocCmd.doc = toDocument(auxDocCmd.getSolrInputDocument(), core.getSchema(), dataModel);

            }

            if (leafDocCmd.doc != null) {
                core.getUpdateHandler().addDoc(leafDocCmd);
            }
            if (auxDocCmd.doc != null) {
                core.getUpdateHandler().addDoc(auxDocCmd);
            }

            for (Reader forClose : toClose) {
                try {
                    forClose.close();
                } catch (IOException ioe) {
                }

            }

            for (File file : toDelete) {
                file.delete();
            }

        }
        long end = System.nanoTime();
        coreTracker.getTrackerStats().addNodeTime(end - start);
    } catch (Exception e) {
        // generic recovery
        // Add failed node marker to try later
        // TODO: add to reporting
        // TODO: Store exception for display via query
        // TODO: retry failed

        DeleteUpdateCommand docCmd = new DeleteUpdateCommand();
        docCmd.id = "LEAF-" + node.getId();
        docCmd.fromPending = true;
        docCmd.fromCommitted = true;
        core.getUpdateHandler().delete(docCmd);

        docCmd = new DeleteUpdateCommand();
        docCmd.id = "AUX-" + node.getId();
        docCmd.fromPending = true;
        docCmd.fromCommitted = true;
        core.getUpdateHandler().delete(docCmd);

        docCmd = new DeleteUpdateCommand();
        docCmd.id = "UNINDEXED-" + node.getId();
        docCmd.fromPending = true;
        docCmd.fromCommitted = true;
        core.getUpdateHandler().delete(docCmd);

        AddUpdateCommand leafDocCmd = new AddUpdateCommand();
        leafDocCmd.overwriteCommitted = overwrite;
        leafDocCmd.overwritePending = overwrite;

        SolrInputDocument doc = new SolrInputDocument();
        doc.addField(QueryConstants.FIELD_ID, PREFIX_ERROR + node.getId());
        doc.addField(QueryConstants.FIELD_DBID, node.getId());
        doc.addField(QueryConstants.FIELD_INTXID, node.getTxnId());
        doc.addField(QueryConstants.FIELD_EXCEPTION_MESSAGE, e.getMessage());

        StringWriter stringWriter = new StringWriter(4096);
        PrintWriter printWriter = new PrintWriter(stringWriter, true);
        try {
            e.printStackTrace(printWriter);
            doc.addField(QueryConstants.FIELD_EXCEPTION_STACK, stringWriter.toString());
        } finally {
            printWriter.close();
        }

        leafDocCmd.solrDoc = doc;
        leafDocCmd.doc = toDocument(leafDocCmd.getSolrInputDocument(), core.getSchema(), dataModel);

        if (leafDocCmd.doc != null) {
            core.getUpdateHandler().addDoc(leafDocCmd);
        }

        log.warn("Node index failed and skipped for " + node.getId() + " in Tx " + node.getTxnId(), e);
    } finally {
        if (refCounted != null) {
            refCounted.decref();
        }
    }

}

From source file:org.alfresco.solr.LegacySolrInformationServer.java

License:Open Source License

private void doUpdateDescendantAuxDocs(NodeMetaData parentNodeMetaData, boolean overwrite,
        SolrIndexSearcher solrIndexSearcher, LinkedHashSet<Long> stack, DocSet skippingDocs)
        throws AuthenticationException, IOException, JSONException {
    if ((skipDescendantAuxDocsForSpecificTypes
            && typesForSkippingDescendantAuxDocs.contains(parentNodeMetaData.getType()))
            || (skipDescendantAuxDocsForSpecificAspects
                    && shouldBeIgnoredByAnyAspect(parentNodeMetaData.getAspects()))) {
        return;/*from   ww  w .j a  va2 s . c  o  m*/
    }

    HashSet<Long> childIds = new HashSet<Long>();

    if (parentNodeMetaData.getChildIds() != null) {
        childIds.addAll(parentNodeMetaData.getChildIds());
    }

    BooleanQuery bQuery = new BooleanQuery();
    bQuery.add(new TermQuery(new Term(QueryConstants.FIELD_PARENT, parentNodeMetaData.getNodeRef().toString())),
            Occur.MUST);
    DocSet docSet = solrIndexSearcher.getDocSet(bQuery);
    ResizeableArrayList<CacheEntry> indexedByDocId = (ResizeableArrayList<CacheEntry>) solrIndexSearcher
            .cacheLookup(AlfrescoSolrEventListener.ALFRESCO_ARRAYLIST_CACHE,
                    AlfrescoSolrEventListener.KEY_DBID_LEAF_PATH_BY_DOC_ID);
    if (docSet instanceof BitDocSet) {
        BitDocSet source = (BitDocSet) docSet;
        OpenBitSet openBitSet = source.getBits();
        int current = -1;
        while ((current = openBitSet.nextSetBit(current + 1)) != -1) {
            if (!skippingDocs.exists(current)) {
                CacheEntry entry = indexedByDocId.get(current);
                childIds.add(entry.getDbid());
            }
        }
    } else {
        for (DocIterator it = docSet.iterator(); it.hasNext(); /* */) {
            int current = it.nextDoc();
            if (!skippingDocs.exists(current)) {
                CacheEntry entry = indexedByDocId.get(current);
                childIds.add(entry.getDbid());
            }
        }
    }

    for (Long childId : childIds) {
        if (!shouldElementBeIgnored(childId, solrIndexSearcher, skippingDocs)) {
            NodeMetaDataParameters nmdp = new NodeMetaDataParameters();
            nmdp.setFromNodeId(childId);
            nmdp.setToNodeId(childId);
            nmdp.setIncludeAclId(true);
            nmdp.setIncludeAspects(true);
            nmdp.setIncludeChildAssociations(false);
            nmdp.setIncludeChildIds(true);
            nmdp.setIncludeNodeRef(true);
            nmdp.setIncludeOwner(true);
            nmdp.setIncludeParentAssociations(true);
            nmdp.setIncludePaths(true);
            nmdp.setIncludeProperties(false);
            nmdp.setIncludeType(true);
            nmdp.setIncludeTxnId(true);
            // call back to core tracker to talk to client
            List<NodeMetaData> nodeMetaDatas = coreTracker.getNodesMetaData(nmdp, MAX_RESULTS_NODES_META_DATA);

            for (NodeMetaData nodeMetaData : nodeMetaDatas) {
                if (mayHaveChildren(nodeMetaData)) {
                    updateDescendantAuxDocs(nodeMetaData, overwrite, solrIndexSearcher, stack, skippingDocs);
                }

                // Avoid adding aux docs for stuff yet to be indexed or unindexed (via the index control aspect)
                log.info(".. checking aux doc exists in index before we update it");
                Query query = new TermQuery(new Term(QueryConstants.FIELD_ID, "AUX-" + childId));
                DocSet auxSet = solrIndexSearcher.getDocSet(query);
                if (auxSet.size() > 0) {
                    log.debug("... cascade update aux doc " + childId);

                    SolrInputDocument aux = createAuxDoc(nodeMetaData);
                    AddUpdateCommand auxDocCmd = new AddUpdateCommand();
                    auxDocCmd.overwriteCommitted = overwrite;
                    auxDocCmd.overwritePending = overwrite;
                    auxDocCmd.solrDoc = aux;
                    auxDocCmd.doc = toDocument(auxDocCmd.getSolrInputDocument(), core.getSchema(), dataModel);

                    core.getUpdateHandler().addDoc(auxDocCmd);
                } else {
                    log.debug("... no aux doc found to update " + childId);
                }
            }
        }
    }
}

From source file:org.alfresco.solr.LegacySolrInformationServer.java

License:Open Source License

@Override
public long indexAcl(List<AclReaders> aclReaderList, boolean overwrite) throws IOException {
    long start = System.nanoTime();
    for (AclReaders aclReaders : aclReaderList) {
        AddUpdateCommand cmd = new AddUpdateCommand();
        cmd.overwriteCommitted = overwrite;
        cmd.overwritePending = overwrite;
        SolrInputDocument input = new SolrInputDocument();
        input.addField(QueryConstants.FIELD_ID, "ACL-" + aclReaders.getId());
        input.addField(QueryConstants.FIELD_ACLID, aclReaders.getId());
        input.addField(QueryConstants.FIELD_INACLTXID, aclReaders.getAclChangeSetId());
        String tenant = aclReaders.getTenantDomain();
        for (String reader : aclReaders.getReaders()) {
            switch (AuthorityType.getAuthorityType(reader)) {
            case USER:
                input.addField(QueryConstants.FIELD_READER, reader);
                break;
            case GROUP:
            case EVERYONE:
            case GUEST:
                if (tenant.length() == 0) {
                    // Default tenant matches 4.0
                    input.addField(QueryConstants.FIELD_READER, reader);
                } else {
                    input.addField(QueryConstants.FIELD_READER, reader + "@" + tenant);
                }/* ww  w .  j  a  v  a2  s  .  c  o m*/
                break;
            default:
                input.addField(QueryConstants.FIELD_READER, reader);
                break;
            }
        }
        cmd.solrDoc = input;
        cmd.doc = LegacySolrInformationServer.toDocument(cmd.getSolrInputDocument(), core.getSchema(),
                dataModel);
        core.getUpdateHandler().addDoc(cmd);
    }

    long end = System.nanoTime();
    return (end - start);
}

From source file:org.alfresco.solr.LegacySolrInformationServer.java

License:Open Source License

@Override
public void checkCache() throws IOException {
    AddUpdateCommand checkDocCmd = new AddUpdateCommand();
    checkDocCmd.indexedId = "CHECK_CACHE";
    core.getUpdateHandler().addDoc(checkDocCmd);
    this.commit();
}