无法索引所需的文档数量

时间:2015-09-28 13:42:32

标签: elasticsearch

我在弹性搜索中逐个索引1000个json文档(仅用于测试目的),如下所示

deleteIndex(myIndex);
List<XContentBuilder> docs = new ArrayList<XContentBuilder>();
for (int i=0;i< 1000; i++)
{
   docs.add(createJsonDoc());
}
for (int i = 0; i < 1000; i++)    
{
   client.prepareIndex(myindex, mytype)
            .setSource(docs.get(i))
            .execute();
            //Thread.sleep(10);           
}

我注意到每次运行此代码时,它总是索引不到1000个文档。 如果我取消注释上面的thread.sleep行,那么我可以将其指向1000的唯一方法就是。

为什么?

1 个答案:

答案 0 :(得分:0)

检查从ES返回的响应,其中一些很可能是失败的。这是我们在所有调用中使用的扩展方法:

    internal IRestResponse<T> VerifyResponse<T>(IRestResponse<T> response, Action okAction)
    {
        response.ThrowIfNull("response");

        if (response.StatusCode == HttpStatusCode.OK)
        {
            if (okAction != null)
            {
                okAction();
            }

            return response;
        }

        if (response.StatusCode == HttpStatusCode.BadRequest)
        {
            throw new BadRequestException(response);
        }

        throw new Exception(string.Format("Unhandled API result: {0}\r\n{1}", response.StatusCode, response.Content));
    }
相关问题