Search code examples
javaelasticsearchspring-data-elasticsearch

Unable to update phone number using elastic search query, getting mapper_parsing_exception


I am trying to update the phone number using the below query, but I am getting "mapper_parsing_exception. Refer the code attached below.

Query:

POST /ps/_update/alOKenkBHofR-Ip1hPcv
{
  "doc": {
    "ph": {
      "na": null,
      "nu": "877842376",
      "cc": "91",
      "ex": null,
      "pt": "M"
    },
    "mdt": "20210615T044700.714Z"
  }

Response:

{
  "error" : {
    "root_cause" : [
      {
        "type" : "mapper_parsing_exception",
        "reason" : "failed to parse field [ph] of type [text] in document with id 'alOKenkBHofR-Ip1hPcv'. Preview of field's value: '{cc=91, na=null, ex=null, pt=M, nu=877842376}'"
      }
    ],
    "type" : "mapper_parsing_exception",
    "reason" : "failed to parse field [ph] of type [text] in document with id 'alOKenkBHofR-Ip1hPcv'. Preview of field's value: '{cc=91, na=null, ex=null, pt=M, nu=877842376}'",
    "caused_by" : {
      "type" : "illegal_state_exception",
      "reason" : "Can't get text on a START_OBJECT at 1:3558"
    }
  },
  "status" : 400
}

// code

 Document document = Document.create();
            Map<String, Object> map = new Phone.ToMap().convert((Phone) value);
            document.put(key, map);

        UpdateQuery updateQuery = UpdateQuery.builder(id).withDocument(document).build();
        UpdateResponse updateResponse = operations.update(updateQuery, indexCoordinates());
        Result result = updateResponse.getResult();

Solution

  • Your ph is part of bi object so for updating your document you should do this query:

    POST /ps/_update/alOKenkBHofR-Ip1hPcv
    {
      "doc": {
        "bi"{
            "ph": {
              "na": null,
              "nu": "877842376",
              "cc": "91",
              "ex": null,
              "pt": "M"
             }
        },
        "mdt": "20210615T044700.714Z"
      }