# Write genric search query using nest C# client

**URL:** <https://discuss.elastic.co/t/write-genric-search-query-using-nest-c-client/236555>\
**Category:** Elasticsearch\
**Created:** [June 10, 2020, 5:25pm UTC](https://discuss.elastic.co/t/write-genric-search-query-using-nest-c-client/236555 "2020-06-10T17:25:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![DILIP\_SHARMA](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dilip_sharma/32/42672_2.png) [@DILIP\_SHARMA](https://discuss.elastic.co/u/DILIP_SHARMA)\
**Post date:** [June 10, 2020, 5:25pm UTC](https://discuss.elastic.co/t/write-genric-search-query-using-nest-c-client/236555/1 "2020-06-10T17:25:46Z")

</div>

Hi i am trying to use nest client for c# to write search query .  
i know these ways of writing query

```
            var searchresponse = client.Search<test>(s => s
                                        .From(0)
                                        .Size(10)
                                        .Index("sales*")
                                        .Query(q => q
                                             .Match(m => m
                                                .Field(f => f.documenttype)
                                                .Query("salesinvoice")
                                             )
                                        )
                                    );

```

other way of doing same thing

```
            var searchResponse = client.LowLevel.Search<StringResponse>("sales*", @"
            {
                ""from"": 0,
                ""size"": 10,
                ""query"": {
                    ""match"": {
                        ""documenttype"": {
                            ""query"": ""SalesInvoice""
               }
                    }
                }
            }");

```

But i want to create some generic function where my class type can be pass as argument

```
 var searchresponse = client.Search<T>((s => s
                                             .From(0)
                                             .Size(10)
                                             .Index("sales*")
                                             .Query(q => q
                                                  .Match(m => m
                                                     .Field(f => f.documenttype)
                                                     .Query("salesinvoice")
                                                  )
                                             )

```

is there any way where i can do some thing like this , where my class type can be generic for ES client .  
As response i directly want to map with my class type .  
any help or suggestion .  
Thanks .

---

<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:** [June 11, 2020, 7:11am UTC](https://discuss.elastic.co/t/write-genric-search-query-using-nest-c-client/236555/2 "2020-06-11T07:11:59Z")

</div>

You can write a generic method over `Search<T>()`. Member expressions such as `.Field(f => f.documenttype)` would require `T` to be contrained to a generic parameter that has a property getter for a property named `documenttype`. This can be achieved by putting a generic parameter constraint on the generic method such that `T` must implement a specific interface e.g.

```auto
public static ISearchResponse<T> MySearch<T>(IElasticClient client) 
    where T: class, IMyDocument =>
	client.Search<T>(s => s
		.From(0)
		.Size(10)
		.Index("sales*")
		.Query(q => q
			 .Match(m => m
				.Field(f => f.documenttype)
				.Query("salesinvoice")
			 )
		)
	);

public interface IMyDocument
{
	string documenttype { get; } 
}

```

---

<div class="post-metadata">

**Author:** ![DILIP\_SHARMA](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dilip_sharma/32/42672_2.png) [@DILIP\_SHARMA](https://discuss.elastic.co/u/DILIP_SHARMA)\
**Post date:** [June 11, 2020, 8:20am UTC](https://discuss.elastic.co/t/write-genric-search-query-using-nest-c-client/236555/3 "2020-06-11T08:20:16Z")

</div>

Thanks for reply ,  
but still i need to create mysearch function again if my interface changes  
for example one as u given example of IMyDocument .  
i have another index says inventory .  
where i don't have documenttype i may have field called outofstock .  
so i need to create another search function where it will be -  
`where T: class, IStock =>`  
correct me if i m wrong.  
regards  
Dilip

---

<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:** [June 11, 2020, 8:59am UTC](https://discuss.elastic.co/t/write-genric-search-query-using-nest-c-client/236555/4 "2020-06-11T08:59:09Z")

</div>

The question being asked is really a C# design question and not specific to the client.

If a generic method over some `T` performs strongly typed access to members of `T` as in the case of field

> <https://github.com/elastic/elasticsearch-net/blob/f1ffbf260132eb83ea4e7950c1bcf19ef679ee6f/src/Nest/QueryDsl/Abstractions/FieldName/FieldNameQueryDescriptorBase.cs#L24-L25>

then those members need to exist on `T`. The parts of the search request that perform access to members of `T` could be pulled out of the generic method and passed to it. As an example

```auto
public class Document
{
    public int Id { get; set; }
	public int LocalityId { get; set; }
    public IGeoShape LocationShape { get; set; }
}

public static class ElasticClientExtensions
{
	public static ISearchResponse<T> SearchWithMatch<T>(this IElasticClient client, Expression<Func<T, object>> field)
		where T : class =>
		client.Search<T>(s => s
			.From(0)
			.Size(10)
			.Index("sales*")
			.Query(q => q
				.Match(m => m
					.Field(field)
					.Query("salesinvoice")
				)
			)
		);
}

static void Main()
{
	var client = new ElasticClient();
	var response = client.SearchWithMatch<Document>(f => f.LocalityId);
}

```

the field for the match query is provided to the generic `SearchWithMatch<T>()` extension method.

---

<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:** [July 9, 2020, 8:59am UTC](https://discuss.elastic.co/t/write-genric-search-query-using-nest-c-client/236555/5 "2020-07-09T08:59:15Z")

</div>

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