Search in sources :

Example 11 with SolrInputDocument

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);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IndexEntry(com.thinkaurelius.titan.diskstorage.indexing.IndexEntry) TemporaryBackendException(com.thinkaurelius.titan.diskstorage.TemporaryBackendException) SolrServerException(org.apache.solr.client.solrj.SolrServerException) PermanentBackendException(com.thinkaurelius.titan.diskstorage.PermanentBackendException) BackendException(com.thinkaurelius.titan.diskstorage.BackendException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) SolrInputDocument(org.apache.solr.common.SolrInputDocument) IndexMutation(com.thinkaurelius.titan.diskstorage.indexing.IndexMutation) Map(java.util.Map) HashMap(java.util.HashMap)

Example 12 with SolrInputDocument

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);
    }
}
Also used : ArrayList(java.util.ArrayList) IndexEntry(com.thinkaurelius.titan.diskstorage.indexing.IndexEntry) TemporaryBackendException(com.thinkaurelius.titan.diskstorage.TemporaryBackendException) SolrServerException(org.apache.solr.client.solrj.SolrServerException) PermanentBackendException(com.thinkaurelius.titan.diskstorage.PermanentBackendException) BackendException(com.thinkaurelius.titan.diskstorage.BackendException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) SolrInputDocument(org.apache.solr.common.SolrInputDocument) TemporaryBackendException(com.thinkaurelius.titan.diskstorage.TemporaryBackendException) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap)

Example 13 with SolrInputDocument

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);
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) HashMap(java.util.HashMap) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) IndexEntry(com.thinkaurelius.titan.diskstorage.indexing.IndexEntry)

Example 14 with SolrInputDocument

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;
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) PropertyState(org.apache.jackrabbit.oak.api.PropertyState)

Example 15 with SolrInputDocument

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;
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest)

Aggregations

SolrInputDocument (org.apache.solr.common.SolrInputDocument)511 Test (org.junit.Test)166 ArrayList (java.util.ArrayList)96 UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)75 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)68 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)63 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)55 SolrQuery (org.apache.solr.client.solrj.SolrQuery)53 SolrException (org.apache.solr.common.SolrException)47 IOException (java.io.IOException)43 IndexSchema (org.apache.solr.schema.IndexSchema)41 AddUpdateCommand (org.apache.solr.update.AddUpdateCommand)41 HashMap (java.util.HashMap)34 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)34 List (java.util.List)31 SolrDocument (org.apache.solr.common.SolrDocument)31 NamedList (org.apache.solr.common.util.NamedList)31 CloudSolrClient (org.apache.solr.client.solrj.impl.CloudSolrClient)29 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)29 SolrServerException (org.apache.solr.client.solrj.SolrServerException)28