0

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.

Jose Vf
  • 1,493
  • 17
  • 26
Mohammad Sunny
  • 371
  • 1
  • 3
  • 15

1 Answers1

3

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

Jo Kristian Bergum
  • 2,984
  • 5
  • 8
  • After creating sesssion, it works.Thanks Jo very much. I am able to add documents now. And also please upvote my question. – Mohammad Sunny Sep 20 '18 at 10:44
  • You must move the creation of the DocumentAccess and Session to the constructor as those are quite costly to construct and also deconstruct. Your searcher should @Override deconstruct() { and close down both the session and the document access instances}. – Jo Kristian Bergum Sep 20 '18 at 10:53
  • Sure Jo, I will take care of this. Thanks for the suggestion. And if you find question useful please upvote it.Thanks. – Mohammad Sunny Sep 20 '18 at 11:12