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

**URL:** https://discuss.elastic.co/t/how-can-i-write-dsl-query-inside-of-the-nest-query-with-c-for-updatinds-specific-fields/218590
**Category:** Elasticsearch
**Created:** [February 10, 2020, 12:56pm UTC](https://discuss.elastic.co/t/how-can-i-write-dsl-query-inside-of-the-nest-query-with-c-for-updatinds-specific-fields/218590 "2020-02-10T12:56:26Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Yusuf\_Karatoprak](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yusuf_karatoprak/32/44539_2.png) [@Yusuf\_Karatoprak](https://discuss.elastic.co/u/Yusuf_Karatoprak)
#### Post date: [February 10, 2020, 12:56pm UTC](https://discuss.elastic.co/t/how-can-i-write-dsl-query-inside-of-the-nest-query-with-c-for-updatinds-specific-fields/218590/1 "2020-02-10T12:56:26Z")

</div>

Hi;

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

```auto
{
   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.

```auto
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# ?

```auto

 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

---

<div class="post-metadata">

### Author: ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)
#### Post date: [February 11, 2020, 2:11am UTC](https://discuss.elastic.co/t/how-can-i-write-dsl-query-inside-of-the-nest-query-with-c-for-updatinds-specific-fields/218590/2 "2020-02-11T02:11:27Z")

</div>

> [@Yusuf\_Karatoprak](#):
>
> ```json
> POST usereventsreduced-*/_update_by_query
> {
> "script": {
> "source": "ctx._source.AId = 1;",
> "lang": "painless"
> },
> "query": {
> "match": {
> "CId": "b60d505f-baf6-4522-b2a3-659509435c29"
> }
> }
> }
> 
> ```

The same `update_by_query` API call in NEST would be

```auto
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

```auto
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"
	}
});

```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [March 10, 2020, 2:11am UTC](https://discuss.elastic.co/t/how-can-i-write-dsl-query-inside-of-the-nest-query-with-c-for-updatinds-specific-fields/218590/3 "2020-03-10T02:11:29Z")

</div>

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