Search in sources :

Example 1 with TenantAclIdDbId

use of org.alfresco.solr.AlfrescoSolrDataModel.TenantAclIdDbId in project SearchServices by Alfresco.

the class SolrInformationServer method cascadeUpdateV2.

private void cascadeUpdateV2(NodeMetaData parentNodeMetaData, boolean overwrite, SolrQueryRequest request, UpdateRequestProcessor processor) throws AuthenticationException, IOException, JSONException {
    // System.out.println("################ Cascade update V2 !");
    RefCounted<SolrIndexSearcher> refCounted = null;
    IntArrayList docList = null;
    HashSet<Long> childIds = new HashSet<Long>();
    try {
        refCounted = core.getSearcher();
        SolrIndexSearcher searcher = refCounted.get();
        BooleanQuery.Builder builder = new BooleanQuery.Builder();
        TermQuery termQuery = new TermQuery(new Term(FIELD_ANCESTOR, parentNodeMetaData.getNodeRef().toString()));
        BooleanClause booleanClause = new BooleanClause(termQuery, BooleanClause.Occur.MUST);
        builder.add(booleanClause);
        BooleanQuery booleanQuery = builder.build();
        // System.out.println("################ ANCESTOR QUERY:"+booleanQuery.toString());
        DocListCollector collector = new DocListCollector();
        searcher.search(booleanQuery, collector);
        docList = collector.getDocs();
        int size = docList.size();
        Set set = new HashSet();
        set.add(FIELD_SOLR4_ID);
        for (int i = 0; i < size; i++) {
            int docId = docList.get(i);
            Document document = searcher.doc(docId, set);
            IndexableField indexableField = document.getField(FIELD_SOLR4_ID);
            String id = indexableField.stringValue();
            TenantAclIdDbId ids = AlfrescoSolrDataModel.decodeNodeDocumentId(id);
            childIds.add(ids.dbId);
        }
    } finally {
        refCounted.decref();
    }
    for (Long childId : childIds) {
        NodeMetaDataParameters nmdp = new NodeMetaDataParameters();
        nmdp.setFromNodeId(childId);
        nmdp.setToNodeId(childId);
        nmdp.setIncludeAclId(false);
        nmdp.setIncludeAspects(false);
        nmdp.setIncludeChildAssociations(false);
        nmdp.setIncludeChildIds(true);
        nmdp.setIncludeNodeRef(false);
        nmdp.setIncludeOwner(false);
        nmdp.setIncludeParentAssociations(false);
        // We only care about the path and ancestors (which is included) for this case
        nmdp.setIncludePaths(true);
        nmdp.setIncludeProperties(false);
        nmdp.setIncludeType(false);
        nmdp.setIncludeTxnId(true);
        // Gets only one
        List<NodeMetaData> nodeMetaDatas = repositoryClient.getNodesMetaData(nmdp, 1);
        if (!nodeMetaDatas.isEmpty()) {
            NodeMetaData nodeMetaData = nodeMetaDatas.get(0);
            // We do not bring in changes from the future as nodes may switch shards and we do not want the logic here.
            if (nodeMetaData.getTxnId() < parentNodeMetaData.getTxnId()) {
                long nodeId = nodeMetaData.getId();
                try {
                    if (!spinLock(nodeId, 120000)) {
                        throw new IOException("Unable to acquire spinlock on:" + nodeId);
                    }
                    // System.out.println("################ Starting CASCADE UPDATE:"+nodeMetaData.getId());
                    if (log.isDebugEnabled()) {
                        log.debug("... cascade update child doc " + childId);
                    }
                    // Gets the document that we have from the content store and updates it
                    String fixedTenantDomain = AlfrescoSolrDataModel.getTenantId(nodeMetaData.getTenantDomain());
                    SolrInputDocument cachedDoc = solrContentStore.retrieveDocFromSolrContentStore(fixedTenantDomain, nodeMetaData.getId());
                    if (cachedDoc == null) {
                        cachedDoc = recreateSolrDoc(nodeMetaData.getId(), fixedTenantDomain);
                        // This is a work around for ACE-3228/ACE-3258 and the way stores are expunged when deleting a tenant
                        if (cachedDoc == null) {
                            deleteNode(processor, request, nodeMetaData.getId());
                        }
                    }
                    if (cachedDoc != null) {
                        updatePathRelatedFields(nodeMetaData, cachedDoc);
                        updateNamePathRelatedFields(nodeMetaData, cachedDoc);
                        updateAncestorRelatedFields(nodeMetaData, cachedDoc);
                        AddUpdateCommand addDocCmd = new AddUpdateCommand(request);
                        addDocCmd.overwrite = overwrite;
                        addDocCmd.solrDoc = cachedDoc;
                        // System.out.println("######## Final Cascade Doc :"+cachedDoc);
                        processor.processAdd(addDocCmd);
                        solrContentStore.storeDocOnSolrContentStore(fixedTenantDomain, nodeMetaData.getId(), cachedDoc);
                    } else {
                        if (log.isDebugEnabled()) {
                            log.debug("... no child doc found to update " + childId);
                        }
                    }
                } finally {
                    unlock(nodeId);
                }
            }
        }
    }
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) Set(java.util.Set) AclChangeSet(org.alfresco.solr.client.AclChangeSet) LinkedHashSet(java.util.LinkedHashSet) IOpenBitSet(org.alfresco.solr.adapters.IOpenBitSet) HashSet(java.util.HashSet) TenantAclIdDbId(org.alfresco.solr.AlfrescoSolrDataModel.TenantAclIdDbId) BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) Document(org.apache.lucene.document.Document) SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrDocument(org.apache.solr.common.SolrDocument) SolrInputDocument(org.apache.solr.common.SolrInputDocument) NodeMetaDataParameters(org.alfresco.solr.client.NodeMetaDataParameters) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) TermQuery(org.apache.lucene.search.TermQuery) NodeMetaData(org.alfresco.solr.client.NodeMetaData) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) Term(org.apache.lucene.index.Term) IOException(java.io.IOException) BooleanClause(org.apache.lucene.search.BooleanClause) IndexableField(org.apache.lucene.index.IndexableField) IntArrayList(com.carrotsearch.hppc.IntArrayList) AddUpdateCommand(org.apache.solr.update.AddUpdateCommand)

Example 2 with TenantAclIdDbId

use of org.alfresco.solr.AlfrescoSolrDataModel.TenantAclIdDbId in project SearchServices by Alfresco.

the class SolrInformationServer method doUpdateDescendantDocs.

private void doUpdateDescendantDocs(NodeMetaData parentNodeMetaData, boolean overwrite, SolrQueryRequest request, UpdateRequestProcessor processor, LinkedHashSet<Long> stack) throws AuthenticationException, IOException, JSONException {
    if ((skipDescendantDocsForSpecificTypes && typesForSkippingDescendantDocs.contains(parentNodeMetaData.getType())) || (skipDescendantDocsForSpecificAspects && shouldBeIgnoredByAnyAspect(parentNodeMetaData.getAspects()))) {
        return;
    }
    HashSet<Long> childIds = new HashSet<Long>();
    if (parentNodeMetaData.getChildIds() != null) {
        childIds.addAll(parentNodeMetaData.getChildIds());
    }
    String query = FIELD_PARENT + ":\"" + parentNodeMetaData.getNodeRef() + "\"";
    ModifiableSolrParams params = new ModifiableSolrParams(request.getParams());
    params.set("q", query).set("fl", FIELD_SOLR4_ID);
    if (skippingDocsQueryString != null && !skippingDocsQueryString.isEmpty()) {
        params.set("fq", "NOT ( " + skippingDocsQueryString + " )");
    }
    SolrDocumentList docs = cloud.getSolrDocumentList(nativeRequestHandler, request, params);
    for (SolrDocument doc : docs) {
        String id = getFieldValueString(doc, FIELD_SOLR4_ID);
        TenantAclIdDbId ids = AlfrescoSolrDataModel.decodeNodeDocumentId(id);
        childIds.add(ids.dbId);
    }
    for (Long childId : childIds) {
        NodeMetaDataParameters nmdp = new NodeMetaDataParameters();
        nmdp.setFromNodeId(childId);
        nmdp.setToNodeId(childId);
        nmdp.setIncludeAclId(false);
        nmdp.setIncludeAspects(false);
        nmdp.setIncludeChildAssociations(false);
        nmdp.setIncludeChildIds(true);
        nmdp.setIncludeNodeRef(false);
        nmdp.setIncludeOwner(false);
        nmdp.setIncludeParentAssociations(false);
        // We only care about the path and ancestors (which is included) for this case
        nmdp.setIncludePaths(true);
        nmdp.setIncludeProperties(false);
        nmdp.setIncludeType(false);
        nmdp.setIncludeTxnId(false);
        // Gets only one
        List<NodeMetaData> nodeMetaDatas = repositoryClient.getNodesMetaData(nmdp, 1);
        if (!nodeMetaDatas.isEmpty()) {
            NodeMetaData nodeMetaData = nodeMetaDatas.get(0);
            if (mayHaveChildren(nodeMetaData)) {
                updateDescendantDocs(nodeMetaData, overwrite, request, processor, stack);
            }
            try {
                if (!spinLock(childId, 120000)) {
                    // We haven't acquired the lock in over 2 minutes. This really shouldn't happen unless something has gone wrong.
                    throw new IOException("Unable to acquire lock on nodeId:" + childId);
                }
                if (log.isDebugEnabled()) {
                    log.debug("... cascade update child doc " + childId);
                }
                // Gets the document that we have from the content store and updates it
                String fixedTenantDomain = AlfrescoSolrDataModel.getTenantId(nodeMetaData.getTenantDomain());
                SolrInputDocument cachedDoc = solrContentStore.retrieveDocFromSolrContentStore(fixedTenantDomain, nodeMetaData.getId());
                if (cachedDoc != null) {
                    updatePathRelatedFields(nodeMetaData, cachedDoc);
                    updateNamePathRelatedFields(nodeMetaData, cachedDoc);
                    updateAncestorRelatedFields(nodeMetaData, cachedDoc);
                    AddUpdateCommand addDocCmd = new AddUpdateCommand(request);
                    addDocCmd.overwrite = overwrite;
                    addDocCmd.solrDoc = cachedDoc;
                    processor.processAdd(addDocCmd);
                    solrContentStore.storeDocOnSolrContentStore(fixedTenantDomain, nodeMetaData.getId(), cachedDoc);
                } else {
                    if (log.isDebugEnabled()) {
                        log.debug("... no child doc found to update " + childId);
                    }
                }
            } finally {
                unlock(childId);
            }
        }
    }
}
Also used : NodeMetaData(org.alfresco.solr.client.NodeMetaData) TenantAclIdDbId(org.alfresco.solr.AlfrescoSolrDataModel.TenantAclIdDbId) SolrDocumentList(org.apache.solr.common.SolrDocumentList) IOException(java.io.IOException) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrDocument(org.apache.solr.common.SolrDocument) NodeMetaDataParameters(org.alfresco.solr.client.NodeMetaDataParameters) AddUpdateCommand(org.apache.solr.update.AddUpdateCommand) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet)

Example 3 with TenantAclIdDbId

use of org.alfresco.solr.AlfrescoSolrDataModel.TenantAclIdDbId in project SearchServices by Alfresco.

the class CachedDocTransformer method transform.

/* (non-Javadoc)
     * @see org.apache.solr.response.transform.DocTransformer#transform(org.apache.solr.common.SolrDocument, int)
     */
@Override
public void transform(SolrDocument doc, int docid, float score) throws IOException {
    SolrInputDocument cachedDoc = null;
    try {
        String id = getFieldValueString(doc, FIELD_SOLR4_ID);
        TenantAclIdDbId tenantAndDbId = AlfrescoSolrDataModel.decodeNodeDocumentId(id);
        CoreContainer coreContainer = context.getSearcher().getCore().getCoreContainer();
        AlfrescoCoreAdminHandler coreAdminHandler = (AlfrescoCoreAdminHandler) coreContainer.getMultiCoreHandler();
        SolrInformationServer srv = (SolrInformationServer) coreAdminHandler.getInformationServers().get(context.getSearcher().getCore().getName());
        SolrContentStore solrContentStore = srv.getSolrContentStore();
        cachedDoc = solrContentStore.retrieveDocFromSolrContentStore(tenantAndDbId.tenant, tenantAndDbId.dbId);
    } catch (StringIndexOutOfBoundsException e) {
    // ignore invalid forms ....
    }
    if (cachedDoc != null) {
        Collection<String> fieldNames = cachedDoc.getFieldNames();
        for (String fieldName : fieldNames) {
            SchemaField schemaField = context.getSearcher().getSchema().getFieldOrNull(fieldName);
            if (schemaField != null) {
                doc.removeFields(fieldName);
                if (schemaField.multiValued()) {
                    int index = fieldName.lastIndexOf("@{");
                    if (index == -1) {
                        doc.addField(fieldName, cachedDoc.getFieldValues(fieldName));
                    } else {
                        String alfrescoFieldName = AlfrescoSolrDataModel.getInstance().getAlfrescoPropertyFromSchemaField(fieldName);
                        Collection<Object> values = cachedDoc.getFieldValues(fieldName);
                        ArrayList<Object> newValues = new ArrayList<Object>(values.size());
                        for (Object value : values) {
                            if (value instanceof String) {
                                String stringValue = (String) value;
                                int start = stringValue.lastIndexOf('\u0000');
                                if (start == -1) {
                                    newValues.add(stringValue);
                                } else {
                                    newValues.add(stringValue.substring(start + 1));
                                }
                            } else {
                                newValues.add(value);
                            }
                        }
                        doc.removeFields(alfrescoFieldName);
                        doc.addField(alfrescoFieldName, newValues);
                    }
                } else {
                    int index = fieldName.lastIndexOf("@{");
                    if (index == -1) {
                        doc.addField(fieldName, cachedDoc.getFieldValue(fieldName));
                    } else {
                        String alfrescoFieldName = AlfrescoSolrDataModel.getInstance().getAlfrescoPropertyFromSchemaField(fieldName);
                        alfrescoFieldName = alfrescoFieldName.contains(":") ? alfrescoFieldName.replace(":", "_") : alfrescoFieldName;
                        Object value = cachedDoc.getFieldValue(fieldName);
                        if (value instanceof String) {
                            String stringValue = (String) value;
                            int start = stringValue.lastIndexOf('\u0000');
                            if (start == -1) {
                                doc.removeFields(alfrescoFieldName);
                                doc.addField(alfrescoFieldName, stringValue);
                            } else {
                                doc.removeFields(alfrescoFieldName);
                                doc.addField(alfrescoFieldName, stringValue.substring(start + 1));
                            }
                        } else {
                            doc.removeFields(alfrescoFieldName);
                            doc.addField(alfrescoFieldName, value);
                        }
                    }
                }
            }
        }
    }
}
Also used : TenantAclIdDbId(org.alfresco.solr.AlfrescoSolrDataModel.TenantAclIdDbId) SolrContentStore(org.alfresco.solr.content.SolrContentStore) ArrayList(java.util.ArrayList) SolrInformationServer(org.alfresco.solr.SolrInformationServer) SchemaField(org.apache.solr.schema.SchemaField) SolrInputDocument(org.apache.solr.common.SolrInputDocument) CoreContainer(org.apache.solr.core.CoreContainer) AlfrescoCoreAdminHandler(org.alfresco.solr.AlfrescoCoreAdminHandler)

Example 4 with TenantAclIdDbId

use of org.alfresco.solr.AlfrescoSolrDataModel.TenantAclIdDbId in project SearchServices by Alfresco.

the class AlfrescoSolrHighlighter method getDocument.

private Document getDocument(Document doc, SolrQueryRequest req) throws IOException {
    try {
        String id = getFieldValueString(doc, FIELD_SOLR4_ID);
        TenantAclIdDbId tenantAndDbId = AlfrescoSolrDataModel.decodeNodeDocumentId(id);
        CoreContainer coreContainer = req.getSearcher().getCore().getCoreContainer();
        AlfrescoCoreAdminHandler coreAdminHandler = (AlfrescoCoreAdminHandler) coreContainer.getMultiCoreHandler();
        SolrInformationServer srv = (SolrInformationServer) coreAdminHandler.getInformationServers().get(req.getSearcher().getCore().getName());
        SolrContentStore solrContentStore = srv.getSolrContentStore();
        SolrInputDocument sid = solrContentStore.retrieveDocFromSolrContentStore(tenantAndDbId.tenant, tenantAndDbId.dbId);
        if (sid == null) {
            sid = new SolrInputDocument();
            sid.addField(FIELD_SOLR4_ID, id);
            sid.addField("_version_", 0);
            return DocumentBuilder.toDocument(sid, req.getSchema());
        } else {
            sid.removeField(FIELD_SOLR4_ID);
            sid.addField(FIELD_SOLR4_ID, id);
            return DocumentBuilder.toDocument(sid, req.getSchema());
        }
    } catch (StringIndexOutOfBoundsException e) {
        throw new IOException(e);
    }
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) TenantAclIdDbId(org.alfresco.solr.AlfrescoSolrDataModel.TenantAclIdDbId) CoreContainer(org.apache.solr.core.CoreContainer) AlfrescoCoreAdminHandler(org.alfresco.solr.AlfrescoCoreAdminHandler) SolrContentStore(org.alfresco.solr.content.SolrContentStore) IOException(java.io.IOException) SolrInformationServer(org.alfresco.solr.SolrInformationServer)

Example 5 with TenantAclIdDbId

use of org.alfresco.solr.AlfrescoSolrDataModel.TenantAclIdDbId in project SearchServices by Alfresco.

the class SolrDataModelTest method testDecodeSolr4id.

@Test
public void testDecodeSolr4id() {
    String tenant = "TheTenant";
    Long aclId = 987698769860l;
    Long dbId = 9879987l;
    String id = AlfrescoSolrDataModel.getNodeDocumentId(tenant, aclId, dbId);
    TenantAclIdDbId ids = AlfrescoSolrDataModel.decodeNodeDocumentId(id);
    assertEquals(tenant, ids.tenant);
    assertEquals(aclId, ids.alcId);
    assertEquals(dbId, ids.dbId);
}
Also used : TenantAclIdDbId(org.alfresco.solr.AlfrescoSolrDataModel.TenantAclIdDbId) Test(org.junit.Test)

Aggregations

TenantAclIdDbId (org.alfresco.solr.AlfrescoSolrDataModel.TenantAclIdDbId)10 SolrInputDocument (org.apache.solr.common.SolrInputDocument)7 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)4 LinkedHashSet (java.util.LinkedHashSet)4 SolrDocument (org.apache.solr.common.SolrDocument)4 IntArrayList (com.carrotsearch.hppc.IntArrayList)3 AlfrescoCoreAdminHandler (org.alfresco.solr.AlfrescoCoreAdminHandler)3 SolrInformationServer (org.alfresco.solr.SolrInformationServer)3 NodeMetaData (org.alfresco.solr.client.NodeMetaData)3 NodeMetaDataParameters (org.alfresco.solr.client.NodeMetaDataParameters)3 SolrContentStore (org.alfresco.solr.content.SolrContentStore)3 Document (org.apache.lucene.document.Document)3 IndexableField (org.apache.lucene.index.IndexableField)3 Term (org.apache.lucene.index.Term)3 BooleanClause (org.apache.lucene.search.BooleanClause)3 BooleanQuery (org.apache.lucene.search.BooleanQuery)3 TermQuery (org.apache.lucene.search.TermQuery)3 BytesRefBuilder (org.apache.lucene.util.BytesRefBuilder)3