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

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

Introduction

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

Prototype

DeleteUpdateCommand

Source Link

Usage

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 {//from w  ww.  ja va  2s.  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 deleteByQuery(SolrIndexSearcher solrIndexSearcher, Query query) throws IOException {
    HashSet<String> idsToDelete = new HashSet<String>();

    DocSet docSet = solrIndexSearcher.getDocSet(query);
    if (docSet instanceof BitDocSet) {
        BitDocSet source = (BitDocSet) docSet;
        OpenBitSet openBitSet = source.getBits();
        int current = -1;
        while ((current = openBitSet.nextSetBit(current + 1)) != -1) {
            Document doc = solrIndexSearcher.doc(current, Collections.singleton(QueryConstants.FIELD_ID));
            Fieldable fieldable = doc.getFieldable(QueryConstants.FIELD_ID);
            if (fieldable != null) {
                idsToDelete.add(fieldable.stringValue());
            }//from www  . j a v  a  2 s .  co m

        }
    } else {
        for (DocIterator it = docSet.iterator(); it.hasNext(); /* */) {
            Document doc = solrIndexSearcher.doc(it.nextDoc(), Collections.singleton(QueryConstants.FIELD_ID));
            Fieldable fieldable = doc.getFieldable(QueryConstants.FIELD_ID);
            if (fieldable != null) {
                idsToDelete.add(fieldable.stringValue());
            }
        }
    }

    for (String idToDelete : idsToDelete) {
        DeleteUpdateCommand docCmd = new DeleteUpdateCommand();
        docCmd.id = idToDelete;
        docCmd.fromPending = true;
        docCmd.fromCommitted = true;
        core.getUpdateHandler().delete(docCmd);
    }

}