use of org.alfresco.solr.client.PropertyValue in project SearchServices by Alfresco.
the class SolrInformationServer method addPropertiesToDoc.
static void addPropertiesToDoc(Map<QName, PropertyValue> properties, boolean isContentIndexedForNode, SolrInputDocument newDoc, SolrInputDocument cachedDoc, boolean transformContentFlag) throws IOException {
for (QName propertyQName : properties.keySet()) {
newDoc.addField(FIELD_PROPERTIES, propertyQName.toString());
newDoc.addField(FIELD_PROPERTIES, propertyQName.getPrefixString());
PropertyValue value = properties.get(propertyQName);
// System.out.println("####### indexing prop:"+propertyQName.toString()+":"+value.toString());
if (value != null) {
if (value instanceof StringPropertyValue) {
for (FieldInstance field : AlfrescoSolrDataModel.getInstance().getIndexedFieldNamesForProperty(propertyQName).getFields()) {
// System.out.println("####### field:"+field.getField()+":"+value.toString());
addStringPropertyToDoc(newDoc, field, (StringPropertyValue) value, properties);
}
} else if (value instanceof MLTextPropertyValue) {
for (FieldInstance field : AlfrescoSolrDataModel.getInstance().getIndexedFieldNamesForProperty(propertyQName).getFields()) {
addMLTextPropertyToDoc(newDoc, field, (MLTextPropertyValue) value);
}
} else if (value instanceof ContentPropertyValue) {
boolean transform = transformContentFlag;
if (!isContentIndexedForNode) {
transform = false;
}
addContentPropertyToDocUsingCache(newDoc, cachedDoc, propertyQName, (ContentPropertyValue) value, transform);
} else if (value instanceof MultiPropertyValue) {
MultiPropertyValue typedValue = (MultiPropertyValue) value;
for (PropertyValue singleValue : typedValue.getValues()) {
if (singleValue instanceof StringPropertyValue) {
for (FieldInstance field : AlfrescoSolrDataModel.getInstance().getIndexedFieldNamesForProperty(propertyQName).getFields()) {
addStringPropertyToDoc(newDoc, field, (StringPropertyValue) singleValue, properties);
}
} else if (singleValue instanceof MLTextPropertyValue) {
for (FieldInstance field : AlfrescoSolrDataModel.getInstance().getIndexedFieldNamesForProperty(propertyQName).getFields()) {
addMLTextPropertyToDoc(newDoc, field, (MLTextPropertyValue) singleValue);
}
} else if (singleValue instanceof ContentPropertyValue) {
boolean transform = transformContentFlag;
if (!isContentIndexedForNode) {
transform = false;
}
addContentPropertyToDocUsingCache(newDoc, cachedDoc, propertyQName, (ContentPropertyValue) singleValue, transform);
}
}
}
} else {
// NULL property
newDoc.addField(FIELD_NULLPROPERTIES, propertyQName.toString());
}
}
}
use of org.alfresco.solr.client.PropertyValue 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();
}
}
}
Aggregations