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

[BUG] Cannot output AggregateDictionary as JSON

Open winzig opened this issue 1 year ago • 1 comments

What is the bug?

I'm trying to output an AggregateDictionary to the end-user browser as JSON. When I do this, all of the properties of the object seem to be at their default state (nulls, 0s, Items are just empty JSON objects instead of IBuckets, etc).

How can one reproduce the bug?

I'm doing a simple Terms based aggregate request in my query:

new AggregationDictionary()
{
    ["DiscussionTypes"] = new AggregationContainer
    {
        Terms = new TermsAggregation("CommunityTypeId")
        {
            Field = "CommunityTypeId",
            Size = 1000
        }
    }
}

The JSON coming back from OpenSearch makes sense to me:

"aggregations": {
    "DiscussionTypes": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": 4,
          "doc_count": 364
        },
        {
          "key": 3,
          "doc_count": 88
        },
        {
          "key": 1,
          "doc_count": 28
        },
        {
          "key": 6,
          "doc_count": 13
        },
        {
          "key": 7,
          "doc_count": 13
        },
        {
          "key": 8,
          "doc_count": 12
        },
        {
          "key": 12,
          "doc_count": 12
        },
        {
          "key": 2,
          "doc_count": 6
        },
        {
          "key": 13,
          "doc_count": 3
        },
        {
          "key": 9,
          "doc_count": 2
        },
        {
          "key": 5,
          "doc_count": 1
        }
      ]
    }
  }

Then I return the Aggregations property from the ISearchResponse that I get back from the OpenSearch client to the client, something I do with C# objects all the time in my web app, and it comes out looking like this below. Notice that there are 11 empty Items objects, matching the 11 results in the JSON data above.

"DiscussionTypes": {
    "AfterKey": null,
    "BgCount": 0,
    "DocCount": 0,
    "DocCountErrorUpperBound": 0,
    "Items": [
        {},
        {},
        {},
        {},
        {},
        {},
        {},
        {},
        {},
        {},
        {}
    ],
    "Meta": null,
    "SumOtherDocCount": 0,
    "AutoInterval": null
}

Typically when I see behavior like this in my own code, it's because I've over-zealously put a [JsonIgnore] on a property, or have the property defined with a private set. Is that possibly the problem here within the OS code base?

What is the expected behavior?

Outputting an AggregateDictionary should export all of the public fields via JSON.

What is your host/environment?

OpenSearch.Client 1.4.0 connected to OpenSearch 2.7 running in AWS OpenSearch Service (also tried it with Client 1.3.0 and OpenSearch 2.5).

winzig avatar Aug 16 '23 21:08 winzig

Hi @winzig, this is not exactly a bug as the types within the client are not written with the intention or expectation of being (de)serializable outside of the client. This would not be feasible to implement or maintain due to the extent of the client and differing behaviours between the various potential JSON implementations (Newtonsoft, System.Text, Utf8Json, etc).

I think it would be useful if you could provide a bit more information about your usecase. There are mechanisms to get the raw json response or a dynamic object with the low-level client if you're not planning on doing much manipulation in C#.

Xtansia avatar Aug 18 '23 03:08 Xtansia