use of com.yahoo.document.datatypes.Struct in project vespa by vespa-engine.
the class DocumentToPathUpdateTestCase method requireThatStructAssignIsConverted.
@Test
public void requireThatStructAssignIsConverted() {
DocumentType docType = new DocumentType("my_type");
StructDataType structType = new StructDataType("my_struct");
structType.addField(new Field("b", DataType.INT));
docType.addField(new Field("a", structType));
Struct struct = structType.createFieldValue();
struct.setFieldValue("b", new IntegerFieldValue(69));
FieldPathUpdate upd = new AssignFieldPathUpdate(docType, "a", "", struct);
Document doc = FieldPathUpdateHelper.newPartialDocument(null, upd);
assertNotNull(doc);
FieldValue obj = doc.getFieldValue("a");
assertTrue(obj instanceof Struct);
struct = (Struct) obj;
struct.setFieldValue("b", new IntegerFieldValue(96));
DocumentUpdate docUpd = new FieldPathUpdateAdapter(new SimpleDocumentAdapter(null, doc), upd).getOutput();
assertNotNull(docUpd);
assertEquals(1, docUpd.getFieldPathUpdates().size());
assertNotNull(upd = docUpd.getFieldPathUpdates().get(0));
assertTrue(upd instanceof AssignFieldPathUpdate);
assertEquals("a", upd.getOriginalFieldPath());
assertEquals(struct, ((AssignFieldPathUpdate) upd).getNewValue());
}
use of com.yahoo.document.datatypes.Struct in project vespa by vespa-engine.
the class DocumentToPathUpdateTestCase method requireThatStructElementAssignIsConverted.
@Test
public void requireThatStructElementAssignIsConverted() {
DocumentType docType = new DocumentType("my_type");
StructDataType structType = new StructDataType("my_struct");
structType.addField(new Field("b", DataType.INT));
docType.addField(new Field("a", structType));
FieldPathUpdate upd = new AssignFieldPathUpdate(docType, "a.b", "", new IntegerFieldValue(69));
Document doc = FieldPathUpdateHelper.newPartialDocument(null, upd);
assertNotNull(doc);
FieldValue obj = doc.getFieldValue("a");
assertTrue(obj instanceof Struct);
Struct struct = (Struct) obj;
struct.setFieldValue("b", new IntegerFieldValue(96));
DocumentUpdate docUpd = new FieldPathUpdateAdapter(new SimpleDocumentAdapter(null, doc), upd).getOutput();
assertNotNull(docUpd);
assertEquals(1, docUpd.getFieldPathUpdates().size());
assertNotNull(upd = docUpd.getFieldPathUpdates().get(0));
assertTrue(upd instanceof AssignFieldPathUpdate);
assertEquals("a.b", upd.getOriginalFieldPath());
obj = ((AssignFieldPathUpdate) upd).getNewValue();
assertTrue(obj instanceof IntegerFieldValue);
assertEquals(96, ((IntegerFieldValue) obj).getInteger());
}
use of com.yahoo.document.datatypes.Struct in project vespa by vespa-engine.
the class DocumentUpdateTestCase method requireThatArrayOfStructElementAddIsProcessedCorrectly.
@Test
public void requireThatArrayOfStructElementAddIsProcessedCorrectly() throws ParseException {
DocumentType docType = new DocumentType("my_input");
docType.addField(new Field("my_str", DataType.getArray(DataType.STRING)));
docType.addField(new Field("my_pos", DataType.getArray(PositionDataType.INSTANCE)));
DocumentUpdate docUpdate = new DocumentUpdate(docType, "doc:scheme:");
docUpdate.addFieldUpdate(FieldUpdate.createAdd(docType.getField("my_str"), new StringFieldValue("6;9")));
docUpdate = Expression.execute(Expression.fromString("input my_str | for_each { to_pos } | index my_pos"), docUpdate);
assertNotNull(docUpdate);
assertEquals(0, docUpdate.getFieldPathUpdates().size());
assertEquals(1, docUpdate.getFieldUpdates().size());
FieldUpdate fieldUpd = docUpdate.getFieldUpdate(0);
assertNotNull(fieldUpd);
assertEquals(docType.getField("my_pos"), fieldUpd.getField());
assertEquals(1, fieldUpd.getValueUpdates().size());
ValueUpdate valueUpd = fieldUpd.getValueUpdate(0);
assertNotNull(valueUpd);
assertTrue(valueUpd instanceof AddValueUpdate);
Object val = valueUpd.getValue();
assertNotNull(val);
assertTrue(val instanceof Struct);
Struct pos = (Struct) val;
assertEquals(PositionDataType.INSTANCE, pos.getDataType());
assertEquals(new IntegerFieldValue(6), PositionDataType.getXValue(pos));
assertEquals(new IntegerFieldValue(9), PositionDataType.getYValue(pos));
}
use of com.yahoo.document.datatypes.Struct in project vespa by vespa-engine.
the class SimpleDocumentAdapterTestCase method requireThatStructFieldsCanBeAccessed.
@Test
public void requireThatStructFieldsCanBeAccessed() {
DataType barType = DataType.STRING;
FieldValue bar = barType.createFieldValue("bar");
StructDataType fooType = new StructDataType("my_struct");
fooType.addField(new Field("bar", barType));
Struct foo = new Struct(fooType);
foo.setFieldValue("bar", bar);
DocumentType docType = new DocumentType("my_doc");
docType.addField("foo", fooType);
Document doc = new Document(docType, "doc:scheme:");
doc.setFieldValue("foo", foo);
DocumentAdapter adapter = new SimpleDocumentAdapter(doc);
assertEquals(fooType, adapter.getInputType(null, "foo"));
assertEquals(foo, adapter.getInputValue("foo"));
assertEquals(barType, adapter.getInputType(null, "foo.bar"));
assertEquals(bar, adapter.getInputValue("foo.bar"));
}
use of com.yahoo.document.datatypes.Struct in project vespa by vespa-engine.
the class SchemaMappingAndAccessesTest method getDoc.
private Document getDoc() {
DocumentType type = new DocumentType("album");
AnnotationType personType = new AnnotationType("person");
Annotation person = new Annotation(personType);
type.addField("title", DataType.STRING);
type.addField("artist", DataType.STRING);
type.addField("guitarist", DataType.STRING);
type.addField("year", DataType.INT);
type.addField("labels", DataType.getArray(DataType.STRING));
Document doc = new Document(type, new DocumentId("doc:map:test:1"));
doc.setFieldValue("title", new StringFieldValue("Black Rock"));
StringFieldValue joe = new StringFieldValue("Joe Bonamassa");
joe.setSpanTree(new SpanTree("mytree").annotate(person));
doc.setFieldValue("artist", joe);
doc.setFieldValue("year", new IntegerFieldValue(2010));
Array<StringFieldValue> labels = new Array<>(type.getField("labels").getDataType());
labels.add(new StringFieldValue("audun"));
labels.add(new StringFieldValue("tylden"));
doc.setFieldValue("labels", labels);
StructDataType personStructType = new StructDataType("artist");
personStructType.addField(new com.yahoo.document.Field("firstname", DataType.STRING));
personStructType.addField(new com.yahoo.document.Field("lastname", DataType.STRING));
type.addField("listeners", DataType.getArray(personStructType));
Array<Struct> listeners = new Array<>(type.getField("listeners").getDataType());
Struct listenerOne = new Struct(personStructType);
listenerOne.setFieldValue("firstname", new StringFieldValue("per"));
listenerOne.setFieldValue("lastname", new StringFieldValue("olsen"));
Struct listenerTwo = new Struct(personStructType);
listenerTwo.setFieldValue("firstname", new StringFieldValue("anders"));
listenerTwo.setFieldValue("lastname", new StringFieldValue("and"));
listeners.add(listenerOne);
listeners.add(listenerTwo);
doc.setFieldValue("listeners", listeners);
return doc;
}
Aggregations