Search in sources :

Example 51 with FieldValue

use of com.yahoo.document.datatypes.FieldValue in project vespa by vespa-engine.

the class FieldUpdate method applyTo.

/**
 * Applies this field update.
 *
 * @param doc the document to apply the update to
 * @return a reference to itself
 */
public FieldUpdate applyTo(Document doc) {
    for (ValueUpdate vupd : valueUpdates) {
        DataType dataType = field.getDataType();
        FieldValue oldValue = doc.getFieldValue(field);
        boolean existed = (oldValue != null);
        if (!existed) {
            oldValue = dataType.createFieldValue();
        }
        FieldValue newValue = vupd.applyTo(oldValue);
        if (newValue == null) {
            if (existed) {
                doc.removeFieldValue(field);
            }
        } else {
            doc.setFieldValue(field, newValue);
        }
    }
    return this;
}
Also used : DataType(com.yahoo.document.DataType) FieldValue(com.yahoo.document.datatypes.FieldValue)

Example 52 with FieldValue

use of com.yahoo.document.datatypes.FieldValue in project vespa by vespa-engine.

the class SingleValueReader method readSingleValue.

public static FieldValue readSingleValue(TokenBuffer buffer, DataType expectedType) {
    if (buffer.currentToken().isScalarValue()) {
        return readAtomic(buffer.currentText(), expectedType);
    } else {
        FieldValue fieldValue = expectedType.createFieldValue();
        CompositeReader.populateComposite(buffer, fieldValue);
        return fieldValue;
    }
}
Also used : FieldValue(com.yahoo.document.datatypes.FieldValue)

Example 53 with FieldValue

use of com.yahoo.document.datatypes.FieldValue in project vespa by vespa-engine.

the class GetSearcher method handleFieldFiltering.

private void handleFieldFiltering(GetResponse response, Result result, String fieldName, String contentType, boolean headersOnly) {
    if (response.getDocumentHits().isEmpty()) {
        result.hits().addError(ErrorMessage.createNotFound("Document not found, could not return field '" + fieldName + "'"));
        return;
    }
    if (result.hits().getErrorHit() == null) {
        Document doc = response.getDocumentHits().get(0).getDocument();
        Field field = doc.getDataType().getField(fieldName);
        boolean wrapXml = false;
        if (field == null) {
            result.hits().addError(ErrorMessage.createIllegalQuery("Field '" + fieldName + "' not found in document type"));
            return;
        }
        FieldValue value = doc.getFieldValue(field);
        // content will be null. We treat this as an error.
        if (value == null) {
            if (!field.isHeader() && headersOnly) {
                // TODO(vekterli): make this work with field sets as well.
                result.hits().addError(ErrorMessage.createInvalidQueryParameter("Field '" + fieldName + "' is located in document body, but headersonly " + "prevents it from being retrieved in " + doc.getId().toString()));
            } else {
                result.hits().addError(ErrorMessage.createNotFound("Field '" + fieldName + "' found in document type, but had " + "no content in " + doc.getId().toString()));
            }
            return;
        }
        String encoding = null;
        if (field.getDataType() == DataType.RAW) {
            if (contentType == null) {
                contentType = "application/octet-stream";
            }
            encoding = "ISO-8859-1";
        } else {
            // By default, return field wrapped in a blanket of vespa XML
            contentType = "text/xml";
            wrapXml = true;
        }
        if (encoding == null) {
            // Encoding doesn't matter for binary content, since we're always
            // writing directly to the byte buffer and not through a charset
            // encoder. Presumably, the client is intelligent enough to not
            // attempt to UTF-8 decode binary data.
            encoding = "UTF-8";
        }
        // Add hit now that we know there aren't any field errors. Otherwise,
        // there would be both an error hit and a document hit in the result
        response.addHitsToResult(result, false);
        // Override Vespa XML template
        result.getTemplating().setTemplates(new DocumentFieldTemplate(field, contentType, encoding, wrapXml));
    }
// else: return with error hit, invoking regular Vespa XML error template
}
Also used : Field(com.yahoo.document.Field) FieldValue(com.yahoo.document.datatypes.FieldValue) Document(com.yahoo.document.Document)

Example 54 with FieldValue

use of com.yahoo.document.datatypes.FieldValue in project vespa by vespa-engine.

the class SimpleDocument method wrapValue.

private static FieldValue wrapValue(DataType type, Object val) {
    if (val == null) {
        return null;
    }
    if (val instanceof FieldValue) {
        return (FieldValue) val;
    }
    FieldValue ret = type.createFieldValue();
    ret.assign(val);
    return ret;
}
Also used : StructuredFieldValue(com.yahoo.document.datatypes.StructuredFieldValue) FieldValue(com.yahoo.document.datatypes.FieldValue)

Example 55 with FieldValue

use of com.yahoo.document.datatypes.FieldValue in project vespa by vespa-engine.

the class ExtendedField method setFieldValue.

public FieldValue setFieldValue(StructuredFieldValue doc, FieldValue fv) {
    FieldValue old = getFieldValue(doc);
    extract.set(doc, (fv == null) ? null : fv.getWrappedValue());
    return old;
}
Also used : StructuredFieldValue(com.yahoo.document.datatypes.StructuredFieldValue) FieldValue(com.yahoo.document.datatypes.FieldValue)

Aggregations

FieldValue (com.yahoo.document.datatypes.FieldValue)106 StringFieldValue (com.yahoo.document.datatypes.StringFieldValue)69 Test (org.junit.Test)52 IntegerFieldValue (com.yahoo.document.datatypes.IntegerFieldValue)45 SimpleTestAdapter (com.yahoo.vespa.indexinglanguage.SimpleTestAdapter)30 MapFieldValue (com.yahoo.document.datatypes.MapFieldValue)26 Array (com.yahoo.document.datatypes.Array)20 LongFieldValue (com.yahoo.document.datatypes.LongFieldValue)20 TensorFieldValue (com.yahoo.document.datatypes.TensorFieldValue)18 StructuredFieldValue (com.yahoo.document.datatypes.StructuredFieldValue)14 Struct (com.yahoo.document.datatypes.Struct)13 DataType (com.yahoo.document.DataType)12 Document (com.yahoo.document.Document)12 CollectionFieldValue (com.yahoo.document.datatypes.CollectionFieldValue)12 DoubleFieldValue (com.yahoo.document.datatypes.DoubleFieldValue)12 ByteFieldValue (com.yahoo.document.datatypes.ByteFieldValue)11 FloatFieldValue (com.yahoo.document.datatypes.FloatFieldValue)11 ByteArrayInputStream (java.io.ByteArrayInputStream)11 InputStream (java.io.InputStream)11 DocumentPut (com.yahoo.document.DocumentPut)10