I've been working with Elasticsearch for some days. As i'm creating a CRUD, I've come across the updateByQuery method. I'm working with nestjs, and the way that I'm updating a field is:
await this.elasticSearch.updateByQuery(
{
index: 'my_index_user',
body:{
query:{
match:{
name: 'user_name',
}
},
script: {
inline : 'ctx._source.name = "new_user_name"'
}
}
}
);
My question is:
Why does elasticsearch need this syntax 'ctx._source.name = "new_user_name"'
to specifie what the new value of the field name should be? What is ctx._source is this context?
CodePudding user response:
As mentioned in the official doc of source filtering, using this you can fetch field value in the _source
(Value which sent to Elasticsearch and this is stored as it is, and doesn't go through the analysis process).
Let's take an example of text
field for which standard
analyzer(Default) is applied, and you store the value of foo bar
in this field, Elasticsearch
breaks the value of field as it goes through the analysis process and foo
and bar
two tokens are stored in the inverted index of Elasticsearch, but if you want to see the original value ie foo bar
, you can check the _source
and get it.
Hence, it's always better to have the original value(without analysis process) to be in the _source
, hence using this API, you are updating the field value there.. this also helps when you want to reindex later to new index or change the way its analyzed as you have the original value in _source.