# How to create dynamic Query DSL for Includes

**URL:** <https://discuss.elastic.co/t/how-to-create-dynamic-query-dsl-for-includes/333581>\
**Category:** Elasticsearch\
**Created:** [May 16, 2023, 2:38pm UTC](https://discuss.elastic.co/t/how-to-create-dynamic-query-dsl-for-includes/333581 "2023-05-16T14:38:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Koi\_Kin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/koi_kin/32/119836_2.png) [@Koi\_Kin](https://discuss.elastic.co/u/Koi_Kin)\
**Post date:** [May 16, 2023, 2:38pm UTC](https://discuss.elastic.co/t/how-to-create-dynamic-query-dsl-for-includes/333581/1 "2023-05-16T14:38:20Z")

</div>

I have this query:

```auto
.Search<Person>("person", s => s
    .Index("person")
    .Source(s => s
        .Includes(i => i
            .Fields(
                f => f.Id,
            )
        )
    )
    .Query(q => q
        .Match(m => m
            .Field(personname)
            .Query(search)
        )
    )
)

```

and I try to make include dynamic with if-else in the code below:

```auto
public Func<SourceFilterDescriptor<Person>, ISourceFilter> PersonIncludeBuilder
{
    var fields = new List<Func<FieldsDescriptor<Person>, FieldsDescriptor<Person>>>();
    fields.Add(f => f.Field(f => f.Id));

    if(includePersonName)
    {
        fields.Add(f => f.Field(f => f.Name));
    }

    return //How to convert to Func<SourceFilterDescriptor<Person>, ISourceFilter> or are they other way to do it?;
}

```

Am I on the right track or are they otherways to do it?

---

<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 13, 2023, 2:38pm UTC](https://discuss.elastic.co/t/how-to-create-dynamic-query-dsl-for-includes/333581/2 "2023-06-13T14:38:41Z")

</div>

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