0

I am currently in the process of attempting to update an ElasticSearch document via the Java API. I have a groovy script with the following code:

static updateRequestById(String agencyIndex, String type, String id, def policy) {        
        UpdateRequest updateRequest = new UpdateRequest()
        updateRequest.docAsUpsert(true);
        updateRequest.parent("agentNumber");
        updateRequest.index(agencyIndex)
        updateRequest.type(type)
        updateRequest.id(id)
        updateRequest.doc("policies", policy)
        elasticsearchClient.update(updateRequest).get()
    }

The problem with I am having is that I want to update an array within the following document:

{
   "took": 4,
   "timed_out": false,
   "_shards": {
      "total": 10,
      "successful": 10,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 1,
      "hits": [
         {
            "_index": "int-b-agency",
            "_type": "jacket",
            "_id": "99808.1.27.09_4644",
            "_score": 1,
            "_source": {
               "agentNumber": "99808.1.27.09",
               "fileNumber": "4644",
               "policies": [
                  {
                     "agentNumber": "99808.1.27.09",
                     "fileNumber": "4644",
                     "policyNumber": "2730609-91029084",
                     "checkNumber": "0",
                     "checkAmount": 0,
                     "createdOn": null,
                     "createdBy": "traxuser621",
                     "propertyTypeCode": "",
                     "propertyTypeDesc": "1-4 FAMILY RESIDENTIAL",
                     "ppaddress": "110 Allan Ct ",
                     "ppcity": "Jacksonville",
                     "ppstate": "FL",
                     "ppzip": "32226",
                     "ppcounty": "Duval",
                     "policytype": "",
                     "status": "Active",
                     "effectiveDate": "2015-04-01T00:00:00-05:00",
                     "formType": "BASIC OWNERS - ALTA Owners Policy 06_306_FL - FL Original Rate",
                     "rateCode": "FLOR",
                     "rateCodeDesc": "FL Original Rate",
                     "policyTypeCode": "1",
                     "policyTypeCodeDesc": "BASIC OWNERS",
                     "amount": 200000,
                     "hoiAgentNumber": "",
                     "proForma": false,
                     "pdfLocation": "\\\\10.212.61.206\\FNFCenter\\legacy_jacket_pdfs\\2015_4_FL6465\\Policy_2730609-91029084.pdf",
                     "legacyPolicy": "true",
                     "associatedPolNbr": null
                  }
               ]
            }
         }
      ]
   }
}

In the document above I have a document that has an array called "policies" with a single object. I want to be able to update the "policies" array with additional objects. The end result should look something like the following:

{
   "took": 4,
   "timed_out": false,
   "_shards": {
      "total": 10,
      "successful": 10,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 1,
      "hits": [
         {
            "_index": "int-b-agency",
            "_type": "jacket",
            "_id": "41341.1.81.38_41340103",
            "_score": 1,
            "_source": {
               "agentNumber": "41341.1.81.38",
               "fileNumber": "41340103",
               "policies": [
                  {
                     "agentNumber": "41341.1.81.38",
                     "fileNumber": "41340103",
                     "policyNumber": "8122638-91036874",
                     "checkNumber": "0",
                     "checkAmount": 0,
                     "createdOn": null,
                     "createdBy": "traxuser621",
                     "propertyTypeCode": "",
                     "propertyTypeDesc": "1-4 FAMILY RESIDENTIAL",
                     "ppaddress": "1800 Smith St ",
                     "ppcity": "sicklerville",
                     "ppstate": "PA",
                     "ppzip": "08105",
                     "ppcounty": "Dauphin",
                     "policytype": "",
                     "status": "Active",
                     "effectiveDate": "2016-02-01T00:00:00-06:00",
                     "formType": "TestData",
                     "rateCode": "PASALERATE",
                     "rateCodeDesc": "Sale Rate - Agent",
                     "policyTypeCode": "26",
                     "policyTypeCodeDesc": "SALE OWNERS",
                     "amount": 180000,
                     "hoiAgentNumber": "",
                     "proForma": false,
                     "pdfLocation": "SomeLocation1",
                     "legacyPolicy": "true",
                     "associatedPolNbr": null
                  },
                  {
                     "agentNumber": "41341.1.81.38",
                     "fileNumber": "41340103",
                     "policyNumber": "8122638-91036875",
                     "checkNumber": "0",
                     "checkAmount": 0,
                     "createdOn": null,
                     "createdBy": "traxuser621",
                     "propertyTypeCode": "",
                     "propertyTypeDesc": "1-4 FAMILY RESIDENTIAL",
                     "ppaddress": "1800 Smith St ",
                     "ppcity": "sicklerville",
                     "ppstate": "PA",
                     "ppzip": "08105",
                     "ppcounty": "Dauphin",
                     "policytype": "",
                     "status": "Active",
                     "effectiveDate": "2016-02-01T00:00:00-06:00",
                     "formType": "Test Data",
                     "rateCode": "PASALERATE",
                     "rateCodeDesc": "Sale Rate - Agent",
                     "policyTypeCode": "26",
                     "policyTypeCodeDesc": "SALE OWNERS",
                     "amount": 180000,
                     "hoiAgentNumber": "",
                     "proForma": false,
                     "pdfLocation": "SomeLocation2",
                     "legacyPolicy": "true",
                     "associatedPolNbr": null
                  }
               ]
            }
         }
      ]
   }
}

What am I doing wrong?

2 Answers 2

2

You can use a scripted update:

  1. Put your new policy in a parameter, for example policy
  2. Use a script like the following :

    if (!ctxt._source.policies) { ctxt._source.policies = [] }
    ctxt._source.policies += policy
    

See this documentation : https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the post. I have made the following modification "static updateRequestById(String agencyIndex, String type, String id, def policy) { String script = "ctx._source.policies+=" + policy; UpdateRequest updateRequest = new UpdateRequest(agencyIndex, type, id).routing("agentNumber").parent(id).script(script) elasticsearchClient.update(updateRequest).get() }" and now I receive document missing exception. I have also attempting to do this directly within a curl call and received the same message. What am I missing?
1

Updates in inverted indexes are deletes and replacements of documents. There is no in-place update like you find in a db. ES uses Lucene under the hood which in-turn implements a kick-ass inverted index.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.