弹性搜索为所有文档添加空值。如何添加完整的对象/类?

Posted

技术标签:

【中文标题】弹性搜索为所有文档添加空值。如何添加完整的对象/类?【英文标题】:Elastic Search adding empty values for all the documents. How can I add complete object/class? 【发布时间】:2022-01-22 08:03:19 【问题描述】:

现在,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"

我正在尝试在弹性搜索中将其添加为带有索引的文档。 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 的弹性搜索客户端

【问题讨论】:

您的JObject 被序列化为空数组的集合这一事实表明_client.IndexAsync() 使用的序列化程序与Json.NET 不同,后者仅将JObject 识别为某种可枚举.这正是 System.Text.Json 对JObject, 所做的,请参阅Issue with serializing 'object' with System.Text.Json。但是不知道CreateElasticClient()返回的客户端用的是什么序列化器,能不能给个doc链接或者minimal reproducible example? 要检查它是否在使用 System.Text.Json,请将 values 声明为 public JsonElement values 并使用 values = System.Text.Json.JsonSerializer.Deserialize&lt;JsonElement&gt;(serializedObject) 进行反序列化。如果问题自行解决,则证明 CreateElasticClient() 使用 System.Text.Json。 现在它返回 values = valuekind = all the json 并且弹性搜索不喜欢那样 Visual Studio 可能无法很好地格式化JsonElement,因此您无法相信监视窗口中显示的内容。您是否测试过_clientJsonElement 的实际作用?但如果_client 真的对JsonElement 做错了事,那么它一定是在使用除Json.NET 或System.Text.Json 之外的其他序列化程序。您能否为CreateElasticClient() 返回的任何类型提供文档链接? @dbc CreateElasticClient 只是一个私有函数。我用从 NEST 库返回弹性客户端的函数更新了问题,我改变了你想要的,现在 kibana 显示了这个。它现在在对象中有 valuekind ibb.co/9TdRJHr 【参考方案1】:

您还需要引用 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;

【讨论】:

以上是关于弹性搜索为所有文档添加空值。如何添加完整的对象/类?的主要内容,如果未能解决你的问题,请参考以下文章

弹性搜索:索引具有空值的日期字段

在弹性搜索中搜索具有空/空对象字段的文档

如何将排序添加到弹簧数据弹性搜索

弹性盒布局

在 MongoDB 中添加唯一索引忽略空值

如何使用类成员搜索对象指针向量?