Dynamically adding document using Document API in Vespa

134 Views Asked by At

I have created a Searcher class in my application which have multiple documents. In Searcher class I want to write document into particular type document. But it is not reflecting in Vespa. My Code is below:

    public Result search(Query query, Execution execution) {

        Result result = execution.search(query);

        DocumentAccess access = DocumentAccess.createDefault();
        DocumentType type = access.getDocumentTypeManager().getDocumentType("location");
        DocumentId id = new DocumentId("id:location:location::4");
        Document document = new Document(type, id);

        document.setFieldValue("token", "qwerty");
        document.setFieldValue("latlong", "12.343,12.4343");
        document.setFieldValue("data_timestamp", "00:00:00 00:00:00");

        // return the result up the chain
        return result;
    }

Here I am writing document to location type. My Location.sd class:

search location 
{
    document location {
        field token type string {
            indexing: index
        }
        field latlong type string {
            indexing: attribute
        }
        field data_timestamp type string {
            indexing: attribute
        }
    }
    fieldset default {
        fields: token
    }
}

When I want to get document using: http://localhost:8080/document/v1/location/location/docid/4

I got the result:
{
    "id": "id:location:location::4",
    "pathId": "/document/v1/location/location/docid/4"
}

I should get following output:

{
    "fields": {
        "token": "qwerty",
        "latlong": "12.343,12.4343",
        "data_timestamp": "00:00:00 00:00:00"
    },
    "id": "id:location:location::4",
    "pathId": "/document/v1/location/location/docid/4"
}

Please help me out what I am doing wrong or missing something.

1

There are 1 best solutions below

3
On BEST ANSWER

If you want to add documents from within a searcher you should move the creation of the DocumentAccess instance to the constructor of your searcher to avoid creating a new instance on a per search request basis.

Creating a Document instance does not persist the data in Vespa, you then need to send it by creating a Session from the DocumentAccess instance. See complete examples here https://docs.vespa.ai/documentation/document-api-guide.html