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;
}
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;
}
}
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
}
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;
}
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;
}
Aggregations