使用C#API的弹性搜索批量索引

时间:2017-10-26 12:43:18

标签: c# elasticsearch

我想使用NEST或Elasticsearch.Net API将json记录批量索引到弹性搜索

我的json数据是:

{"index":{"_index":"abc","_type":"abc","_id":1}},{"Name":"ApplicationFrameHost","CPU":1.25,"Company":null,"Product":null,"Path":null},{"index":{"_index":"abc","_type":"abc","_id":2}},{"Name":"Calculator","CPU":0.5,"Company":null,"Product":null,"Path":null},{"index":{"_index":"abc","_type":"abc","_id":3}},{"Name":"chrome","CPU":142.9375,"Company":null,"Product":null,"Path":null},{"index":{"_index":"abc","_type":"abc","_id":4}},{"Name":"chrome","CPU":3336.34375,"Company":null,"Product":null,"Path":null},{"index":{"_index":"abc","_type":"abc","_id":5}},{"Name":"chrome","CPU":7.1875,"Company":null,"Product":null,"Path":null}\n\n

我的代码:

 var connectionSettings = new ConnectionSettings(new 
 Uri("http://localhost:9200/"));

        var client2 = new ElasticClient(connectionSettings);
       var jsonPostData = new PostData<object>(myJson);
        var bulkRequestParameters = new BulkRequestParameters
        {

        };
  Func<BulkRequestParameters, BulkRequestParameters> convert = delegate 
  (BulkRequestParameters s)
        {
            s.ErrorTrace(true);

            return s.Refresh(Refresh.True); 
        };

        var response = client2.LowLevel.Bulk<VoidResponse>("abc", "abc", 
        jsonPostData, convert);

在响应弹性返回成功且没有错误但是仍然没有弹性数据?

弹性调试信息:POST上成功的低级别调用:/ abc/abc/_bulk?error_trace=true&refresh=true

如果有人可以提供我在这里做错的任何线索,那将会非常有帮助吗?

感谢高级。

1 个答案:

答案 0 :(得分:1)

通过在每个记录集之后修改输入JSON格式来解决这个问题,它不需要逗号:

{"index":{"_index":"abc","_type":"abc","_id":1}}{"Name":"ApplicationFrameHost","CPU":1.25,"Company":null,"Product":null,"Path":null}{"index":{"_index":"abc","_type":"abc","_id":2}},{"Name":"audiodg","CPU":1.5625,"Company":null,"Product":null,"Path":null}{"index":{"_index":"abc","_type":"abc","_id":3}},{"Name":"Calculator","CPU":0.5,"Company":null,"Product":null,"Path":null}{"index":{"_index":"abc","_type":"abc","_id":4}},{"Name":"chrome","CPU":144.109375,"Company":null,"Product":null,"Path":null}{"index":{"_index":"abc","_type":"abc","_id":5}},{"Name":"chrome","CPU":3384.609375,"Company":null,"Product":null,"Path":null}