微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何将 json 附加到 Elastic Search .NEST 客户端中的现有 json?

如何解决如何将 json 附加到 Elastic Search .NEST 客户端中的现有 json?

在我的 .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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?