Search in sources :

Example 1 with Document

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

the class Rot13DocumentProcessor method process.

@Override
public Progress process(Processing processing) {
    int oldVal = counter.getAndIncrement();
    if ((oldVal % 3) != 0) {
        return Progress.LATER;
    }
    for (DocumentOperation op : processing.getDocumentOperations()) {
        if (op instanceof DocumentPut) {
            Document document = ((DocumentPut) op).getDocument();
            StringFieldValue oldTitle = (StringFieldValue) document.getFieldValue(FIELD_NAME);
            if (oldTitle != null) {
                document.setFieldValue(FIELD_NAME, rot13(oldTitle.getString()));
            }
        }
    }
    return Progress.DONE;
}
Also used : DocumentOperation(com.yahoo.document.DocumentOperation) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) DocumentPut(com.yahoo.document.DocumentPut) Document(com.yahoo.document.Document)

Example 2 with Document

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

the class JDiscContainerDocprocTest method requireThatLaterDocumentProcessingWorks.

@Test
public void requireThatLaterDocumentProcessingWorks() throws Exception {
    try (Application app = new ApplicationBuilder().servicesXml(getXML(CHAIN_NAME, Rot13DocumentProcessor.class.getCanonicalName())).networking(Networking.disable).documentType("music", DOCUMENT).build()) {
        JDisc container = app.getJDisc("container");
        DocumentProcessing docProc = container.documentProcessing();
        DocumentType type = docProc.getDocumentTypes().get("music");
        ChainRegistry<DocumentProcessor> chains = docProc.getChains();
        assertTrue(chains.allComponentsById().containsKey(new ComponentId(CHAIN_NAME)));
        Document doc = new Document(type, "doc:this:is:a:great:album");
        doc.setFieldValue("title", "Great Album!");
        com.yahoo.docproc.Processing processing;
        DocumentProcessor.Progress progress;
        DocumentPut put = new DocumentPut(doc);
        processing = com.yahoo.docproc.Processing.of(put);
        progress = docProc.processOnce(ComponentSpecification.fromString(CHAIN_NAME), processing);
        assertThat(progress, instanceOf(DocumentProcessor.LaterProgress.class));
        assertThat(doc.getFieldValue("title").toString(), equalTo("Great Album!"));
        progress = docProc.processOnce(ComponentSpecification.fromString(CHAIN_NAME), processing);
        assertThat(progress, instanceOf(DocumentProcessor.LaterProgress.class));
        assertThat(doc.getFieldValue("title").toString(), equalTo("Great Album!"));
        progress = docProc.processOnce(ComponentSpecification.fromString(CHAIN_NAME), processing);
        assertThat(progress, sameInstance(DocumentProcessor.Progress.DONE));
        assertThat(doc.getFieldValue("title").toString(), equalTo("Terng Nyohz!"));
    }
}
Also used : Rot13DocumentProcessor(com.yahoo.application.container.docprocs.Rot13DocumentProcessor) Rot13DocumentProcessor(com.yahoo.application.container.docprocs.Rot13DocumentProcessor) DocumentProcessor(com.yahoo.docproc.DocumentProcessor) DocumentPut(com.yahoo.document.DocumentPut) DocumentType(com.yahoo.document.DocumentType) Document(com.yahoo.document.Document) ApplicationBuilder(com.yahoo.application.ApplicationBuilder) Application(com.yahoo.application.Application) ComponentId(com.yahoo.component.ComponentId) Test(org.junit.Test)

Example 3 with Document

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

the class ScriptTestCase method requireThatIfExpressionPassesOriginalInputAlong.

@Test
public void requireThatIfExpressionPassesOriginalInputAlong() throws ParseException {
    Document input = new Document(type, "doc:scheme:");
    Document output = Expression.execute(Expression.fromString("'foo' | if (1 < 2) { 'bar' | index 'out-1' } else { 'baz' | index 'out-1' } | index 'out-1'"), input);
    assertNotNull(output);
    assertEquals(new StringFieldValue("foo"), output.getFieldValue("out-1"));
}
Also used : StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) Document(com.yahoo.document.Document) Test(org.junit.Test)

Example 4 with Document

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

the class ScriptTestCase method requireThatFactoryMethodWorks.

@Test
public void requireThatFactoryMethodWorks() throws ParseException {
    Document input = new Document(type, "doc:scheme:");
    input.setFieldValue("in-1", new StringFieldValue("FOO"));
    Document output = Expression.execute(Expression.fromString("input 'in-1' | { index 'out-1'; lowercase | index 'out-2' }"), input);
    assertNotNull(output);
    assertEquals(new StringFieldValue("FOO"), output.getFieldValue("out-1"));
    assertEquals(new StringFieldValue("foo"), output.getFieldValue("out-2"));
}
Also used : StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) Document(com.yahoo.document.Document) Test(org.junit.Test)

Example 5 with Document

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

the class DocumentFieldTemplate method hit.

@Override
public void hit(Context context, Writer writer) throws IOException {
    DocumentHit hit = (DocumentHit) context.get("hit");
    Document doc = hit.getDocument();
    // Assume field existence has been checked before we ever get here.
    // Also assume that relevant encoding/content type is set
    // appropriately according to the request and the field's content
    // type, as this is immutable in the template set.
    FieldValue value = doc.getFieldValue(field);
    if (field.getDataType() == DataType.RAW) {
        ByteWriter bw = (ByteWriter) writer;
        bw.append(((Raw) value).getByteBuffer().array());
    } else {
        writer.write(XML.xmlEscape(value.toString(), false));
    }
}
Also used : ByteWriter(com.yahoo.io.ByteWriter) Raw(com.yahoo.document.datatypes.Raw) FieldValue(com.yahoo.document.datatypes.FieldValue) Document(com.yahoo.document.Document)

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