elasticsearch C# Elastic Search使用NEST检索数据

lymnna71  于 2023-05-06  发布在  ElasticSearch
关注(0)|答案(1)|浏览(193)

我对Elastic Search / NEST非常陌生,需要一些关于如何查询/过滤数据的帮助。
我正在尝试使用NEST从Elastic Search中退出数据。即使我得到了正确的数据计数,它也没有正确地Map到模型类。有人能帮我确定是什么原因导致了这个问题吗?
这是我在Elastic Search上得到的:

这就是我在NEST上得到的。

代码函数

public async Task<List<WebstoreOperationLogModel>> GetAllWebStoreLogs(int WebStoreId)
{
      var response = await elasticClient.SearchAsync<WebstoreLogModel>(s => s
                                    .Index("webstore-logs")
                                    .Query(q => q.Term("WebstoreId", 
                                     WebStoreId.ToString())));

    var logList = response.Documents.ToList();  

    return mapper.Map<List<WebstoreOperationLogModel>>(logList);
}

模型类

public class WebstoreLogModel
{
    public Guid WebstoreLogId { get; set; }
    public int WebstoreId { get; set; }

    public string LogMessage { get; set; }
    public int LogType { get; set; }
    public bool IsExceptionOccurred { get; set; }

    public DateTime LogDateTime { get; set; }
}
7xllpg7q

7xllpg7q1#

我通过一些研究找到了解决方案。它通过将ElasticProperty属性放在Model类上来解决。

修改模型类

[ElasticsearchType(RelationName = "webstore-logs")]
    public class WebstoreLogModel
    {
        /// <summary>
        /// Gets or sets the webstore log identifier.
        /// </summary>
        [Text(Name = "WebJobLogId")]
        public Guid WebJobLogId { get; set; }

        /// <summary>
        /// Gets or sets the webstore identifier.
        /// </summary>
        [Number(DocValues = false, IgnoreMalformed = true, Coerce = true,Name = "WebstoreId")]
        public int WebstoreId { get; set; }

        /// <summary>
        /// Gets or sets the log message.
        /// </summary>
        [Text(Name = "LogMessage")]
        public string LogMessage { get; set; }

        /// <summary>
        /// Gets or sets the type of the log.
        /// </summary>
        [Number(DocValues = false, IgnoreMalformed = true, Coerce = true,Name = "LogType")]
        public int LogType { get; set; }
  }
}

相关问题