Skip to content

Wrong serialization of TermsQuery, MinimumShouldMatch and TrackTotalHits #8387

@andersosthus

Description

@andersosthus

Elastic.Clients.Elasticsearch version: 8.15.9

Elasticsearch version: N/A

.NET runtime version: 8.0

Operating system version: Win 11

Description of the problem including expected versus actual behavior:
Term in TermsQuery does not serialize properly.
MinimumShouldMatch in BoolQuery does not serialize properly.
TrackTotalHits in SearchRequest does not serialize properly.

Steps to reproduce:

using System.Text;
using Elastic.Clients.Elasticsearch;
using Elastic.Clients.Elasticsearch.Core.Search;
using Elastic.Clients.Elasticsearch.QueryDsl;
using Elastic.Transport;

var client = new ElasticsearchClient();

var request = new SearchRequest("indexName")
{
    Query = new BoolQuery
    {
        Should =
        [
            new TermsQuery
            {
                Field = "abcd",
                Term = new TermsQueryField(new FieldValue[]{"a", "b"}),
            },
        ],
        MinimumShouldMatch = MinimumShouldMatch.Fixed(1)
    },
    TrackTotalHits = new TrackHits(false)
};

using var ms = new MemoryStream();
client.Transport.Configuration.SourceSerializer.Serialize(request, ms, SerializationFormatting.Indented);
var jsonPayload = Encoding.UTF8.GetString(ms.ToArray());

Console.WriteLine(jsonPayload);

Expected behavior
Expected the following output:

{
  "query": {
    "bool": {
      "should": {
        "terms": {
          "abcd": ["a", "b"]
        }
      },
      "minimum_should_match": 1
    }
  },
  "track_total_hits": false
}

but this is the actual produces JSON:

{
  "query": {
    "bool": {
      "should": {
        "terms": {
          "abcd": {}
        }
      },
      "minimum_should_match": {}
    }
  },
  "track_total_hits": {}
}

Other notes:
Using new TrackHits(1) for TrackTotalHits gives the same output.

We've worked around it for now with custom serializers, just reporting it here to make sure you're aware of it.

Sidenote, for TermsQuery, shouldn't Term be renamed to Terms to align semantically since the use case is for one or many terms? Also, thinking about DX, TermsQuery.Term(s) should allow us to just pass in a collection in stead of a TermsQueryField which feels a lot more clunky.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions