首页 > 解决方案 > ElasticSearch PostData 序列化插入不起作用

问题描述

这是我的发布方法

[HttpPost]
    public IActionResult InsertEmployee([FromForm]Employee model)
    {
      
       
        var Employee = new Employee
        {
            EmpId = 3,
            Name = "asadasasad",
            Country = "pak",
            Gender = false,
            StartDate = DateTime.Now.Date,
            MultiCheck = "3,2,1"
        };

        PostData fromObject = PostData.Serializable(Employee);
        _elasticClient.Index(fromObject, i => i
               .Index("employee"));

        string message = "Failed to create the product. Please try again";
        return Content(message);
    }

使用此方法时,它会增加值但不会显示在记录上

http://localhost:9200/employee/_search?typed_keys=true?size=1000

此查询返回此响应

这是响应

标签: c#elasticsearch

解决方案


在 Elastic Search Post Data insert 中,您首先需要序列化将其转储到类中的对象,然后对其进行反序列化,然后使用这种方式插入到 elasticsearch

                        var data = JsonConvert.DeserializeObject<NetConnectTransactionDTO>(deserialize);
                        var Response = _elasticClient.Index(data, i => i
                          .Index(NetConnectTransactionIndex));

推荐阅读