# Using Sort API via Elastic.Clients.Elasticsearch 8.1.0 .NET

**URL:** <https://discuss.elastic.co/t/using-sort-api-via-elastic-clients-elasticsearch-8-1-0-net/330908>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [April 27, 2023, 7:12am UTC](https://discuss.elastic.co/t/using-sort-api-via-elastic-clients-elasticsearch-8-1-0-net/330908 "2023-04-27T07:12:15Z")\
**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:** [April 27, 2023, 7:12am UTC](https://discuss.elastic.co/t/using-sort-api-via-elastic-clients-elasticsearch-8-1-0-net/330908/1 "2023-04-27T07:12:15Z")

</div>

I have an Elasticsearch cluster, which contains an index called `persons`. I want to query and sort the documents of the index using the latest Elasticsearch client for .NET (Elastic.Clients.Elasticsearch 8.1.0 .NET). The query I want to execute should like this (note: `persons` index contains documents with a `name` field):

```auto
GET persons/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "name.keyword": {
        "order": "asc"
      }
    }
  ]
}

```

Currently I am using this code to execute the above shown query via C#:

```auto
var response = await _elasticsearchClient.SearchAsync<Person>(s => s
    .Index("persons")
    .Query(q => q.MatchAll())
    .Sort(so => so
        .Field(f => f.Name.Suffix("keyword"))
        .Score(sc => sc.Order(SortOrder.Asc))));

```

Unfortunately, this query does not actually sort the documents. Therefore, I want to ask how I could represent the above mentioned query using the 8.1.0 .NET client. The way it used to work in the client with version 7.17 does not work anymore ([Sort Usage 7.17](https://www.elastic.co/guide/en/elasticsearch/client/net-api/7.17/sort-usage.html)).

---

<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:** [April 27, 2023, 7:58am UTC](https://discuss.elastic.co/t/using-sort-api-via-elastic-clients-elasticsearch-8-1-0-net/330908/2 "2023-04-27T07:58:13Z")

</div>

Hi, @felix-stnr.

Your code is close but is actually setting a score sort. To achieve your goal, you need to provide the `FieldSort` after providing the field name.

```auto
var response = await client.SearchAsync<Person>(s => s
	.Index("persons")
	.Query(q => q.MatchAll())
	.Sort(so => so
		.Field(f => f.Name.Suffix("keyword"), new FieldSort { Order = SortOrder.Asc })));

```

---

<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:** [April 27, 2023, 9:37am UTC](https://discuss.elastic.co/t/using-sort-api-via-elastic-clients-elasticsearch-8-1-0-net/330908/3 "2023-04-27T09:37:32Z")

</div>

Thanks, that did the trick for me!

---

<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:** [May 25, 2023, 9:38am UTC](https://discuss.elastic.co/t/using-sort-api-via-elastic-clients-elasticsearch-8-1-0-net/330908/4 "2023-05-25T09:38:32Z")

</div>

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