Updating some Fields and Reindexing Google Search API Documents

56 Views Asked by At

I'm trying to use the Google Search API on my Android App and I have added the documents in the index.

Now I'm retrieving the document by its ID and I want to update just some fields, not all of then.

That I tried to do but with no success:

 // Fetch a single document by its  doc_id
            Document doc = index.get(myDocID);
            LOG.warning("FOUND DOC " + doc.toString());

            doc.newBuilder()

                    .addField(Field.newBuilder()
                            .setName("field1")
                            .setNumber(valueForField1))
                    .addField(Field.newBuilder()
                            .setName("field2")
                            .setNumber(valueForField2))
                    .addField(Field.newBuilder()
                            .setName("field3")
                            .setDate(valueForField3))
                    .build();

            LOG.warning("NEW DOC " + doc.toString());
            try {
                index.put(doc);
                LOG.warning("PUT OK");
            } catch (PutException e) {
                if (StatusCode.TRANSIENT_ERROR.equals(e.getOperationResult().getCode())) {
                    // retry putting the document
                }
                LOG.warning("ERROR");
            }

In the code above I'm trying to update just 3 fields of my document.

The only info that I found in the documentation is that if I want to Reindex a document I have to save a new one with the same ID and it will be Reindexed.

Do I have to fill all the fields or there's a way to update just some fields?

I appreciate any answers

1

There are 1 best solutions below

0
Mehdy On

You need to copy all properties (fields, facets, doc_id, etc.) of the existing document to the new builder. Document.newBuilder is static and is not using any of the existing document's data.