elastic / elasticsearch-net

This strongly-typed, client library enables working with Elasticsearch. It is the official client maintained and supported by Elastic.
https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/index.html
Apache License 2.0
3.57k stars 1.15k forks source link

8.15.6 - Sorting Feedback #8350

Open niemyjski opened 2 weeks ago

niemyjski commented 2 weeks ago

Elastic.Clients.Elasticsearch version: 8.15.6

Elasticsearch version: 8.15.1

.NET runtime version: 8.x

Operating system version: Any

Description of the problem including expected versus actual behavior:

Sorting with the new client is insanely verbose and tedious, to the point I don't even want to convert the tests we have...

Example 1

// Previous
Client.Search<MyType>(d => d.Index(index).Aggregations(a => a
            .Terms("terms_field1", t => t
                .Field("field1.keyword")
                .Order(o => o.Descending("cardinality_field4"))));

// New
await Client.SearchAsync<MyType>(d => d.Index(index).Aggregations(a => a
                .Add("terms_field1", a1 => a1
                    .Terms(t => t.Field("field1.keyword").Order(new List<KeyValuePair<Field, SortOrder>> { new ("cardinality_field4", SortOrder.Desc) })))));

Example 2

// Previous
if (termsAggregation != null && (child.Prefix == "-" || child.Prefix == "+"))
            {
                if (termsAggregation.Order == null)
                    termsAggregation.Order = new List<TermsOrder>();

                termsAggregation.Order.Add(new TermsOrder
                {
                    Key = ((IAggregation)aggregation).Name,
                    Order = child.Prefix == "-" ? SortOrder.Descending : SortOrder.Ascending
                });
            }

// New
            if (termsAggregation != null && child.Prefix is "-" or "+")
            {
                termsAggregation.Order ??= new List<KeyValuePair<Field, SortOrder>>();
                termsAggregation.Order.Add(new KeyValuePair<Field, SortOrder>(aggregation.Name, child.Prefix == "-" ? SortOrder.Desc : SortOrder.Asc));
            }

Example 3 (I don't even want to think about converting)

await Client.SearchAsync<MyType>(d => d.Index(index).Sort(s => s
            .Field(f => f.Field(new Field("field3")).Ascending().UnmappedType(FieldType.GeoPoint))
            .Field(f => f.Field(new Field("field1.keyword")).Descending().UnmappedType(FieldType.Keyword))
            .Field(f => f.Field(new Field("field2.sort")).Descending().UnmappedType(FieldType.Keyword))
            .Field(f => f.Field(new Field("field3")).Descending().UnmappedType(FieldType.GeoPoint))
            .Field(f => f.Field(new Field("field4")).Ascending().UnmappedType(FieldType.Long))
            .Field(f => f.Field(new Field("field5")).Ascending().UnmappedType(FieldType.Date))
            .Field(f => f.Field(new Field("field3")).Ascending().UnmappedType(FieldType.GeoPoint))
        ));

Expected behavior

Sorting should be a very common operation and easy todo.

Reference: https://github.com/FoundatioFx/Foundatio.Parsers/pull/84