Search in sources :

Example 16 with Struct

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

the class SpanTreeTestCase method testCleanup.

@Test
public void testCleanup() {
    SpanList a = new SpanList();
    SpanTree tree = new SpanTree("ballooO", a);
    Span b = new Span(0, 1);
    Span c = new Span(1, 1);
    a.add(b).add(c);
    AnnotationReferenceDataType refTypeToDummy = new AnnotationReferenceDataType(dummy);
    AnnotationType annotationTypeWithRefToDummy = new AnnotationType("reftodummy", refTypeToDummy);
    AnnotationReferenceDataType refTypeToRefTypeToDummy = new AnnotationReferenceDataType(annotationTypeWithRefToDummy);
    AnnotationType annotationTypeWithRefToRefToDummy = new AnnotationType("reftoreftodummy", refTypeToRefTypeToDummy);
    StructDataType structType = new StructDataType("str");
    Field refToDummyField = new Field("refToDummy", refTypeToDummy);
    structType.addField(refToDummyField);
    AnnotationType annotationTypeWithStruct = new AnnotationType("structwithref", structType);
    Annotation dummyAnnotationForB = new Annotation(dummy);
    tree.annotate(b, dummyAnnotationForB);
    AnnotationReference referenceToDummyB = new AnnotationReference(refTypeToDummy, dummyAnnotationForB);
    Annotation annotationWithRefToDummyB = new Annotation(annotationTypeWithRefToDummy, referenceToDummyB);
    tree.annotate(annotationWithRefToDummyB);
    AnnotationReference referenceToRefToDummyB = new AnnotationReference(refTypeToRefTypeToDummy, annotationWithRefToDummyB);
    Annotation annotationWithRefToRefToDummyB = new Annotation(annotationTypeWithRefToRefToDummy, referenceToRefToDummyB);
    tree.annotate(annotationWithRefToRefToDummyB);
    Struct struct = new Struct(structType);
    struct.setFieldValue(refToDummyField, new AnnotationReference(refTypeToDummy, dummyAnnotationForB));
    Annotation annotationWithStruct = new Annotation(annotationTypeWithStruct, struct);
    tree.annotate(annotationWithStruct);
    Iterator<Annotation> annotationIt;
    assertEquals(4, tree.numAnnotations());
    assertTrue(a.isValid());
    assertTrue(b.isValid());
    assertTrue(c.isValid());
    assertEquals(2, a.numChildren());
    {
        List<Annotation> allAnnotationsList = new ArrayList<>();
        for (Annotation an : tree) {
            allAnnotationsList.add(an);
        }
        Collections.sort(allAnnotationsList);
        annotationIt = allAnnotationsList.iterator();
    }
    // NOTE: ordering of annotations is derived from their name
    assertSame(annotationWithRefToDummyB, annotationIt.next());
    assertFalse(annotationWithRefToDummyB.hasSpanNode());
    assertFalse(annotationWithRefToDummyB.isSpanNodeValid());
    assertTrue(annotationWithRefToDummyB.hasFieldValue());
    assertSame(dummyAnnotationForB, ((AnnotationReference) annotationWithRefToDummyB.getFieldValue()).getReference());
    assertSame(annotationWithStruct, annotationIt.next());
    assertFalse(annotationWithStruct.hasSpanNode());
    assertFalse(annotationWithStruct.isSpanNodeValid());
    assertTrue(annotationWithStruct.hasFieldValue());
    assertSame(struct, annotationWithStruct.getFieldValue());
    assertSame(dummyAnnotationForB, ((AnnotationReference) struct.getFieldValue(refToDummyField)).getReference());
    assertSame(annotationWithRefToRefToDummyB, annotationIt.next());
    assertFalse(annotationWithRefToRefToDummyB.hasSpanNode());
    assertFalse(annotationWithRefToRefToDummyB.isSpanNodeValid());
    assertTrue(annotationWithRefToRefToDummyB.hasFieldValue());
    assertSame(annotationWithRefToDummyB, ((AnnotationReference) annotationWithRefToRefToDummyB.getFieldValue()).getReference());
    assertSame(dummyAnnotationForB, annotationIt.next());
    assertTrue(dummyAnnotationForB.hasSpanNode());
    assertTrue(dummyAnnotationForB.isSpanNodeValid());
    assertFalse(dummyAnnotationForB.hasFieldValue());
    assertFalse(annotationIt.hasNext());
    // removing b!!
    a.remove(b);
    assertEquals(4, tree.numAnnotations());
    assertTrue(a.isValid());
    assertFalse(b.isValid());
    assertTrue(c.isValid());
    assertEquals(1, a.numChildren());
    {
        List<Annotation> allAnnotationsList = new ArrayList<>();
        for (Annotation an : tree) {
            allAnnotationsList.add(an);
        }
        Collections.sort(allAnnotationsList);
        annotationIt = allAnnotationsList.iterator();
    }
    assertSame(annotationWithRefToDummyB, annotationIt.next());
    assertFalse(annotationWithRefToDummyB.hasSpanNode());
    assertFalse(annotationWithRefToDummyB.isSpanNodeValid());
    assertTrue(annotationWithRefToDummyB.hasFieldValue());
    assertSame(dummyAnnotationForB, ((AnnotationReference) annotationWithRefToDummyB.getFieldValue()).getReference());
    assertSame(annotationWithStruct, annotationIt.next());
    assertFalse(annotationWithStruct.hasSpanNode());
    assertFalse(annotationWithStruct.isSpanNodeValid());
    assertTrue(annotationWithStruct.hasFieldValue());
    assertSame(struct, annotationWithStruct.getFieldValue());
    assertSame(dummyAnnotationForB, ((AnnotationReference) struct.getFieldValue(refToDummyField)).getReference());
    assertSame(annotationWithRefToRefToDummyB, annotationIt.next());
    assertFalse(annotationWithRefToRefToDummyB.hasSpanNode());
    assertFalse(annotationWithRefToRefToDummyB.isSpanNodeValid());
    assertTrue(annotationWithRefToRefToDummyB.hasFieldValue());
    assertSame(annotationWithRefToDummyB, ((AnnotationReference) annotationWithRefToRefToDummyB.getFieldValue()).getReference());
    assertSame(dummyAnnotationForB, annotationIt.next());
    assertTrue(dummyAnnotationForB.hasSpanNode());
    assertFalse(dummyAnnotationForB.isSpanNodeValid());
    assertFalse(dummyAnnotationForB.hasFieldValue());
    assertFalse(annotationIt.hasNext());
    // cleaning up tree!!
    tree.cleanup();
    assertEquals(1, tree.numAnnotations());
    assertTrue(a.isValid());
    assertFalse(b.isValid());
    assertTrue(c.isValid());
    assertEquals(1, a.numChildren());
    assertFalse(dummyAnnotationForB.hasSpanNode());
    assertFalse(dummyAnnotationForB.isSpanNodeValid());
    assertFalse(dummyAnnotationForB.hasFieldValue());
    assertFalse(annotationWithRefToDummyB.hasSpanNode());
    assertFalse(annotationWithRefToDummyB.isSpanNodeValid());
    assertFalse(annotationWithRefToDummyB.hasFieldValue());
    assertFalse(annotationWithRefToRefToDummyB.hasSpanNode());
    assertFalse(annotationWithRefToRefToDummyB.isSpanNodeValid());
    assertFalse(annotationWithRefToRefToDummyB.hasFieldValue());
    annotationIt = tree.iterator();
    assertSame(annotationWithStruct, annotationIt.next());
    assertFalse(annotationWithStruct.hasSpanNode());
    assertFalse(annotationWithStruct.isSpanNodeValid());
    assertTrue(annotationWithStruct.hasFieldValue());
    assertSame(struct, annotationWithStruct.getFieldValue());
    assertNull(struct.getFieldValue(refToDummyField));
    assertFalse(annotationIt.hasNext());
}
Also used : Struct(com.yahoo.document.datatypes.Struct) Field(com.yahoo.document.Field) StructDataType(com.yahoo.document.StructDataType) Test(org.junit.Test)

Example 17 with Struct

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

the class SpanTreeTestCase method testSimpleCopy2.

@Test
public void testSimpleCopy2() {
    StringFieldValue string = new StringFieldValue("yahoooo");
    SpanList list = new SpanList();
    Span span1 = new Span(0, 1);
    Span span2 = new Span(1, 1);
    list.add(span1).add(span2);
    SpanTree tree = new SpanTree("foo", list);
    string.setSpanTree(tree);
    Annotation a1 = new Annotation(grape);
    tree.annotate(span1, a1);
    Annotation a2 = new Annotation(apple);
    tree.annotate(span2, a2);
    Struct donald = new Struct(person);
    donald.setFieldValue("firstname", "donald");
    donald.setFieldValue("lastname", "duck");
    donald.setFieldValue("birthyear", 1929);
    Annotation donaldAnn = new Annotation(personA, donald);
    tree.annotate(list, donaldAnn);
    StringFieldValue stringCopy = string.clone();
    assertEquals(string, stringCopy);
    assertNotSame(string, stringCopy);
    SpanTree treeCopy = stringCopy.getSpanTree("foo");
    assertEquals(tree, treeCopy);
    assertNotSame(tree, treeCopy);
    SpanList listCopy = (SpanList) treeCopy.getRoot();
    assertEquals(list, listCopy);
    assertNotSame(list, listCopy);
    Span span1Copy = (Span) listCopy.children().get(0);
    assertEquals(span1, span1Copy);
    assertNotSame(span1, span1Copy);
    Span span2Copy = (Span) listCopy.children().get(1);
    assertEquals(span2, span2Copy);
    assertNotSame(span2, span2Copy);
    Iterator<Annotation> annotationIt;
    {
        List<Annotation> allAnnotationsList = new ArrayList<>();
        for (Annotation an : treeCopy) {
            allAnnotationsList.add(an);
        }
        Collections.sort(allAnnotationsList);
        annotationIt = allAnnotationsList.iterator();
    }
    Annotation a1Copy = annotationIt.next();
    assertEquals(a1, a1Copy);
    assertNotSame(a1, a1Copy);
    assertSame(span1Copy, a1Copy.getSpanNode());
    assertNotSame(span1, a1Copy.getSpanNode());
    Annotation donaldAnnCopy = annotationIt.next();
    assertEquals(donaldAnn, donaldAnnCopy);
    assertNotSame(donaldAnn, donaldAnnCopy);
    assertSame(listCopy, donaldAnnCopy.getSpanNode());
    assertNotSame(list, donaldAnnCopy.getSpanNode());
    Struct donaldCopy = (Struct) donaldAnnCopy.getFieldValue();
    assertEquals(donald, donaldCopy);
    assertNotSame(donald, donaldCopy);
    Annotation a2Copy = annotationIt.next();
    assertEquals(a2, a2Copy);
    assertNotSame(a2, a2Copy);
    assertSame(span2Copy, a2Copy.getSpanNode());
    assertNotSame(span2, a2Copy.getSpanNode());
}
Also used : StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) Struct(com.yahoo.document.datatypes.Struct) Test(org.junit.Test)

Example 18 with Struct

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

the class DocTestCase method simple4.

public void simple4() {
    // the following line works inside process(Document, Arguments, Processing) in a DocumentProcessor
    AnnotationTypeRegistry atr = processing.getService().getDocumentTypeManager().getAnnotationTypeRegistry();
    StringFieldValue text = new StringFieldValue("<html><head><title>Diary</title></head><body>I live in San Francisco</body></html>");
    // 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
    SpanList root = new SpanList();
    SpanTree tree = new SpanTree("html", root);
    AnnotationType textType = atr.getType("text");
    AnnotationType beginTag = atr.getType("begintag");
    AnnotationType endTag = atr.getType("endtag");
    AnnotationType bodyType = atr.getType("body");
    AnnotationType headerType = atr.getType("header");
    AnnotationType cityType = atr.getType("city");
    Struct position = (Struct) cityType.getDataType().createFieldValue();
    position.setFieldValue("latitude", 37.774929);
    position.setFieldValue("longitude", -122.419415);
    Annotation city = new Annotation(cityType, position);
    SpanList header = new SpanList();
    {
        Span span1 = new Span(6, 6);
        Span span2 = new Span(12, 7);
        Span span3 = new Span(19, 5);
        Span span4 = new Span(24, 8);
        Span span5 = new Span(32, 7);
        header.add(span1).add(span2).add(span3).add(span4).add(span5);
        tree.annotate(span1, beginTag).annotate(span2, beginTag).annotate(span3, textType).annotate(span4, endTag).annotate(span4, endTag).annotate(header, headerType);
    }
    SpanList textNode = new SpanList();
    {
        Span span1 = new Span(45, 10);
        Span span2 = new Span(55, 13);
        textNode.add(span1).add(span2);
        tree.annotate(span2, city).annotate(textNode, textType);
    }
    SpanList body = new SpanList();
    {
        Span span1 = new Span(39, 6);
        Span span2 = new Span(68, 7);
        body.add(span1).add(textNode).add(span2);
        tree.annotate(span1, beginTag).annotate(span2, endTag).annotate(body, bodyType);
    }
    {
        Span span1 = new Span(0, 6);
        Span span2 = new Span(75, 7);
        root.add(span1).add(header).add(body).add(span2);
        tree.annotate(span1, beginTag).annotate(span2, endTag);
    }
    text.setSpanTree(tree);
}
Also used : StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) Struct(com.yahoo.document.datatypes.Struct)

Example 19 with Struct

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

the class JsonRendererTestCase method testFieldValueInHit.

@Test
public final void testFieldValueInHit() throws IOException, InterruptedException, ExecutionException, JSONException {
    String expected = "{\n" + "    \"root\": {\n" + "        \"children\": [\n" + "            {\n" + "                \"fields\": {\n" + "                    \"fromDocumentApi\":{\"integerField\":123, \"stringField\":\"abc\"}" + "                },\n" + "                \"id\": \"fieldValueTest\",\n" + "                \"relevance\": 1.0\n" + "            }\n" + "        ],\n" + "        \"fields\": {\n" + "            \"totalCount\": 1\n" + "        },\n" + "        \"id\": \"toplevel\",\n" + "        \"relevance\": 1.0\n" + "    }\n" + "}\n";
    Result r = newEmptyResult();
    Hit h = new Hit("fieldValueTest");
    StructDataType structType = new StructDataType("jsonRenderer");
    structType.addField(new Field("stringField", DataType.STRING));
    structType.addField(new Field("integerField", DataType.INT));
    Struct struct = structType.createFieldValue();
    struct.setFieldValue("stringField", "abc");
    struct.setFieldValue("integerField", 123);
    h.setField("fromDocumentApi", struct);
    r.hits().add(h);
    r.setTotalHitCount(1L);
    String summary = render(r);
    assertEqualJson(expected, summary);
}
Also used : Field(com.yahoo.document.Field) FastHit(com.yahoo.prelude.fastsearch.FastHit) Hit(com.yahoo.search.result.Hit) StructDataType(com.yahoo.document.StructDataType) JSONString(com.yahoo.prelude.hitfield.JSONString) Result(com.yahoo.search.Result) Struct(com.yahoo.document.datatypes.Struct) Test(org.junit.Test)

Example 20 with Struct

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

the class DocumentScriptTestCase method requireThatLinguisticsSpanTreeIsRemovedFromStructStringFields.

@Test
public void requireThatLinguisticsSpanTreeIsRemovedFromStructStringFields() {
    StructDataType structType = new StructDataType("myStruct");
    structType.addField(new Field("myString", DataType.STRING));
    Struct in = new Struct(structType);
    in.setFieldValue("myString", newString(SpanTrees.LINGUISTICS, "mySpanTree"));
    Struct out = (Struct) processDocument(in);
    assertSpanTrees(out.getFieldValue("myString"), "mySpanTree");
    StringFieldValue str = (StringFieldValue) ((MapValueUpdate) processFieldUpdate(in)).getUpdate().getValue();
    assertSpanTrees(str, "mySpanTree");
    str = (StringFieldValue) ((MapValueUpdate) processFieldUpdate(in)).getUpdate().getValue();
    assertSpanTrees(str, "mySpanTree");
}
Also used : MapValueUpdate(com.yahoo.document.update.MapValueUpdate) Field(com.yahoo.document.Field) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) StructDataType(com.yahoo.document.StructDataType) Struct(com.yahoo.document.datatypes.Struct) Test(org.junit.Test)

Aggregations

Struct (com.yahoo.document.datatypes.Struct)38 StringFieldValue (com.yahoo.document.datatypes.StringFieldValue)25 Test (org.junit.Test)21 FieldValue (com.yahoo.document.datatypes.FieldValue)13 IntegerFieldValue (com.yahoo.document.datatypes.IntegerFieldValue)11 StructDataType (com.yahoo.document.StructDataType)9 Array (com.yahoo.document.datatypes.Array)9 Field (com.yahoo.document.Field)7 Document (com.yahoo.document.Document)6 MapFieldValue (com.yahoo.document.datatypes.MapFieldValue)5 GrowableByteBuffer (com.yahoo.io.GrowableByteBuffer)4 DocumentType (com.yahoo.document.DocumentType)3 DoubleFieldValue (com.yahoo.document.datatypes.DoubleFieldValue)3 FloatFieldValue (com.yahoo.document.datatypes.FloatFieldValue)3 LongFieldValue (com.yahoo.document.datatypes.LongFieldValue)3 WeightedSet (com.yahoo.document.datatypes.WeightedSet)3 AddValueUpdate (com.yahoo.document.update.AddValueUpdate)3 FieldUpdate (com.yahoo.document.update.FieldUpdate)3 MapValueUpdate (com.yahoo.document.update.MapValueUpdate)3 ValueUpdate (com.yahoo.document.update.ValueUpdate)3