Updating document in Elastic App Search with script not working

657 Views Asked by At

I am fairly new to the elastic search, and I am using the Elastic App Search.

So I am trying to update data in elastic app-search through MongoDB Realm App which basically provide triggers on CRUD operations.

I am able to add documents or update existing fields.

But the problem is I am unable to add elements to the array field. I want to add or delete elements from array, after some research I found out that it can be done using some scripts:

"script": {
  "source": "ctx._source.fieldToUpdate.add(elementToAdd);",
  "lang": "painless"
}

But it's just not working. I am using REST APIs to add or update data in elastic app search. And I am using elastic cloud managed service.

UPDATE - 1

I was using ES App Search, and I created and named the engine as "articles", when I tried to run queries using kibana, I had to use some weird name ".ent-search-engine-documents-article". So I tried using the same name in Elastic Search REST API

POST /.ent-search-engine-documents-article/_update/docid

And it worked perfectly fine, but I want to perform the same work using REST API of APP Search only.

1

There are 1 best solutions below

0
On

To perform CRUD operations on your data stored through AppSearch, you should use the Documents API.


AppSearch does not handle nested objects and only provides 4 field types: text, number, date and geolocation. if you are posting objects, it will flatten and stringify them as you described in your comment.

It's also the case for arrays, so you can't just add elements to a field that holds an array as it's just a text field, you need to re-write the whole field (though it does detect them as arrays and handles each element separately if you use that field as a facet for instance).

as for how to patch with the AppSearch REST API, here's a small example inspired from the official documentation:

curl -X PATCH 'https://[instance id].ent-search.[region].[provider].cloud.es.io:443/api/as/v1/engines/articles/documents' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer private-xxxxxxxxxxxxxxxxxxxx' \
-d '[
  { "id": "your_article_id", 
    "source": "your article source",
    "lang": "painless" }
]'

There are also clients for several programming languages that you may find helpful or more intuitive to use.


The weird names you see for your engines, like ".ent-search-engine-documents-article" are the underlying indices on ElasticSearch, and you normally would not like to manipulate them directly.