use of org.apache.solr.common.SolrInputDocument in project incubator-atlas by apache.
the class Solr5Index method mutate.
@Override
public void mutate(Map<String, Map<String, IndexMutation>> mutations, KeyInformation.IndexRetriever informations, BaseTransaction tx) throws BackendException {
logger.debug("Mutating SOLR");
try {
for (Map.Entry<String, Map<String, IndexMutation>> stores : mutations.entrySet()) {
String collectionName = stores.getKey();
String keyIdField = getKeyFieldId(collectionName);
List<String> deleteIds = new ArrayList<>();
Collection<SolrInputDocument> changes = new ArrayList<>();
for (Map.Entry<String, IndexMutation> entry : stores.getValue().entrySet()) {
String docId = entry.getKey();
IndexMutation mutation = entry.getValue();
Preconditions.checkArgument(!(mutation.isNew() && mutation.isDeleted()));
Preconditions.checkArgument(!mutation.isNew() || !mutation.hasDeletions());
Preconditions.checkArgument(!mutation.isDeleted() || !mutation.hasAdditions());
//Handle any deletions
if (mutation.hasDeletions()) {
if (mutation.isDeleted()) {
logger.trace("Deleting entire document {}", docId);
deleteIds.add(docId);
} else {
HashSet<IndexEntry> fieldDeletions = Sets.newHashSet(mutation.getDeletions());
if (mutation.hasAdditions()) {
for (IndexEntry indexEntry : mutation.getAdditions()) {
fieldDeletions.remove(indexEntry);
}
}
deleteIndividualFieldsFromIndex(collectionName, keyIdField, docId, fieldDeletions);
}
}
if (mutation.hasAdditions()) {
int ttl = mutation.determineTTL();
SolrInputDocument doc = new SolrInputDocument();
doc.setField(keyIdField, docId);
boolean isNewDoc = mutation.isNew();
if (isNewDoc)
logger.trace("Adding new document {}", docId);
for (IndexEntry e : mutation.getAdditions()) {
final Object fieldValue = convertValue(e.value);
doc.setField(e.field, isNewDoc ? fieldValue : new HashMap<String, Object>(1) {
{
put("set", fieldValue);
}
});
}
if (ttl > 0) {
Preconditions.checkArgument(isNewDoc, "Solr only supports TTL on new documents [%s]", docId);
doc.setField(ttlField, String.format("+%dSECONDS", ttl));
}
changes.add(doc);
}
}
commitDeletes(collectionName, deleteIds);
commitDocumentChanges(collectionName, changes);
}
} catch (Exception e) {
throw storageException(e);
}
}
use of org.apache.solr.common.SolrInputDocument in project incubator-atlas by apache.
the class Solr5Index method restore.
@Override
public void restore(Map<String, Map<String, List<IndexEntry>>> documents, KeyInformation.IndexRetriever informations, BaseTransaction tx) throws BackendException {
try {
for (Map.Entry<String, Map<String, List<IndexEntry>>> stores : documents.entrySet()) {
final String collectionName = stores.getKey();
List<String> deleteIds = new ArrayList<>();
List<SolrInputDocument> newDocuments = new ArrayList<>();
for (Map.Entry<String, List<IndexEntry>> entry : stores.getValue().entrySet()) {
final String docID = entry.getKey();
final List<IndexEntry> content = entry.getValue();
if (content == null || content.isEmpty()) {
if (logger.isTraceEnabled())
logger.trace("Deleting document [{}]", docID);
deleteIds.add(docID);
continue;
}
newDocuments.add(new SolrInputDocument() {
{
setField(getKeyFieldId(collectionName), docID);
for (IndexEntry addition : content) {
Object fieldValue = addition.value;
setField(addition.field, convertValue(fieldValue));
}
}
});
}
commitDeletes(collectionName, deleteIds);
commitDocumentChanges(collectionName, newDocuments);
}
} catch (Exception e) {
throw new TemporaryBackendException("Could not restore Solr index", e);
}
}
use of org.apache.solr.common.SolrInputDocument in project incubator-atlas by apache.
the class Solr5Index method deleteIndividualFieldsFromIndex.
private void deleteIndividualFieldsFromIndex(String collectionName, String keyIdField, String docId, HashSet<IndexEntry> fieldDeletions) throws SolrServerException, IOException {
if (fieldDeletions.isEmpty())
return;
Map<String, String> fieldDeletes = new HashMap<String, String>(1) {
{
put("set", null);
}
};
SolrInputDocument doc = new SolrInputDocument();
doc.addField(keyIdField, docId);
StringBuilder sb = new StringBuilder();
for (IndexEntry fieldToDelete : fieldDeletions) {
doc.addField(fieldToDelete.field, fieldDeletes);
sb.append(fieldToDelete).append(",");
}
if (logger.isTraceEnabled())
logger.trace("Deleting individual fields [{}] for document {}", sb.toString(), docId);
UpdateRequest singleDocument = newUpdateRequest();
singleDocument.add(doc);
solrClient.request(singleDocument, collectionName);
}
use of org.apache.solr.common.SolrInputDocument in project jackrabbit-oak by apache.
the class SolrIndexEditor method docFromState.
private SolrInputDocument docFromState(NodeState state) {
SolrInputDocument inputDocument = new SolrInputDocument();
String path = getPath();
inputDocument.addField(configuration.getPathField(), path);
inputDocument.addField(configuration.getPathDepthField(), PathUtils.getDepth(path));
if (configuration.collapseJcrContentNodes()) {
int jcrContentIndex = path.lastIndexOf(JcrConstants.JCR_CONTENT);
if (jcrContentIndex >= 0) {
int index = jcrContentIndex + JcrConstants.JCR_CONTENT.length();
String collapsedPath = path.substring(0, index);
inputDocument.addField(configuration.getCollapsedPathField(), collapsedPath);
}
}
for (PropertyState property : state.getProperties()) {
if ((configuration.getUsedProperties().size() > 0 && configuration.getUsedProperties().contains(property.getName())) || !configuration.getIgnoredProperties().contains(property.getName())) {
// try to get the field to use for this property from configuration
String fieldName = configuration.getFieldNameFor(property.getType());
Object fieldValue;
if (fieldName != null) {
fieldValue = property.getValue(property.getType());
} else {
fieldName = property.getName();
if (Type.BINARY.tag() == property.getType().tag()) {
fieldValue = extractTextValues(property, state);
} else if (property.isArray()) {
fieldValue = property.getValue(Type.STRINGS);
} else {
fieldValue = property.getValue(Type.STRING);
}
}
// add property field
inputDocument.addField(fieldName, fieldValue);
Object sortValue;
if (fieldValue instanceof Iterable) {
Iterable values = (Iterable) fieldValue;
StringBuilder builder = new StringBuilder();
String stringValue = null;
for (Object value : values) {
builder.append(value);
if (builder.length() > 1024) {
stringValue = builder.substring(0, 1024);
break;
}
}
if (stringValue == null) {
stringValue = builder.toString();
}
sortValue = stringValue;
} else {
if (fieldValue.toString().length() > 1024) {
sortValue = fieldValue.toString().substring(0, 1024);
} else {
sortValue = fieldValue;
}
}
// add sort field
inputDocument.addField(getSortingField(property.getType().tag(), property.getName()), sortValue);
}
}
return inputDocument;
}
use of org.apache.solr.common.SolrInputDocument in project lucene-solr by apache.
the class DistribJoinFromCollectionTest method indexDoc.
protected static Integer indexDoc(String collection, int id, String joinField, String matchField, String getField) throws Exception {
UpdateRequest up = new UpdateRequest();
up.setCommitWithin(50);
up.setParam("collection", collection);
SolrInputDocument doc = new SolrInputDocument();
Integer docId = new Integer(id);
doc.addField("id", docId);
doc.addField("join_s", joinField);
if (matchField != null)
doc.addField("match_s", matchField);
if (getField != null)
doc.addField("get_s", getField);
up.add(doc);
cluster.getSolrClient().request(up);
return docId;
}
Aggregations