How can I Write dsl query inside of the Nest query with C# for updatinds specific fields?

Hi;

Iam trying to update my index fields by using CId ; CId can be used many rows. My Structure like :

{
   AId: 4244,
   BId : 53535,
   CId: "4242342"
}

if you look at below query what My intend to achieve, It returns to me a result what I desire.

POST usereventsreduced-*/_update_by_query
{
  "script": {
    "source": "ctx._source.AId = 1;",
    "lang": "painless"
  },
  "query": {
    "match": {
      "CId": "b60d505f-baf6-4522-b2a3-659509435c29"
    }
  }
}

But I want to use above query in C# by using Nest. if you look at my below code , you will understand what I mean. But it didn't work.
Nest is really complicated and not easy to understand . how can I do that above query by writing Nest -C# ?


 foreach (var pageview in pageviews)
           {
              
               var updateResponse = client.Update<object>("CId:${pageview.CId}", u => u
   .Index(indexName)
   .Script(s => s.Source("ctx._source.AId = params.AId")
       .Lang("painless")
       .Params(d => d
           .Add("AId", pageview.AId)
       )
   )
);
               Console.WriteLine(updateResponse.Result.ToString());
           }

YOU HAVE TO BE CAREFUL!

it's not easy creating a class. Please don't do that. Because my index feeding from very dynamic fields MyClass can have 50 filelds 1 hot later 65 fieds.
Actually I don't want to use creating class ( AId, BId, CId, DId... etc.) because my fields are dynamic
Thanks for your help

The same update_by_query API call in NEST would be

var client = new ElasticClient();

var updateByQueryResponse = client.UpdateByQuery<object>(u => u
    .Index("usereventsreduced-*")
	.Script(s => s
		.Source("ctx._source.AId = 1;")
		.Lang("painless")
	)
	.Query(q => q
		.Match(m => m
			.Field("CId")
			.Query("b60d505f-baf6-4522-b2a3-659509435c29")
		)
	)
);

with the fluent interface API/syntax. The fluent method calls and lambda expressions intend to mimic the structure of the JSON request.

Or if you prefer the object initializer syntax

var updateByQueryResponse = client.UpdateByQuery(new UpdateByQueryRequest("usereventsreduced-*")
{
	Script = new InlineScript("ctx._source.AId = 1;")
	{
		Lang = "painless"
	},
	Query = new MatchQuery
	{
		Field = "CId",
		Query = "b60d505f-baf6-4522-b2a3-659509435c29"
	}
});
1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.