Search in sources :

Example 81 with Field

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

the class StringTestCase method consume.

public Document consume(Document document, DocumentTypeManager docTypeMgr) {
    DocumentType type = docTypeMgr.getDocumentType("blog");
    Collection<Field> fc = type.getFields();
    for (Field f : fc) {
        System.out.println("\n\nField Name: " + f.getName());
        System.out.println("DataType: " + f.getDataType());
        System.out.println("isHeader? " + f.isHeader());
        FieldValue val = document.getFieldValue(f);
        if (val instanceof StringFieldValue) {
            StringFieldValue sfv = (StringFieldValue) val;
            System.out.println(f.getName() + " is a StringField. Field Value: " + sfv.getString());
            Collection<SpanTree> c = sfv.getSpanTrees();
            for (SpanTree tree : c) {
                System.out.println(f.getName() + " has annotations");
                consumeAnnotations(tree, (SpanList) tree.getRoot());
            }
        }
    }
    return document;
}
Also used : Field(com.yahoo.document.Field) DocumentType(com.yahoo.document.DocumentType) SpanTree(com.yahoo.document.annotation.SpanTree)

Example 82 with Field

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

the class SerializeAnnotationsTestCase method testSerializeAdvancedTree.

public void testSerializeAdvancedTree() throws IOException {
    SpanList root = new SpanList();
    SpanTree tree = new SpanTree("html", root);
    DataType positionType = docMan.getDataType("myposition");
    StructDataType cityDataType = (StructDataType) docMan.getDataType("annotation.city");
    AnnotationTypeRegistry registry = docMan.getAnnotationTypeRegistry();
    AnnotationType textType = registry.getType("text");
    AnnotationType beginTag = registry.getType("begintag");
    AnnotationType endTag = registry.getType("endtag");
    AnnotationType bodyType = registry.getType("body");
    AnnotationType paragraphType = registry.getType("paragraph");
    AnnotationType cityType = registry.getType("city");
    AnnotationReferenceDataType annRefType = (AnnotationReferenceDataType) docMan.getDataType("annotationreference<text>");
    Struct position = new Struct(positionType);
    position.setFieldValue("latitude", new DoubleFieldValue(37.774929));
    position.setFieldValue("longitude", new DoubleFieldValue(-122.419415));
    Annotation sanAnnotation = new Annotation(textType);
    Annotation franciscoAnnotation = new Annotation(textType);
    Struct positionWithRef = cityDataType.createFieldValue();
    positionWithRef.setFieldValue("position", position);
    Field referencesField = cityDataType.getField("references");
    Array<FieldValue> refList = new Array<FieldValue>(referencesField.getDataType());
    refList.add(new AnnotationReference(annRefType, sanAnnotation));
    refList.add(new AnnotationReference(annRefType, franciscoAnnotation));
    positionWithRef.setFieldValue(referencesField, refList);
    Annotation city = new Annotation(cityType, positionWithRef);
    AlternateSpanList paragraph = new AlternateSpanList();
    paragraph.addChildren(new ArrayList<SpanNode>(), 0);
    paragraph.setProbability(0, 0.9);
    paragraph.setProbability(1, 0.1);
    {
        Span span1 = new Span(6, 3);
        Span span2 = new Span(9, 10);
        Span span3 = new Span(19, 4);
        Span span4 = new Span(23, 4);
        paragraph.add(0, span1).add(0, span2).add(0, span3).add(0, span4);
        Span alt_span1 = new Span(6, 13);
        Span alt_span2 = new Span(19, 8);
        paragraph.add(1, alt_span1).add(1, alt_span2);
        tree.annotate(span1, beginTag).annotate(span2, textType).annotate(span3, sanAnnotation).annotate(span4, endTag).annotate(alt_span1, textType).annotate(alt_span2, bodyType).annotate(paragraph, paragraphType);
    }
    {
        Span span1 = new Span(0, 6);
        Span span2 = new Span(27, 9);
        Span span3 = new Span(36, 8);
        root.add(span1).add(paragraph).add(span2).add(span3);
        tree.annotate(span1, beginTag).annotate(span2, franciscoAnnotation).annotate(span3, endTag).annotate(root, bodyType).annotate(city);
    }
    StringFieldValue value = new StringFieldValue("lkj lkj lkj lkj lkj lkj lkj lkj lkj lkj lkj lkj " + "lkj lkj lkj lkj lkj lkj lkj lkj lkj lkj l jlkj lkj lkj " + "lkjoijoij oij oij oij oij oij oijoijoij oij oij oij oij oij " + "oijoijoijoijoijoijoijoijoijoijoijoijoij oij oij oij oij " + "oijaosdifjoai fdoais jdoasi jai os oafoai ai dfojsfoa dfoi dsf" + "aosifjasofija sodfij oasdifj aosdiosifjsi ooai oais osi");
    value.setSpanTree(tree);
    // important! call readFile() before writeFile()!
    ByteBuffer serializedFromFile = readFile("test_data_serialized_advanced");
    ByteBuffer serialized = writeFile(value, "test_data_serialized_advanced");
    assertEquals(serialized.limit(), serializedFromFile.limit());
    StringFieldValue valueFromFile = new StringFieldValue();
    DocumentDeserializer deserializer = DocumentDeserializerFactory.create42(docMan, new GrowableByteBuffer(serializedFromFile));
    deserializer.read(null, valueFromFile);
    assertEquals(value, valueFromFile);
}
Also used : DoubleFieldValue(com.yahoo.document.datatypes.DoubleFieldValue) GrowableByteBuffer(com.yahoo.io.GrowableByteBuffer) ByteBuffer(java.nio.ByteBuffer) GrowableByteBuffer(com.yahoo.io.GrowableByteBuffer) Struct(com.yahoo.document.datatypes.Struct) Array(com.yahoo.document.datatypes.Array) Field(com.yahoo.document.Field) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) StructDataType(com.yahoo.document.StructDataType) DataType(com.yahoo.document.DataType) StructDataType(com.yahoo.document.StructDataType) DoubleFieldValue(com.yahoo.document.datatypes.DoubleFieldValue) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) FieldValue(com.yahoo.document.datatypes.FieldValue)

Example 83 with Field

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

the class XmlDocumentWriterTestCase method requireThatPredicateFieldValuesAreSerializedAsString.

@Test
public void requireThatPredicateFieldValuesAreSerializedAsString() {
    DocumentType docType = new DocumentType("my_type");
    Field field = new Field("my_predicate", DataType.PREDICATE);
    docType.addField(field);
    Document doc = new Document(docType, "doc:scheme:");
    PredicateFieldValue predicate = Mockito.mock(PredicateFieldValue.class);
    doc.setFieldValue("my_predicate", predicate);
    new XmlDocumentWriter().write(doc);
    Mockito.verify(predicate, Mockito.times(1)).printXml(Mockito.any(XmlStream.class));
}
Also used : Field(com.yahoo.document.Field) DocumentType(com.yahoo.document.DocumentType) Document(com.yahoo.document.Document) PredicateFieldValue(com.yahoo.document.datatypes.PredicateFieldValue) Test(org.junit.Test)

Example 84 with Field

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

the class StructTestCase method testSetUnknownType.

@Test
public void testSetUnknownType() {
    StructDataType type = new StructDataType("teststr");
    type.addField(new Field("int", 0, DataType.INT, true));
    Struct struct = new Struct(type);
    try {
        struct.setFieldValue(new Field("alien", DataType.STRING), new StringFieldValue("foo"));
        fail("Alien type worked");
    } catch (IllegalArgumentException expected) {
        assertTrue(expected.getMessage().matches(".*No such field.*"));
    }
}
Also used : Field(com.yahoo.document.Field) StructDataType(com.yahoo.document.StructDataType) Test(org.junit.Test)

Example 85 with Field

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

the class StructTestCase method sortingOrderDependsOnTypeFieldOrderWhenNonEqual.

@Test
public void sortingOrderDependsOnTypeFieldOrderWhenNonEqual() {
    StructDataType type = new StructDataType("test");
    type.addField(new Field("int", DataType.INT));
    type.addField(new Field("intnotset", DataType.INT));
    type.addField(new Field("flt", DataType.FLOAT));
    type.addField(new Field("str", DataType.STRING));
    Struct a = new Struct(type);
    a.setFieldValue("int", new IntegerFieldValue(123));
    a.setFieldValue("flt", new DoubleFieldValue(45.6));
    Struct b = new Struct(type);
    b.setFieldValue("int", new IntegerFieldValue(123));
    b.setFieldValue("str", new StringFieldValue("hello world"));
    // a sorts before b as it has flt set which occurs before str in the type
    assertTrue(a.compareTo(b) < 0);
    assertTrue(b.compareTo(a) > 0);
    assertFalse(a.equals(b));
    assertFalse(b.equals(a));
}
Also used : Field(com.yahoo.document.Field) StructDataType(com.yahoo.document.StructDataType) Test(org.junit.Test)

Aggregations

Field (com.yahoo.document.Field)115 Test (org.junit.Test)50 StructDataType (com.yahoo.document.StructDataType)46 DocumentType (com.yahoo.document.DocumentType)24 DataType (com.yahoo.document.DataType)17 SimpleTestAdapter (com.yahoo.vespa.indexinglanguage.SimpleTestAdapter)14 ReferenceDataType (com.yahoo.document.ReferenceDataType)13 StringFieldValue (com.yahoo.document.datatypes.StringFieldValue)13 ArrayDataType (com.yahoo.document.ArrayDataType)12 MapDataType (com.yahoo.document.MapDataType)12 TensorDataType (com.yahoo.document.TensorDataType)11 WeightedSetDataType (com.yahoo.document.WeightedSetDataType)11 SDField (com.yahoo.searchdefinition.document.SDField)10 PositionDataType (com.yahoo.document.PositionDataType)9 FieldValue (com.yahoo.document.datatypes.FieldValue)9 IntegerFieldValue (com.yahoo.document.datatypes.IntegerFieldValue)9 GrowableByteBuffer (com.yahoo.io.GrowableByteBuffer)8 Struct (com.yahoo.document.datatypes.Struct)7 DocumentTypeManager (com.yahoo.document.DocumentTypeManager)6 Document (com.yahoo.document.Document)5