現在,彈性搜索正在添加空值,如圖所示,我希望將完整的 json 物件添加到彈性搜索中作為檔案,以便我可以對其進行搜索
代碼
public async Task<CreateResponse> CreateDocumentAndIndex<T>(T document, string index, Type objectType) where T : class
{
_client = CreateElasticClient();
var serializedObject = JsonConvert.SerializeObject(document, Formatting.None,
new JsonSerializerSettings()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
var elasticValues = new ElasticSeachValues
{
values = JObject.Parse(serializedObject)
};
Console.WriteLine(elasticValues.values);
var getIndexResponse = await _client.IndexAsync(elasticValues, idx => idx.Index(index.ToLower()));
}
}
public class ElasticSeachValues
{
public JObject values { get; set; }
}
彈性值
{
"CompanyId": "96af84f8-6cc0-46d6-63ae-08d9c3e170f9",
"Company": {
"CompanyName": "string",
"Country": "string",
"Street": "string",
"PostalCode": "string",
"VATId": "string",
"TeamMembers": [
{
"CompanyId": "96af84f8-6cc0-46d6-63ae-08d9c3e170f9",
"UserId": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
"TeamMemberRoles": [],
"CreatedAt": "2021-12-20T12:52:10.2748443-05:00",
"ModifiedAt": "2021-12-20T12:52:10.2748443-05:00",
"CreatedById": "00000000-0000-0000-0000-000000000000",
"ModifiedById": "00000000-0000-0000-0000-000000000000",
"Version": 1,
"Id": "3fa85f64-5717-4562-b3fc-2c963f66afa6"
}
],
"CompanyInvitations": [
{
"IsAccepted": true,
"IsInvitationSent": true,
"UserId": "6ceed528-5764-4a5f-43a1-08d9be698212",
"Email": "[email protected]",
"RoleId": "71fa9290-23e6-49e4-8bf9-b0f1083793c8",
"Role": {
"Title": "Owner",
"Key": "OWNER",
"CreatedAt": "0001-01-01T00:00:00-05:00",
"ModifiedAt": "2021-12-20T12:52:10.2750237-05:00",
"CreatedById": "00000000-0000-0000-0000-000000000000",
"ModifiedById": "00000000-0000-0000-0000-000000000000",
"Version": 5,
"Id": "71fa9290-23e6-49e4-8bf9-b0f1083793c8"
},
"CompanyId": "96af84f8-6cc0-46d6-63ae-08d9c3e170f9",
"AcceptedAt": "2021-12-20T12:52:10.2239198-05:00",
"ExpiresAt": "2021-12-20T12:52:10.2235813-05:00",
"AuthorizationCode": "ee65e028-dbc0-4994-a01e-a156f2dc8c36",
"CreatedAt": "2021-12-20T12:52:10.2748449-05:00",
"ModifiedAt": "2021-12-20T12:52:10.2748449-05:00",
"CreatedById": "00000000-0000-0000-0000-000000000000",
"ModifiedById": "00000000-0000-0000-0000-000000000000",
"Version": 1,
"Id": "b871455b-f0c4-453d-d6d5-08d9c3e1724b"
}
],
"Size": 0,
"CreatedAt": "2021-12-20T12:52:10.2748435-05:00",
"ModifiedAt": "2021-12-20T12:52:10.2748435-05:00",
"CreatedById": "00000000-0000-0000-0000-000000000000",
"ModifiedById": "00000000-0000-0000-0000-000000000000",
"Version": 1,
"Id": "96af84f8-6cc0-46d6-63ae-08d9c3e170f9"
},
"UserId": "00000000-0000-0000-0000-000000000000",
"TeamMemberRoles": [
{
"Title": "Owner",
"Key": "OWNER",
"CreatedAt": "0001-01-01T00:00:00-05:00",
"ModifiedAt": "2021-12-20T12:52:10.2750237-05:00",
"CreatedById": "00000000-0000-0000-0000-000000000000",
"ModifiedById": "00000000-0000-0000-0000-000000000000",
"Version": 5,
"Id": "71fa9290-23e6-49e4-8bf9-b0f1083793c8"
}
],
"CreatedAt": "2021-12-20T12:52:10.2748398-05:00",
"ModifiedAt": "2021-12-20T12:52:10.2748398-05:00",
"CreatedById": "00000000-0000-0000-0000-000000000000",
"ModifiedById": "00000000-0000-0000-0000-000000000000",
"Version": 1,
"Id": "eaf48b09-3db0-4141-6d33-08d9c3e170eb"
}
我試圖在彈性搜索中將此添加為帶有索引的檔案。IndexAsync 方法回傳 201,當我在 Kibana 中查看它時,它顯示空結果如下:如何添加完整的物件/類?

private ElasticClient CreateElasticClient()
{
var settings = new ConnectionSettings(new Uri("http://localhost:9200/"));
var client = new ElasticClient(settings);
return client;
}
這個客戶端只是一個來自 Nest 庫的彈性搜索客戶端https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/nest.html
uj5u.com熱心網友回復:
您還需要參考 NEST.JsonNetSerializer nuget 包并在連接設定中添加 JsonNetSerializer,如下所示:
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var connectionSettings =
new ConnectionSettings(pool, sourceSerializer: JsonNetSerializer.Default);
var client = new ElasticClient(connectionSettings);
return client;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/387903.html
