Elastic Search adding empty values for all the documents. How can I add complete object/class?(弹性搜索,为所有文档添加空值。如何添加完整的对象/类?)
问题描述
现在,Elastic Search正在添加空值,如图所示,我希望看到在Elastic Search内部添加完整的json对象作为文档,这样我就可以对其进行搜索
代码
    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; }
        }
弹性值
{
  "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"
}
我尝试在Elastic Search中将其添加为带索引的文档。IndexAsync方法返回201,当我在Kibana中查看它时,它显示空结果,如下所示:如何添加完整的对象/类?
   private ElasticClient CreateElasticClient()
        {
            var settings = new ConnectionSettings(new Uri("http://localhost:9200/"));
            var client = new ElasticClient(settings);          
            return client;
        }
此客户端只是来自Nest Library的弹性搜索客户端https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/nest.html
推荐答案
您还需要引用NEST.JsonNetSerializer Nuget包,并在如下所示的连接设置中添加JsonNetSerializer:
     var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
    
                var connectionSettings =
          new ConnectionSettings(pool, sourceSerializer: JsonNetSerializer.Default);
    
                var client = new ElasticClient(connectionSettings);          
                return client;
                        这篇关于弹性搜索,为所有文档添加空值。如何添加完整的对象/类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:弹性搜索,为所有文档添加空值。如何添加完整的对象/类?
				
        
 
            
        - 如何用自己压缩一个 IEnumerable 2022-01-01
 - MoreLinq maxBy vs LINQ max + where 2022-01-01
 - Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
 - 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
 - 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
 - 输入按键事件处理程序 2022-01-01
 - C#MongoDB使用Builders查找派生对象 2022-09-04
 - 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
 - C# 中多线程网络服务器的模式 2022-01-01
 - WebMatrix WebSecurity PasswordSalt 2022-01-01
 
						
						
						
						
						
				
				
				
				