FOSElasticaBundle
FOSElasticaBundle copied to clipboard
Removing empty attributes on update
Hi,
I have an issue with FOSElasticaBundle, with emptying / removing attributes in elasticsearch.
I'm running:
friendsofsymfony/elastica-bundle v5.0.3
ruflin/elastica v6.1.1
jms/serializer-bundle v3.2.0
jms/serializer v2.3.0
symfony v4.2.7
elasticsearch v6.7.1
When I save an object via doctrine. it get's correctly serialized and are being send to elasticsearch. Each update is also working:
{
"update":{
"_index":"demo_companies",
"_type":"company",
"_id":13
}
}
{
"doc":{
"id":13,
"company_name":"Demo Company",
"company_tagline":"Only the finest",
},
"doc_as_upsert":true
}
But when I clear the company_tagline in the form and save it get's emptied in the database, as expected. Now after serialization the company_tagline is missing, due to not serializing emtpy string. So the bulk update looks like this:
{
"update":{
"_index":"demo_companies",
"_type":"company",
"_id":13
}
}
{
"doc":{
"id":13,
"company_name":"Demo Company",
},
"doc_as_upsert":true
}
When now querying elasticsearch the company_tagline is still in place:
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 14,
"max_score" : 1.0,
"hits" : [
{
"_index" : "demo_companies",
"_type" : "company",
"_id" : "13",
"_score" : 1.0,
"_source" : {
"id" : 13,
"company_name" : "FTI Touristik GmbH",
"company_tagline":"Only the finest"
}
}
]
}
}
I assume thats because doc_as_upsert only updates attributes in place. So since company_tagline is missing in the request it does not get updated / removed.
Is there any way to perform an hard update of the entry instead of upserting it?
You can make something like company to elastica transformer in which you will pass all data explicity. Found some example here: http://www.inanzzz.com/index.php/post/ji7r/manually-creating-and-manipulating-elasticsearch-index-with-a-custom-model-to-elastica-transformer-service