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);
}
}
}
}
}
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);
}
}
}
}
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);
}
}
}
}
}
}
}
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);
}
}
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);
}
Aggregations