Search in sources :

Example 11 with Document

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));
}
Also used : DocumentProcessor(com.yahoo.docproc.DocumentProcessor) HashMap(java.util.HashMap) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) DocumentPut(com.yahoo.document.DocumentPut) Document(com.yahoo.document.Document) Processing(com.yahoo.docproc.Processing)

Example 12 with Document

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"));
}
Also used : HashMap(java.util.HashMap) TestDocumentProcessor1(com.yahoo.docproc.DocumentProcessingAbstractTestCase.TestDocumentProcessor1) DocumentId(com.yahoo.document.DocumentId) DocumentType(com.yahoo.document.DocumentType) Document(com.yahoo.document.Document) Struct(com.yahoo.document.datatypes.Struct) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) StructDataType(com.yahoo.document.StructDataType)

Example 13 with Document

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"));
}
Also used : DocumentProcessor(com.yahoo.docproc.DocumentProcessor) HashMap(java.util.HashMap) DocumentPut(com.yahoo.document.DocumentPut) Document(com.yahoo.document.Document) Processing(com.yahoo.docproc.Processing) Struct(com.yahoo.document.datatypes.Struct) Array(com.yahoo.document.datatypes.Array) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue)

Example 14 with Document

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);
}
Also used : HashMap(java.util.HashMap) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) TestDocumentProcessor1(com.yahoo.docproc.DocumentProcessingAbstractTestCase.TestDocumentProcessor1) Document(com.yahoo.document.Document) SpanTree(com.yahoo.document.annotation.SpanTree)

Example 15 with Document

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");
}
Also used : HashMap(java.util.HashMap) DocumentId(com.yahoo.document.DocumentId) DocumentType(com.yahoo.document.DocumentType) Document(com.yahoo.document.Document) DocumentUpdate(com.yahoo.document.DocumentUpdate) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) FieldUpdate(com.yahoo.document.update.FieldUpdate)

Aggregations

Document (com.yahoo.document.Document)109 Test (org.junit.Test)63 DocumentType (com.yahoo.document.DocumentType)41 DocumentPut (com.yahoo.document.DocumentPut)40 StringFieldValue (com.yahoo.document.datatypes.StringFieldValue)33 ByteArrayInputStream (java.io.ByteArrayInputStream)23 InputStream (java.io.InputStream)18 DocumentId (com.yahoo.document.DocumentId)16 DocumentParseInfo (com.yahoo.document.json.readers.DocumentParseInfo)16 VespaJsonDocumentReader (com.yahoo.document.json.readers.VespaJsonDocumentReader)16 IntegerFieldValue (com.yahoo.document.datatypes.IntegerFieldValue)13 TensorFieldValue (com.yahoo.document.datatypes.TensorFieldValue)13 HashMap (java.util.HashMap)13 DocumentTypeManager (com.yahoo.document.DocumentTypeManager)12 FieldValue (com.yahoo.document.datatypes.FieldValue)12 MapFieldValue (com.yahoo.document.datatypes.MapFieldValue)12 DocumentOperation (com.yahoo.document.DocumentOperation)10 GrowableByteBuffer (com.yahoo.io.GrowableByteBuffer)10 DocumentUpdate (com.yahoo.document.DocumentUpdate)8 Array (com.yahoo.document.datatypes.Array)8