My issue is that when I call .Search(...) the result it returns have all null values set for all the fields (even though the fields have valid data).
MyClass is:
public class Member {
public string ID { get; set; }
public string ClientID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string EmailAddress { get; set; }
public string PasswordHash { get; set; }
public string[] UserRoleIDs { get; set; }
}
I construct the Elastic Client as such:
var node = new Uri("<correct url here>");
var config = new ConnectionSettings(node);
config.BasicAuthentication("elastic", "<correct password here>");
config.DisableDirectStreaming(true);
DB = new ElasticClient(config);
I query Elastic as such:
var res = DB.Search<Member>(s => s
.Index("members")
.Skip(0)
.Take(10)
.Query(q => q
.Term(t => t.EmailAddress, emailAddress)
)
);
The results I get are that the result is 200 OK, there are Hits and Documents, both of which have the correct ID's of the records I am searching for (the query is succeeding as expected).
I can also evaluate the res.ApiCall.ResponseBodyInBytes as a string and I can see the JSON that elastic is returning is correct (the fields have data).
BUT.. res.Document.FirstOrDefault() returns a "Member" class where all the fields are NULL (to stress, the JSON has values for the fields that are null).
I tried to change it to use Dictionary instead of my Member object and that seems to work correctly (E.g. myDictionary["FirstName"] == "Dan").
Also, I tried taking the ID the search result gives me and calling:
var user = DB.Get<Member>(id, i => i.Index("members"));
This has the same end result, all the fields of user.Hit are null :(
The issue seems to be in the casing of properties. I too had the same issue when I was new to working with ES and .Net. If you have indexed it through C# Nest Code, the properties of your Class Member would have been converted from PascalCase to camelCase and would be indexed as camelCase. Since by defalut in ES the fields/properties should be camelCased.
If you have not indexed through C# Nest client and through PostMan or other API clients and if the properties appearing in the JSON have PascalCase, then they would not map to your Object for the same reason mentioned above.
Try changing your properties in the document of the ES from PascalCase to camelCase if you have used PostMan or something similar, for indexing.
Otherwise try to index through C# Nest Client. Here is an example of indexing through C# Nest Client(if you are using Nest v7.x) and if there are multiple Documents to be indexed :
Member[] members = new Member[10];
//some code to add Member class objects to members array.
var bulkIndexResponse = DB.BulkAsync(b => b.("members").IndexMany(members));