Search code examples
c#elasticsearchnest

C# Elastic Search retrieving data with NEST


I'm pretty new to Elastic Search / NEST and need some help with how to be able to query/filter my data.

I am trying to retire data from Elastic Search using NEST. Even though I am getting the correct count of data it has not mapped to the model class correctly. Could someone help me to identify what makes went wrong this?

This is what I am getting on Elastic Search:

enter image description here

This is what am getting on NEST. enter image description here

Code - function

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);
}

Model Class

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; }
}

Solution

  • I found the solution with some research. It's solved by putting ElasticProperty attributes on the Model class.

    Modified model class

        [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; }
      }
    }