# Outsource parts in queries

**URL:** <https://discuss.elastic.co/t/outsource-parts-in-queries/299450>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [March 11, 2022, 12:59pm UTC](https://discuss.elastic.co/t/outsource-parts-in-queries/299450 "2022-03-11T12:59:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![simobrusch](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/simobrusch/32/102761_2.png) [@simobrusch](https://discuss.elastic.co/u/simobrusch)\
**Post date:** [March 11, 2022, 12:59pm UTC](https://discuss.elastic.co/t/outsource-parts-in-queries/299450/1 "2022-03-11T12:59:29Z")

</div>

So let's say I have the following code in C# using NEST:

```auto
    private ISearchResponse<Country> GetFilteredResultsByIds(ElasticClient client,
        Query query)
    {
        return client.Search<Country>(s => s
            .From(0)
            .Size(10)
            .Query(q => q
                .Nested(n => n
                    .Path(p => p.Customer)
                    .Query(q => q
                        .Term(t => t
                            .Field(f => f.Customer.CustomerId).Value(query.CustomerId)))
                ) && q
                .Nested(n => n
                    .Path(p => p.Person)
                    .Query(q => q
                        .Term(t => t
                            .Field(f => f.Person.ServiceId).Value(query.ServiceId))))));
    }

```

is it possible to outsource parts of the search query to external classes/parameters? For example the Customer and Person part to be able to reuse this in another query.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [March 17, 2022, 10:01am UTC](https://discuss.elastic.co/t/outsource-parts-in-queries/299450/2 "2022-03-17T10:01:03Z")

</div>

Take a look at search templates, where you only have to provide the parameters for your searches. See [Search templates | Elasticsearch Guide [8.1] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/8.1/search-template.html)

---

<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 14, 2022, 10:01am UTC](https://discuss.elastic.co/t/outsource-parts-in-queries/299450/3 "2022-04-14T10:01:26Z")

</div>

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