1

Right now, elastic search is adding empty values as shown in the image, I would like to see the complete json object added inside the elastic search as document so I can search on it

Code


    public async Task<CreateResponse> CreateDocumentAndIndex<T>(T document, string index, Type objectType) where T : class
            {
                _client = CreateElasticClient();
      var serializedObject = JsonConvert.SerializeObject(document, Formatting.None,
                       new JsonSerializerSettings()
                       {
                           ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                       });
          var elasticValues = new ElasticSeachValues
                {
                    values = JObject.Parse(serializedObject)
                };
    
                Console.WriteLine(elasticValues.values);
    
                var getIndexResponse = await _client.IndexAsync(elasticValues, idx => idx.Index(index.ToLower()));
                }
    }

      public class ElasticSeachValues 
        {
            public JObject values { get; set; }
        }

Elastic Values

{
  "CompanyId": "96af84f8-6cc0-46d6-63ae-08d9c3e170f9",
  "Company": {
    "CompanyName": "string",
    "Country": "string",
    "Street": "string",
    "PostalCode": "string",
    "VATId": "string",
    "TeamMembers": [
      {
        "CompanyId": "96af84f8-6cc0-46d6-63ae-08d9c3e170f9",
        "UserId": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
        "TeamMemberRoles": [],
        "CreatedAt": "2021-12-20T12:52:10.2748443-05:00",
        "ModifiedAt": "2021-12-20T12:52:10.2748443-05:00",
        "CreatedById": "00000000-0000-0000-0000-000000000000",
        "ModifiedById": "00000000-0000-0000-0000-000000000000",
        "Version": 1,
        "Id": "3fa85f64-5717-4562-b3fc-2c963f66afa6"
      }
    ],
    "CompanyInvitations": [
      {
        "IsAccepted": true,
        "IsInvitationSent": true,
        "UserId": "6ceed528-5764-4a5f-43a1-08d9be698212",
        "Email": "nirjhar18@gmail.com",
        "RoleId": "71fa9290-23e6-49e4-8bf9-b0f1083793c8",
        "Role": {
          "Title": "Owner",
          "Key": "OWNER",
          "CreatedAt": "0001-01-01T00:00:00-05:00",
          "ModifiedAt": "2021-12-20T12:52:10.2750237-05:00",
          "CreatedById": "00000000-0000-0000-0000-000000000000",
          "ModifiedById": "00000000-0000-0000-0000-000000000000",
          "Version": 5,
          "Id": "71fa9290-23e6-49e4-8bf9-b0f1083793c8"
        },
        "CompanyId": "96af84f8-6cc0-46d6-63ae-08d9c3e170f9",
        "AcceptedAt": "2021-12-20T12:52:10.2239198-05:00",
        "ExpiresAt": "2021-12-20T12:52:10.2235813-05:00",
        "AuthorizationCode": "ee65e028-dbc0-4994-a01e-a156f2dc8c36",
        "CreatedAt": "2021-12-20T12:52:10.2748449-05:00",
        "ModifiedAt": "2021-12-20T12:52:10.2748449-05:00",
        "CreatedById": "00000000-0000-0000-0000-000000000000",
        "ModifiedById": "00000000-0000-0000-0000-000000000000",
        "Version": 1,
        "Id": "b871455b-f0c4-453d-d6d5-08d9c3e1724b"
      }
    ],
    "Size": 0,
    "CreatedAt": "2021-12-20T12:52:10.2748435-05:00",
    "ModifiedAt": "2021-12-20T12:52:10.2748435-05:00",
    "CreatedById": "00000000-0000-0000-0000-000000000000",
    "ModifiedById": "00000000-0000-0000-0000-000000000000",
    "Version": 1,
    "Id": "96af84f8-6cc0-46d6-63ae-08d9c3e170f9"
  },
  "UserId": "00000000-0000-0000-0000-000000000000",
  "TeamMemberRoles": [
    {
      "Title": "Owner",
      "Key": "OWNER",
      "CreatedAt": "0001-01-01T00:00:00-05:00",
      "ModifiedAt": "2021-12-20T12:52:10.2750237-05:00",
      "CreatedById": "00000000-0000-0000-0000-000000000000",
      "ModifiedById": "00000000-0000-0000-0000-000000000000",
      "Version": 5,
      "Id": "71fa9290-23e6-49e4-8bf9-b0f1083793c8"
    }
  ],
  "CreatedAt": "2021-12-20T12:52:10.2748398-05:00",
  "ModifiedAt": "2021-12-20T12:52:10.2748398-05:00",
  "CreatedById": "00000000-0000-0000-0000-000000000000",
  "ModifiedById": "00000000-0000-0000-0000-000000000000",
  "Version": 1,
  "Id": "eaf48b09-3db0-4141-6d33-08d9c3e170eb"
}

I am trying to add this in elastic search as document with an index. IndexAsync method returns 201 and when I review it in Kibana, it shows empty results as below:How can I add complete object/class?

enter image description here

   private ElasticClient CreateElasticClient()
        {
            var settings = new ConnectionSettings(new Uri("http://localhost:9200/"));

            var client = new ElasticClient(settings);          
            return client;
        }

This client is just an elastic search client from Nest Library https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/nest.html

Learn AspNet
  • 1,192
  • 3
  • 34
  • 74
  • 1
    The fact that your `JObject` is getting serialized as a collection of empty arrays indicates that `_client.IndexAsync()` is using a different serializer than Json.NET, which only recognizes `JObject` as some sort of enumerable. This is exactly what System.Text.Json does with `JObject,` see [Issue with serializing 'object' with System.Text.Json](https://stackoverflow.com/q/65688027/3744182). But I don't know what serializer the client returned by `CreateElasticClient()` uses, can you provide a doc link or [mcve]? – dbc Dec 20 '21 at 19:00
  • 1
    To check to see whether it is using System.Text.Json, declare `values` as `public JsonElement values` and deserialize using `values = System.Text.Json.JsonSerializer.Deserialize(serializedObject)`. If the problem resolves itself, that proves `CreateElasticClient()` uses System.Text.Json. – dbc Dec 20 '21 at 19:01
  • Now it return values = valuekind = { all the json} and elastic search does not like that – Learn AspNet Dec 20 '21 at 19:12
  • 1
    Visual Studio may not format `JsonElement` very well so you can't trust what is being shown in the watch window. Have you tested what `_client` actually does with `JsonElement`? But if `_client` really is doing the wrong thing with `JsonElement` then it must be using some other serializer besides Json.NET or System.Text.Json. Can you provide a doc link for whatever type is returned what `CreateElasticClient()` returns? – dbc Dec 20 '21 at 19:14
  • @dbc CreateElasticClient is just a private function. I updated the question with the function that returns Elastic Client from NEST library I changed that you wanted and now kibana shows this. It has valuekind in the object now https://ibb.co/9TdRJHr – Learn AspNet Dec 20 '21 at 19:20

1 Answers1

2

You need to also reference the NEST.JsonNetSerializer nuget package and add the the JsonNetSerializer in the connection settings like below:


     var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
    
                var connectionSettings =
          new ConnectionSettings(pool, sourceSerializer: JsonNetSerializer.Default);
    
                var client = new ElasticClient(connectionSettings);          
                return client;

Nirjhar Vermani
  • 1,215
  • 8
  • 17