Search in sources :

Example 16 with SolrInputDocument

use of org.apache.solr.common.SolrInputDocument in project lucene-solr by apache.

the class LanguageIdentifierUpdateProcessorFactoryTestCase method tooShortDoc.

protected SolrInputDocument tooShortDoc() {
    SolrInputDocument doc = new SolrInputDocument();
    doc.addField("text", "This text is too short");
    return doc;
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument)

Example 17 with SolrInputDocument

use of org.apache.solr.common.SolrInputDocument in project lucene-solr by apache.

the class TestLTROnSolrCloud method indexDocument.

void indexDocument(String collection, String id, String title, String description, int popularity) throws Exception {
    SolrInputDocument doc = new SolrInputDocument();
    doc.setField("id", id);
    doc.setField("title", title);
    doc.setField("description", description);
    doc.setField("popularity", popularity);
    solrCluster.getSolrClient().add(collection, doc);
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument)

Example 18 with SolrInputDocument

use of org.apache.solr.common.SolrInputDocument in project lucene-solr by apache.

the class DocumentBuilder method toDocument.

/**
   * Convert a SolrInputDocument to a lucene Document.
   * 
   * This function should go elsewhere.  This builds the Document without an
   * extra Map<> checking for multiple values.  For more discussion, see:
   * http://www.nabble.com/Re%3A-svn-commit%3A-r547493---in--lucene-solr-trunk%3A-.--src-java-org-apache-solr-common--src-java-org-apache-solr-schema--src-java-org-apache-solr-update--src-test-org-apache-solr-common--tf3931539.html
   * 
   * TODO: /!\ NOTE /!\ This semantics of this function are still in flux.  
   * Something somewhere needs to be able to fill up a SolrDocument from
   * a lucene document - this is one place that may happen.  It may also be
   * moved to an independent function
   * 
   * @since solr 1.3
   * 
   * @param doc SolrInputDocument from which the document has to be built
   * @param schema Schema instance
   * @param forInPlaceUpdate Whether the output document would be used for an in-place update or not. When this is true,
   *        default fields values and copy fields targets are not populated.
   * @return Built Lucene document

   */
public static Document toDocument(SolrInputDocument doc, IndexSchema schema, boolean forInPlaceUpdate) {
    final SchemaField uniqueKeyField = schema.getUniqueKeyField();
    final String uniqueKeyFieldName = null == uniqueKeyField ? null : uniqueKeyField.getName();
    Document out = new Document();
    Set<String> usedFields = Sets.newHashSet();
    // Load fields from SolrDocument to Document
    for (SolrInputField field : doc) {
        String name = field.getName();
        SchemaField sfield = schema.getFieldOrNull(name);
        boolean used = false;
        // Make sure it has the correct number
        if (sfield != null && !sfield.multiValued() && field.getValueCount() > 1) {
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "ERROR: " + getID(doc, schema) + "multiple values encountered for non multiValued field " + sfield.getName() + ": " + field.getValue());
        }
        List<CopyField> copyFields = schema.getCopyFieldsList(name);
        if (copyFields.size() == 0)
            copyFields = null;
        // load each field value
        boolean hasField = false;
        try {
            for (Object v : field) {
                if (v == null) {
                    continue;
                }
                hasField = true;
                if (sfield != null) {
                    used = true;
                    addField(out, sfield, v, name.equals(uniqueKeyFieldName) ? false : forInPlaceUpdate);
                    // record the field as having a value
                    usedFields.add(sfield.getName());
                }
                // This could happen whether it is explicit or not.
                if (copyFields != null) {
                    // and this is the uniqueKey field (because the uniqueKey can't change so no need to "update" the copyField).
                    if (!(forInPlaceUpdate && name.equals(uniqueKeyFieldName))) {
                        for (CopyField cf : copyFields) {
                            SchemaField destinationField = cf.getDestination();
                            final boolean destHasValues = usedFields.contains(destinationField.getName());
                            // check if the copy field is a multivalued or not
                            if (!destinationField.multiValued() && destHasValues) {
                                throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "ERROR: " + getID(doc, schema) + "multiple values encountered for non multiValued copy field " + destinationField.getName() + ": " + v);
                            }
                            used = true;
                            // Perhaps trim the length of a copy field
                            Object val = v;
                            if (val instanceof String && cf.getMaxChars() > 0) {
                                val = cf.getLimitedValue((String) val);
                            }
                            addField(out, destinationField, val, destinationField.getName().equals(uniqueKeyFieldName) ? false : forInPlaceUpdate);
                            // record the field as having a value
                            usedFields.add(destinationField.getName());
                        }
                    }
                }
            }
        } catch (SolrException ex) {
            throw ex;
        } catch (Exception ex) {
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "ERROR: " + getID(doc, schema) + "Error adding field '" + field.getName() + "'='" + field.getValue() + "' msg=" + ex.getMessage(), ex);
        }
        // make sure the field was used somehow...
        if (!used && hasField) {
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "ERROR: " + getID(doc, schema) + "unknown field '" + name + "'");
        }
    }
    // during the full indexing initially.
    if (!forInPlaceUpdate) {
        for (SchemaField field : schema.getRequiredFields()) {
            if (out.getField(field.getName()) == null) {
                if (field.getDefaultValue() != null) {
                    addField(out, field, field.getDefaultValue(), false);
                } else {
                    String msg = getID(doc, schema) + "missing required field: " + field.getName();
                    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, msg);
                }
            }
        }
    }
    if (!forInPlaceUpdate) {
        moveLargestFieldLast(out);
    }
    return out;
}
Also used : SchemaField(org.apache.solr.schema.SchemaField) SolrInputField(org.apache.solr.common.SolrInputField) Document(org.apache.lucene.document.Document) SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrException(org.apache.solr.common.SolrException) CopyField(org.apache.solr.schema.CopyField) SolrException(org.apache.solr.common.SolrException)

Example 19 with SolrInputDocument

use of org.apache.solr.common.SolrInputDocument in project lucene-solr by apache.

the class AddUpdateCommand method iterator.

@Override
public Iterator<Document> iterator() {
    return new Iterator<Document>() {

        Iterator<SolrInputDocument> iter;

        {
            List<SolrInputDocument> all = flatten(solrDoc);
            String idField = getHashableId();
            boolean isVersion = version != 0;
            for (SolrInputDocument sdoc : all) {
                // should this be a string or the same type as the ID?
                sdoc.setField("_root_", idField);
                if (isVersion)
                    sdoc.setField(CommonParams.VERSION_FIELD, version);
            // TODO: if possible concurrent modification exception (if SolrInputDocument not cloned and is being forwarded to replicas)
            // then we could add this field to the generated lucene document instead.
            }
            iter = all.iterator();
        }

        @Override
        public boolean hasNext() {
            return iter.hasNext();
        }

        @Override
        public Document next() {
            return DocumentBuilder.toDocument(iter.next(), req.getSchema());
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) Iterator(java.util.Iterator)

Example 20 with SolrInputDocument

use of org.apache.solr.common.SolrInputDocument in project lucene-solr by apache.

the class CdcrUpdateLog method copyBufferedUpdates.

/**
   * <p>
   *   Read the entries from the given tlog file and replay them as buffered updates.
   *   The buffered tlog that we are trying to copy might contain duplicate operations with the
   *   current update log. During the tlog replication process, the replica might buffer update operations
   *   that will be present also in the tlog files downloaded from the leader. In order to remove these
   *   duplicates, it will skip any operations with a version inferior to the latest know version.
   */
private void copyBufferedUpdates(File tlogSrc, long offsetSrc, long latestVersion) {
    recoveryInfo = new RecoveryInfo();
    recoveryInfo.positionOfStart = tlog == null ? 0 : tlog.snapshot();
    state = State.BUFFERING;
    operationFlags |= FLAG_GAP;
    ModifiableSolrParams params = new ModifiableSolrParams();
    params.set(DistributingUpdateProcessorFactory.DISTRIB_UPDATE_PARAM, DistributedUpdateProcessor.DistribPhase.FROMLEADER.toString());
    SolrQueryRequest req = new LocalSolrQueryRequest(uhandler.core, params);
    CdcrTransactionLog src = new CdcrTransactionLog(tlogSrc, null, true);
    TransactionLog.LogReader tlogReader = src.getReader(offsetSrc);
    try {
        int operationAndFlags = 0;
        for (; ; ) {
            Object o = tlogReader.next();
            // we reached the end of the tlog
            if (o == null)
                break;
            // should currently be a List<Oper,Ver,Doc/Id>
            List entry = (List) o;
            operationAndFlags = (Integer) entry.get(0);
            int oper = operationAndFlags & OPERATION_MASK;
            long version = (Long) entry.get(1);
            if (version <= latestVersion) {
                // probably a buffered update that is also present in a tlog file coming from the leader,
                // skip it.
                log.debug("Dropping buffered operation - version {} < {}", version, latestVersion);
                continue;
            }
            switch(oper) {
                case UpdateLog.ADD:
                    {
                        SolrInputDocument sdoc = (SolrInputDocument) entry.get(entry.size() - 1);
                        AddUpdateCommand cmd = new AddUpdateCommand(req);
                        cmd.solrDoc = sdoc;
                        cmd.setVersion(version);
                        cmd.setFlags(UpdateCommand.BUFFERING);
                        this.add(cmd);
                        break;
                    }
                case UpdateLog.DELETE:
                    {
                        byte[] idBytes = (byte[]) entry.get(2);
                        DeleteUpdateCommand cmd = new DeleteUpdateCommand(req);
                        cmd.setIndexedId(new BytesRef(idBytes));
                        cmd.setVersion(version);
                        cmd.setFlags(UpdateCommand.BUFFERING);
                        this.delete(cmd);
                        break;
                    }
                case UpdateLog.DELETE_BY_QUERY:
                    {
                        String query = (String) entry.get(2);
                        DeleteUpdateCommand cmd = new DeleteUpdateCommand(req);
                        cmd.query = query;
                        cmd.setVersion(version);
                        cmd.setFlags(UpdateCommand.BUFFERING);
                        this.deleteByQuery(cmd);
                        break;
                    }
                default:
                    throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Invalid Operation! " + oper);
            }
        }
    } catch (Exception e) {
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unable to copy buffered updates", e);
    } finally {
        try {
            tlogReader.close();
        } finally {
            this.doClose(src);
        }
    }
}
Also used : ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) IOException(java.io.IOException) SolrException(org.apache.solr.common.SolrException) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrInputDocument(org.apache.solr.common.SolrInputDocument) ArrayList(java.util.ArrayList) List(java.util.List) BytesRef(org.apache.lucene.util.BytesRef) SolrException(org.apache.solr.common.SolrException)

Aggregations

SolrInputDocument (org.apache.solr.common.SolrInputDocument)520 Test (org.junit.Test)166 ArrayList (java.util.ArrayList)98 UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)76 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)69 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)63 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)55 SolrQuery (org.apache.solr.client.solrj.SolrQuery)54 IOException (java.io.IOException)47 SolrException (org.apache.solr.common.SolrException)47 IndexSchema (org.apache.solr.schema.IndexSchema)41 AddUpdateCommand (org.apache.solr.update.AddUpdateCommand)41 HashMap (java.util.HashMap)39 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)34 List (java.util.List)33 SolrServerException (org.apache.solr.client.solrj.SolrServerException)32 SolrDocument (org.apache.solr.common.SolrDocument)31 NamedList (org.apache.solr.common.util.NamedList)31 Map (java.util.Map)30 CloudSolrClient (org.apache.solr.client.solrj.impl.CloudSolrClient)29