问题描述
在我的 .net 核心应用程序中,我尝试使用 nesT 客户端将我的请求保存到 elasticsearch。问题是它总是覆盖数据,最后我只收到一个最新请求。
我的配置如下:
public static void AddElasticsearch(this IServiceCollection services)
{
var url = "elasticurl";
var defaultIndex = "taikunrequests";
var settings = new ConnectionSettings(new Uri(url))
.DefaultIndex(defaultIndex);
AddDefaultMappings(settings);
var client = new Elasticclient(settings);
services.AddSingleton<IElasticclient>(client);
CreateIndex(client,defaultIndex);
}
private static void AddDefaultMappings(ConnectionSettings settings)
{
settings.
DefaultMappingFor<TaikunRequest>(m => m
.Ignore(p => p.Id)
);
}
private static void CreateIndex(IElasticclient client,string indexName)
{
client.Indices.Create(indexName,index => index.Map<TaikunRequest>(x => x.AutoMap())
);
}
保存数据时的代码:
var dto = new TaikunRequest
{
Endpoint = actionMethodName,OrganizationId = currentOrgId,OrganizationName = currentOrgName,RequestType = actionType,UserId = _userAccessor.GetCurrentUserId(),UserName = currentUserName,UserRole = _userAccessor.GetCurrentRole(),Ip = realIp ?? "none",RequestId = requestId ?? "none",Status = actionStatus
};
await _elasticclient.IndexDocumentAsync(dto,cancellationToken);
当我试图得到结果时,它总是一个结果:
调用elasticurl/taikunrequests/_search
{
"took": 8,"timed_out": false,"_shards": {
"total": 1,"successful": 1,"skipped": 0,"Failed": 0
},"hits": {
"total": {
"value": 1,"relation": "eq"
},"max_score": 1.0,"hits": [
{
"_index": "taikunrequests","_type": "_doc","_id": "0","_score": 1.0,"_source": {
"organizationId": 1,"organizationName": "itera","userId": "d1c523e7-337c-42c3-9c58-83af83a71ed7","userName": "arzu","userRole": "admin","requestType": "GET","endpoint": "/api/v1/Admin/projects/list","createdAt": "2021-07-19T08:34:16.9949230Z","ip": "none","requestId": "none","status": 200
}
}
]
}
}
如何始终将新数据附加到当前数据?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)