# How to apply filter using NEST (.NET)

**URL:** <https://discuss.elastic.co/t/how-to-apply-filter-using-nest-net/267423>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [March 16, 2021, 9:27pm UTC](https://discuss.elastic.co/t/how-to-apply-filter-using-nest-net/267423 "2021-03-16T21:27:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jay2021](https://avatars.discourse-cdn.com/v4/letter/j/7feea3/32.png) [@jay2021](https://discuss.elastic.co/u/jay2021)\
**Post date:** [March 16, 2021, 9:27pm UTC](https://discuss.elastic.co/t/how-to-apply-filter-using-nest-net/267423/1 "2021-03-16T21:27:13Z")

</div>

I am trying to query my server using the .NET library NEST ([GitHub - elastic/elasticsearch-net: Elasticsearch.Net & NEST](https://github.com/elastic/elasticsearch-net))

I want my query to find any data where `source = "abc"` and `source_id in (" 123","145","158m")`

I executed the following using using Postman and it worked

```auto
{
    "query": {
        "bool" : {
            "must": {
                "multi_match": {
                    "query": "abc",
                    "fields": [
                        "source"
                    ]
                }
            },
            "filter" : {
                "terms" : {
                    "source_id" : ["123","145","158m"]
                }
            }
        }        
    },
    "fields": ["source_id"],
    "_source": false
}

```

First, is there a better query (for performance) to get the same data?

Second, I can't find a way to apply the `Filter` part into the query using the .net library.

Here is my c# code

```auto
var mySourceIds = new [] { "123","145","158m" }
var documents = await elasticClient.SearchAsync<MyRecord>(s => 
   s.Query(q =>
       q.Bool(b =>
          b.Must(m =>
              m.MultiMatch(mm =>
                  mm.Query("abc")
                    .Fields(new Field("source"))
              )
        ).Filter(f => f.Terms(ft => ft.Field(ftf => mySourceIds.Contains(ftf.SourceId))))
     )
   ).Fields(new Field("source_id"))
   .Source(false)
);

```

However, the above code does not generate the `filter` part in the query

```auto
"filter" : {
                "terms" : {
                    "source_id" : ["123","145","158m"]
                }
            }

```

How can I set the terms in the filter using the .net library?

---

<div class="post-metadata">

**Author:** ![jay2021](https://avatars.discourse-cdn.com/v4/letter/j/7feea3/32.png) [@jay2021](https://discuss.elastic.co/u/jay2021)\
**Post date:** [March 16, 2021, 9:44pm UTC](https://discuss.elastic.co/t/how-to-apply-filter-using-nest-net/267423/2 "2021-03-16T21:44:06Z")

</div>

I figured it out

```auto
var mySourceIds = new [] { "123","145","158m" }
var documents = await elasticClient.SearchAsync<MyRecord>(s => 
   s.Query(q =>
       q.Bool(b =>
          b.Must(m =>
              m.MultiMatch(mm =>
                  mm.Query("abc")
                    .Fields(new Field("source"))
              )
        ).Filter(f => f.Terms(ft => ft.Field(new Field("source_id")).Terms(mySourceIds)))
     )
   ).Fields(new Field("source_id"))
   .Source(false)
);

```

---

<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:** [April 13, 2021, 9:44pm UTC](https://discuss.elastic.co/t/how-to-apply-filter-using-nest-net/267423/3 "2021-04-13T21:44:23Z")

</div>

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