# Using a Terms Query via Elastic.Clients.Elasticsearch 8.1.1 .NET

**URL:** <https://discuss.elastic.co/t/using-a-terms-query-via-elastic-clients-elasticsearch-8-1-1-net/332817>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [May 8, 2023, 12:33pm UTC](https://discuss.elastic.co/t/using-a-terms-query-via-elastic-clients-elasticsearch-8-1-1-net/332817 "2023-05-08T12:33:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![felix-stnr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/felix-stnr/32/120218_2.png) [@felix-stnr](https://discuss.elastic.co/u/felix-stnr)\
**Post date:** [May 8, 2023, 12:33pm UTC](https://discuss.elastic.co/t/using-a-terms-query-via-elastic-clients-elasticsearch-8-1-1-net/332817/1 "2023-05-08T12:33:13Z")

</div>

I am currently trying to write a Terms Query via the Elastic.Clients.Elasticsearch 8.1.1 .NET client. To be more precise, I want to write following query in C#:

```auto
GET persons/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "terms": {
            "companyId": [
              1, 2, 3
            ]
          }
        }
      ]
    }
  }
}

```

Note that the `persons` index already exists and contains a numeric field called `companyId`. I have tried to write the query in C# like this:

```auto
long[] companyIds = new long[] { 1, 2, 3 };
var response = await _elasticsearchClient.SearchAsync<Person>(s => s
    .Index("persons")
    .Query(q => q
        .Bool(b => b
            .Must(m => m
                .Terms(ts => ts
                    .Field("companyId")
                    .Terms(companyIds)
                )
            )
        )
    )
);

```

Unfortunately, this does not work because the inner `Terms` method expects a `Elastic.Clients.Elasticsearch.QueryDsl.TermsQueryField` as parameter instead of a `long[]`. So my question is: Is there a way to map the `long[]` (or e.g. a `List<long>`) to the `Elastic.Clients.Elasticsearch.QueryDsl.TermsQueryField` or is there another way to write this query in C#?  
I am aware that this platform is not intended to be a 'How to write this query in C#'-forum, but I am a little lost with the currently provided documentation, since it only covers the most basic usage of the library. Thanks in advance for any help!

---

<div class="post-metadata">

**Author:** ![stevejgordon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stevejgordon/32/80912_2.png) [@stevejgordon](https://discuss.elastic.co/u/stevejgordon)\
**Post date:** [May 11, 2023, 1:13pm UTC](https://discuss.elastic.co/t/using-a-terms-query-via-elastic-clients-elasticsearch-8-1-1-net/332817/2 "2023-05-11T13:13:41Z")

</div>

Hi, @felix-stnr.

You raise a great example of where our code generated APIs could be improved. I'll open an issue to track work to make this easier to achieve.

It's quite verbose but you can create it like this for now:

```auto
var companyIds = new long[] { 1, 2, 3 };
var response = await client.SearchAsync<Person>(s => s
	.Index("persons")
	.Query(q => q
		.Bool(b => b
			.Must(m => m
				.Terms(ts => ts
					.Field("companyId")
					.Terms(new Elastic.Clients.Elasticsearch.QueryDsl.TermsQueryField(companyIds.Select(id => FieldValue.Long(id)).ToArray()))
				)
			)
		)
	)
);

```

---

<div class="post-metadata">

**Author:** ![felix-stnr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/felix-stnr/32/120218_2.png) [@felix-stnr](https://discuss.elastic.co/u/felix-stnr)\
**Post date:** [May 11, 2023, 2:20pm UTC](https://discuss.elastic.co/t/using-a-terms-query-via-elastic-clients-elasticsearch-8-1-1-net/332817/3 "2023-05-11T14:20:16Z")

</div>

Thank you very much! I am already looking forward to a more comprehensive documentation for the .NET client. 😁

---

<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:** [June 8, 2023, 2:20pm UTC](https://discuss.elastic.co/t/using-a-terms-query-via-elastic-clients-elasticsearch-8-1-1-net/332817/4 "2023-06-08T14:20:28Z")

</div>

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