How to Insert or Update document in ElasticSearch NEST 6
I want to store web page view event per user to elastic index. if ViewEvent for the specific user exists then Insert, else plus 1 count of existing ViewEvent.
The elastic document type is Post
public class ViewEvent
{
public int UserId {get; set;}
public int Count {get; set;}
}
I can do this by following the REST request:
http://192.168.20.80:9200/db/ViewEvent/2/_update
{
"script": {
"lang": "painless",
"source": "ctx._source.count += 1;"
},
"upsert" : {
"userId": 2,
"count": 1
}
}
Problem is when I try to do this by NEST client. Update part through the following code done.
var response1 = await _vastContext.Client(indexName)
.UpdateByQueryAsync<VastEvent>(u => u.Query(query =>
query.Terms(term => term.Field(f => f.UserId).Terms(2)))
.Script(script => script.Source($"ctx._source.count += 1;")
.Lang(ScriptLang.Painless)));
I cannot add upsert part to the request!