use of com.yahoo.document.Document in project vespa by vespa-engine.
the class SchemaMappingAndAccessesTest method testMappingArrays.
public void testMappingArrays() {
Document doc = getDoc();
DocumentProcessor proc = new TestMappingArrayProcessor();
Map<String, String> fieldMap = new HashMap<>();
fieldMap.put("label", "labels[0]");
ProxyDocument mapped = new ProxyDocument(proc, doc, fieldMap);
Processing p = Processing.of(new DocumentPut(mapped));
proc.process(p);
assertEquals(2, ((Array<StringFieldValue>) doc.getFieldValue("labels")).size());
assertEquals(new StringFieldValue("EMI"), ((Array<StringFieldValue>) doc.getFieldValue("labels")).get(0));
assertEquals(new StringFieldValue("tylden"), ((Array<StringFieldValue>) doc.getFieldValue("labels")).get(1));
fieldMap.clear();
fieldMap.put("label", "labels[2]");
mapped = new ProxyDocument(proc, doc, fieldMap);
p = Processing.of(new DocumentPut(mapped));
try {
proc.process(p);
fail("Should not have worked");
} catch (IllegalArgumentException iae) {
// ok!
}
assertEquals(2, ((Array<StringFieldValue>) doc.getFieldValue("labels")).size());
assertEquals(new StringFieldValue("EMI"), ((Array<StringFieldValue>) doc.getFieldValue("labels")).get(0));
assertEquals(new StringFieldValue("tylden"), ((Array<StringFieldValue>) doc.getFieldValue("labels")).get(1));
}
use of com.yahoo.document.Document in project vespa by vespa-engine.
the class SchemaMappingAndAccessesTest method testMappedDocStruct.
public void testMappedDocStruct() {
StructDataType materialsStructType = new StructDataType("materialstype");
materialsStructType.addField(new com.yahoo.document.Field("ceiling", DataType.STRING));
materialsStructType.addField(new com.yahoo.document.Field("walls", DataType.STRING));
DocumentType docType = new DocumentType("album");
docType.addField("title", DataType.STRING);
docType.addField("artist", DataType.STRING);
StructDataType storeStructType = new StructDataType("storetype");
storeStructType.addField(new com.yahoo.document.Field("name", DataType.STRING));
storeStructType.addField(new com.yahoo.document.Field("city", DataType.STRING));
storeStructType.addField(new com.yahoo.document.Field("materials", materialsStructType));
docType.addField("store", storeStructType);
Document doc = new Document(docType, new DocumentId("doc:map:test:1"));
doc.setFieldValue("title", new StringFieldValue("Black Rock"));
doc.setFieldValue("artist", new StringFieldValue("Joe Bonamassa"));
Struct material = new Struct(materialsStructType);
material.setFieldValue("ceiling", new StringFieldValue("wood"));
material.setFieldValue("walls", new StringFieldValue("brick"));
Struct store = new Struct(storeStructType);
store.setFieldValue("name", new StringFieldValue("Platekompaniet"));
store.setFieldValue("city", new StringFieldValue("Trondheim"));
store.setFieldValue(storeStructType.getField("materials"), material);
doc.setFieldValue(docType.getField("store"), store);
Map<String, String> fieldMap = new HashMap<>();
fieldMap.put("t", "title");
fieldMap.put("c", "store.city");
fieldMap.put("w", "store.materials.walls");
ProxyDocument mapped = new ProxyDocument(new TestDocumentProcessor1(), doc, fieldMap);
assertEquals(new StringFieldValue("Trondheim"), mapped.getFieldValue("c"));
assertEquals(new StringFieldValue("Black Rock"), mapped.getFieldValue("t"));
assertEquals(new StringFieldValue("brick"), mapped.getFieldValue("w"));
assertEquals(new StringFieldValue("brick"), material.getFieldValue("walls"));
mapped.setFieldValue("c", new StringFieldValue("Steinkjer"));
mapped.setFieldValue("w", new StringFieldValue("plaster"));
assertEquals(new StringFieldValue("plaster"), mapped.getFieldValue("w"));
assertEquals(new StringFieldValue("plaster"), material.getFieldValue("walls"));
assertEquals(new StringFieldValue("Steinkjer"), store.getFieldValue("city"));
assertEquals(new StringFieldValue("Steinkjer"), mapped.getFieldValue("c"));
assertEquals(new StringFieldValue("Steinkjer"), mapped.getFieldValue("c"));
mapped.setFieldValue("c", new StringFieldValue("Levanger"));
assertEquals(new StringFieldValue("Levanger"), store.getFieldValue("city"));
assertEquals(new StringFieldValue("Levanger"), mapped.getFieldValue("c"));
mapped.setFieldValue("c", mapped.getFieldValue("c") + "Kommune");
assertEquals(new StringFieldValue("LevangerKommune"), mapped.getFieldValue("c"));
// mapped.set(mapped.getField("c"), mapped.get("c")+"Styre");
// assertEquals(new StringFieldValue("LevangerKommuneStyre"), mapped.getFieldValue("c"));
}
use of com.yahoo.document.Document in project vespa by vespa-engine.
the class SchemaMappingAndAccessesTest method testMappingStructsInArrays.
public void testMappingStructsInArrays() {
Document doc = getDoc();
DocumentProcessor proc = new TestMappingStructInArrayProcessor();
Map<String, String> fieldMap = new HashMap<>();
fieldMap.put("name", "listeners[0].firstname");
ProxyDocument mapped = new ProxyDocument(proc, doc, fieldMap);
Processing p = Processing.of(new DocumentPut(mapped));
proc.process(p);
assertEquals(2, ((Array<Struct>) doc.getFieldValue("listeners")).size());
assertEquals("peter", (((StringFieldValue) ((Array<Struct>) doc.getFieldValue("listeners")).get(0).getFieldValue("firstname")).getString()));
assertEquals("olsen", (((StringFieldValue) ((Array<Struct>) doc.getFieldValue("listeners")).get(0).getFieldValue("lastname")).getString()));
assertEquals("anders", (((StringFieldValue) ((Array<Struct>) doc.getFieldValue("listeners")).get(1).getFieldValue("firstname")).getString()));
assertEquals("and", (((StringFieldValue) ((Array<Struct>) doc.getFieldValue("listeners")).get(1).getFieldValue("lastname")).getString()));
fieldMap.clear();
fieldMap.put("name", "listeners[2].firstname");
mapped = new ProxyDocument(proc, doc, fieldMap);
p = Processing.of(new DocumentPut(mapped));
try {
proc.process(p);
fail("Should not have worked");
} catch (IllegalArgumentException iae) {
// ok!
}
assertEquals(2, ((Array<Struct>) doc.getFieldValue("listeners")).size());
assertEquals("peter", (((StringFieldValue) ((Array<Struct>) doc.getFieldValue("listeners")).get(0).getFieldValue("firstname")).getString()));
assertEquals("olsen", (((StringFieldValue) ((Array<Struct>) doc.getFieldValue("listeners")).get(0).getFieldValue("lastname")).getString()));
assertEquals("anders", (((StringFieldValue) ((Array<Struct>) doc.getFieldValue("listeners")).get(1).getFieldValue("firstname")).getString()));
assertEquals("and", (((StringFieldValue) ((Array<Struct>) doc.getFieldValue("listeners")).get(1).getFieldValue("lastname")).getString()));
// test remove:
proc = new TestRemovingMappingStructInArrayProcessor();
fieldMap.clear();
fieldMap.put("name", "listeners[1].lastname");
mapped = new ProxyDocument(proc, doc, fieldMap);
p = Processing.of(new DocumentPut(mapped));
proc.process(p);
assertEquals(2, ((Array<Struct>) doc.getFieldValue("listeners")).size());
assertEquals("peter", (((StringFieldValue) ((Array<Struct>) doc.getFieldValue("listeners")).get(0).getFieldValue("firstname")).getString()));
assertEquals("olsen", (((StringFieldValue) ((Array<Struct>) doc.getFieldValue("listeners")).get(0).getFieldValue("lastname")).getString()));
assertEquals("anders", (((StringFieldValue) ((Array<Struct>) doc.getFieldValue("listeners")).get(1).getFieldValue("firstname")).getString()));
assertNull(((Array<Struct>) doc.getFieldValue("listeners")).get(1).getFieldValue("lastname"));
fieldMap.clear();
fieldMap.put("name", "listeners[2].lastname");
mapped = new ProxyDocument(proc, doc, fieldMap);
p = Processing.of(new DocumentPut(mapped));
try {
proc.process(p);
fail("Should not have worked");
} catch (IllegalArgumentException iae) {
// ok!
}
assertEquals(2, ((Array<Struct>) doc.getFieldValue("listeners")).size());
assertEquals("peter", (((StringFieldValue) ((Array<Struct>) doc.getFieldValue("listeners")).get(0).getFieldValue("firstname")).getString()));
assertEquals("olsen", (((StringFieldValue) ((Array<Struct>) doc.getFieldValue("listeners")).get(0).getFieldValue("lastname")).getString()));
assertEquals("anders", (((StringFieldValue) ((Array<Struct>) doc.getFieldValue("listeners")).get(1).getFieldValue("firstname")).getString()));
assertNull(((Array<Struct>) doc.getFieldValue("listeners")).get(1).getFieldValue("lastname"));
}
use of com.yahoo.document.Document in project vespa by vespa-engine.
the class SchemaMappingAndAccessesTest method testMappedDoc.
public void testMappedDoc() {
Document doc = getDoc();
Map<String, String> fieldMap = new HashMap<>();
fieldMap.put("t", "title");
fieldMap.put("a", "artist");
ProxyDocument mapped = new ProxyDocument(new TestDocumentProcessor1(), doc, fieldMap);
// Document mapped=doc;
// mapped.setFieldMap(fieldMap);
assertEquals(new StringFieldValue("Black Rock"), mapped.getFieldValue("t"));
// assertEquals(new StringFieldValue("Black Rock"), proxy.getFieldValue(new com.yahoo.document.Field("t")));
assertEquals(new StringFieldValue("Joe Bonamassa").getWrappedValue(), mapped.getFieldValue("a").getWrappedValue());
mapped.setFieldValue("t", new StringFieldValue("The Ballad Of John Henry"));
StringFieldValue bona = new StringFieldValue("Bonamassa");
mapped.setFieldValue("a", bona);
// mapped.setFieldValue("a", new StringFieldValue("Bonamassa"));
assertEquals(new StringFieldValue("The Ballad Of John Henry"), doc.getFieldValue("title"));
assertEquals(new StringFieldValue("The Ballad Of John Henry"), mapped.getFieldValue("t"));
assertEquals(new StringFieldValue("Bonamassa"), doc.getFieldValue("artist"));
assertEquals(new StringFieldValue("Bonamassa"), mapped.getFieldValue("a"));
mapped.setFieldValue("a", mapped.getFieldValue("a") + "Hughes");
assertEquals(new StringFieldValue("BonamassaHughes"), mapped.getFieldValue("a"));
// Verify consistency when using string values to manipluate annotation span trees
StringFieldValue unmapped1 = (StringFieldValue) doc.getFieldValue("artist");
StringFieldValue unmapped2 = (StringFieldValue) doc.getFieldValue("artist");
assertTrue(unmapped1 == unmapped2);
unmapped1.setSpanTree(new SpanTree("test"));
assertEquals(unmapped2.getSpanTree("test").getName(), "test");
StringFieldValue mapped1 = (StringFieldValue) mapped.getFieldValue("a");
mapped1.setSpanTree(new SpanTree("test2"));
StringFieldValue mapped2 = (StringFieldValue) mapped.getFieldValue("a");
assertTrue(mapped1 == mapped2);
assertEquals(mapped2.getSpanTree("test2").getName(), "test2");
mapped.removeFieldValue("a");
assertEquals(mapped.getFieldValue("a"), null);
mapped.removeFieldValue(mapped.getField("t"));
assertEquals(mapped.getFieldValue("t"), null);
mapped.setFieldValue("a", new StringFieldValue("Bonamassa"));
assertEquals(new StringFieldValue("Bonamassa"), doc.getFieldValue("artist"));
mapped.removeFieldValue("a");
assertEquals(mapped.getFieldValue("a"), null);
}
use of com.yahoo.document.Document in project vespa by vespa-engine.
the class SchemaMappingAndAccessesTest method testMappedDocUpdateAPI.
public void testMappedDocUpdateAPI() {
Document doc = getDoc();
DocumentType type = doc.getDataType();
DocumentUpdate dud = new DocumentUpdate(type, new DocumentId("doc:map:test:1"));
FieldUpdate assignSingle = FieldUpdate.createAssign(type.getField("title"), new StringFieldValue("something"));
Map<String, String> fieldMap = new HashMap<>();
fieldMap.put("t", "title");
fieldMap.put("a", "artist");
ProxyDocumentUpdate pup = new ProxyDocumentUpdate(dud, fieldMap);
pup.addFieldUpdate(assignSingle);
assertEquals(pup.getFieldUpdates(), dud.getFieldUpdates());
assertEquals(pup.getDocumentType(), dud.getDocumentType());
assertEquals(pup.getFieldUpdate(new com.yahoo.document.Field("title")).size(), 1);
assertEquals(pup.getFieldUpdate(0), dud.getFieldUpdate(0));
assertEquals(pup.getFieldUpdate("title"), dud.getFieldUpdate("title"));
assertEquals(pup.getId(), dud.getId());
assertEquals(pup.getType(), dud.getType());
assertEquals(pup.applyTo(doc), dud);
assertEquals(doc.getFieldValue("title").getWrappedValue(), "something");
assertEquals(pup, dud);
assertEquals(pup.hashCode(), dud.hashCode());
assertEquals(pup.toString(), dud.toString());
assertEquals(pup.size(), dud.size());
assertEquals(pup.getWrappedDocumentOperation().getId().toString(), "doc:map:test:1");
}
Aggregations