elasticsearch-net icon indicating copy to clipboard operation
elasticsearch-net copied to clipboard

Provide a way to convert QueryDescriptor to Query (Elastic.Clients.Elasticsearch, 8.11.0)

Open petr-horny-bsshop opened this issue 5 months ago • 2 comments

Is your feature request related to a problem? Please describe. There is ony single overload of FiltersAggregationDescriptor.Filter method which accepts Buckets<Query>:

public FiltersAggregationDescriptor<TDocument> Filters(Elastic.Clients.Elasticsearch.Aggregations.Buckets<Elastic.Clients.Elasticsearch.QueryDsl.Query>? filters)

This means that only Query can be used in the filter, not QueryDescriptor using FluentAPI.

What's worse is that we can't use FluentAPI in the whole path and we are stuck with Query object.

There is a related issue described here https://discuss.elastic.co/t/migrating-from-net-nest-client-to-v8-elastic-clients-elasticsearch-net-client/334959

Describe the solution you'd like Provide a .ToQuery() conversion method

var queryDescriptor = new QueryDescriptor<Product>();
queryDescriptor.Term(p => p.Color, "red");

Query query = queryDescriptor.ToQuery();

OR

Create new overload for FiltersAggregationDescriptor.Filter

public FiltersAggregationDescriptor<TDocument> Filters(Elastic.Clients.Elasticsearch.Aggregations.Buckets<Elastic.Clients.Elasticsearch.QueryDsl.QueryDescriptor<TDocument>? filters)

The former is preferred because it is more versatile.

Additional context For now we used this workaround:

private Query ToQuery<TDocument>(QueryDescriptor<TDocument> queryDescriptor)
{
    using var ms = new MemoryStream();
    EsClient.RequestResponseSerializer.Serialize(queryDescriptor, ms, SerializationFormatting.Indented);

    ms.Position = 0;
    var query = EsClient.RequestResponseSerializer.Deserialize<Query>(ms);
    
    return query;
}

petr-horny-bsshop avatar Jan 23 '24 06:01 petr-horny-bsshop