Reputation: 3
We connect to an ElasticSearch from a third party to get some data with the NEST nuget package. However if we use the GET<> call the response is not deserialized in a strong typed POCO. The HTTP call is successful and in the body there is a json object with data.
var result = _elasticSearchClient.Get<Contractor>(188153, request => request
.Index("{index_name}")
.Type("{type_name}"));
--POCO. Naming of the property as in the json message
public class Contractor
{
public int contractor_id { get; set; }
public string type { get; set; }
public string type_description { get; set; }
public string contractor_code { get; set; }
public string name { get; set; }
...
}
A call with HttpClient and JsonCovert to Deserialize the object works
var response = Task.Run(() => _httpClient.GetAsync(url)).Result;
var responseMessage = Task.Run(() => response.Content.ReadAsStringAsync()).Result;
var contractor = JsonConvert.DeserializeObject<Contractor>(responseMessage);
Configuration of the ElasticSearchClient
var url = new Uri($"{BaseUrl}");
var connectionSettings = new ConnectionSettings(url);
connectionSettings.ClientCertificate(GetCertificateFromStore());
connectionSettings.BasicAuthentication(Username, Password);
connectionSettings.ThrowExceptions();
connectionSettings.PrettyJson();
connectionSettings.DisableDirectStreaming();
connectionSettings.DefaultFieldNameInferrer(p => p);
var elasticSearchClient = new ElasticClient(connectionSettings);
DebugInformation: Valid NEST response built from a successful low level call on GET: /{index_name}/{type_name}/188153?pretty=true
Audit trail of this API call: - [1] HealthyResponse: Node: https://{baseUrl}/ Took: 00:00:01.0835021 Request: Request stream not captured or already read to completion by serializer. Set DisableDirectStreaming() on ConnectionSettings to force it to be set on the response.
Response:
{
"_index" : "{index_name}",
"_type" : "{type_name}",
"_id" : "188153",
"_version" : 1,
"found" : true,
"_source" : {
"contractor" : {
"contractor_id" : 188153,
"type" : "OVE",
"type_description" : "Overig",
"contractor_code" : "RES0000826",
"name" : "OrganizationName",
}
}
NEST NuGet package 5.6.0
Elasticsearch version 6.0.0
What am I missing?
Upvotes: 0
Views: 8939
Reputation: 125488
It's not deserialized by NEST because the _source
document in Elasticsearch is an object with one property, "contractor"
, whose value is an object containing properties.
In order to deserialize correctly, the _source
document in Elasticsearch would need to be
"_source" : {
"contractor_id" : 188153,
"type" : "OVE",
"type_description" : "Overig",
"contractor_code" : "RES0000826",
"name" : "OrganizationName"
}
or alternatively, the POCO should be something like
public class ContractorWrapper //choose any type name you like :)
{
public Contractor contractor { get;set; }
}
to match the JSON structure.
Upvotes: 2