Search in sources :

Example 6 with SolrInputDocument

use of org.apache.solr.common.SolrInputDocument in project titan by thinkaurelius.

the class SolrIndex 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<String>();
            Collection<SolrInputDocument> changes = new ArrayList<SolrInputDocument>();
            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 (IllegalArgumentException e) {
        throw new PermanentBackendException("Unable to complete query on Solr.", e);
    } catch (Exception e) {
        throw storageException(e);
    }
}
Also used : KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) SolrInputDocument(org.apache.solr.common.SolrInputDocument)

Example 7 with SolrInputDocument

use of org.apache.solr.common.SolrInputDocument in project titan by thinkaurelius.

the class SolrIndex 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) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest)

Example 8 with SolrInputDocument

use of org.apache.solr.common.SolrInputDocument in project YCSB by brianfrankcooper.

the class SolrClient method insert.

/**
   * Insert a record in the database. Any field/value pairs in the specified values HashMap will be
   * written into the record with the specified record key.
   *
   * @param table
   *          The name of the table
   * @param key
   *          The record key of the record to insert.
   * @param values
   *          A HashMap of field/value pairs to insert in the record
   * @return Zero on success, a non-zero error code on error. See this class's description for a
   *         discussion of error codes.
   */
@Override
public Status insert(String table, String key, HashMap<String, ByteIterator> values) {
    try {
        SolrInputDocument doc = new SolrInputDocument();
        doc.addField("id", key);
        for (Entry<String, String> entry : StringByteIterator.getStringMap(values).entrySet()) {
            doc.addField(entry.getKey(), entry.getValue());
        }
        UpdateResponse response;
        if (batchMode) {
            response = client.add(table, doc, commitTime);
        } else {
            response = client.add(table, doc);
            client.commit(table);
        }
        return checkStatus(response.getStatus());
    } catch (IOException | SolrServerException e) {
        e.printStackTrace();
    }
    return Status.ERROR;
}
Also used : UpdateResponse(org.apache.solr.client.solrj.response.UpdateResponse) SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrServerException(org.apache.solr.client.solrj.SolrServerException) IOException(java.io.IOException)

Example 9 with SolrInputDocument

use of org.apache.solr.common.SolrInputDocument in project YCSB by brianfrankcooper.

the class SolrClient method update.

/**
   * Update a record in the database. Any field/value pairs in the specified values HashMap will be
   * written into the record with the specified record key, overwriting any existing values with the
   * same field name.
   *
   * @param table
   *          The name of the table
   * @param key
   *          The record key of the record to write.
   * @param values
   *          A HashMap of field/value pairs to update in the record
   * @return Zero on success, a non-zero error code on error. See this class's description for a
   *         discussion of error codes.
   */
@Override
public Status update(String table, String key, HashMap<String, ByteIterator> values) {
    try {
        SolrInputDocument updatedDoc = new SolrInputDocument();
        updatedDoc.addField("id", key);
        for (Entry<String, String> entry : StringByteIterator.getStringMap(values).entrySet()) {
            updatedDoc.addField(entry.getKey(), Collections.singletonMap("set", entry.getValue()));
        }
        UpdateResponse writeResponse;
        if (batchMode) {
            writeResponse = client.add(table, updatedDoc, commitTime);
        } else {
            writeResponse = client.add(table, updatedDoc);
            client.commit(table);
        }
        return checkStatus(writeResponse.getStatus());
    } catch (IOException | SolrServerException e) {
        e.printStackTrace();
    }
    return Status.ERROR;
}
Also used : UpdateResponse(org.apache.solr.client.solrj.response.UpdateResponse) SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrServerException(org.apache.solr.client.solrj.SolrServerException) IOException(java.io.IOException)

Example 10 with SolrInputDocument

use of org.apache.solr.common.SolrInputDocument in project gora by apache.

the class SolrStore method put.

@Override
public void put(K key, T persistent) {
    Schema schema = persistent.getSchema();
    if (!persistent.isDirty()) {
        // nothing to do
        return;
    }
    SolrInputDocument doc = new SolrInputDocument();
    // add primary key
    doc.addField(mapping.getPrimaryKey(), key);
    // populate the doc
    List<Field> fields = schema.getFields();
    for (Field field : fields) {
        String sf = mapping.getSolrField(field.name());
        // mapping won't find the primary
        if (sf == null) {
            continue;
        }
        Schema fieldSchema = field.schema();
        Object v = persistent.get(field.pos());
        if (v == null) {
            continue;
        }
        v = serializeFieldValue(fieldSchema, v);
        doc.addField(sf, v);
    }
    LOG.info("Putting DOCUMENT: " + doc);
    batch.add(doc);
    if (batch.size() >= batchSize) {
        try {
            add(batch, commitWithin);
            batch.clear();
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
    }
}
Also used : Field(org.apache.avro.Schema.Field) SolrInputDocument(org.apache.solr.common.SolrInputDocument) Schema(org.apache.avro.Schema) SolrServerException(org.apache.solr.client.solrj.SolrServerException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

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