简体   繁体   English

通过弹性搜索 Java API 批量更新抛出异常

[英]Bulk update via elastic search Java API throws exception

Bulk update in elastic search Java Api throws the following exception.弹性搜索 Java Api 中的批量更新会引发以下异常。

org.elasticsearch.action.ActionRequestValidationException: Validation Failed: 1: no requests added;
at org.elasticsearch.action.ValidateActions.addValidationError(ValidateActions.java:29)
at org.elasticsearch.action.bulk.BulkRequest.validate(BulkRequest.java:412)
at org.elasticsearch.action.support.TransportAction.execute(TransportAction.java:55)
at org.elasticsearch.action.bulk.TransportBulkAction$TransportHandler.messageReceived(TransportBulkAction.java:299)
at org.elasticsearch.action.bulk.TransportBulkAction$TransportHandler.messageReceived(TransportBulkAction.java:288)
at org.elasticsearch.transport.netty.MessageChannelHandler.handleRequest(MessageChannelHandler.java:207)
at org.elasticsearch.transport.netty.MessageChannelHandler.messageReceived(MessageChannelHandler.java:108)
at org.elasticsearch.common.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:70)
at org.elasticsearch.common.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)
at org.elasticsearch.common.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:791)
at org.elasticsearch.common.netty.channel.Channels.fireMessageReceived(Channels.java:296)
at org.elasticsearch.common.netty.handler.codec.frame.FrameDecoder.unfoldAndFireMessageReceived(FrameDecoder.java:462)
at org.elasticsearch.common.netty.handler.codec.frame.FrameDecoder.callDecode(FrameDecoder.java:443)
at org.elasticsearch.common.netty.handler.codec.frame.FrameDecoder.messageReceived(FrameDecoder.java:303)
at org.elasticsearch.common.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:70)
at org.elasticsearch.common.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)
at org.elasticsearch.common.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:791)
at org.elasticsearch.common.netty.OpenChannelsHandler.handleUpstream(OpenChannelsHandler.java:74)
at org.elasticsearch.common.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)
at org.elasticsearch.common.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:559)
at org.elasticsearch.common.netty.channel.Channels.fireMessageReceived(Channels.java:268)
at org.elasticsearch.common.netty.channel.Channels.fireMessageReceived(Channels.java:255)
at org.elasticsearch.common.netty.channel.socket.nio.NioWorker.read(NioWorker.java:88)
at org.elasticsearch.common.netty.channel.socket.nio.AbstractNioWorker.process(AbstractNioWorker.java:109)
at org.elasticsearch.common.netty.channel.socket.nio.AbstractNioSelector.run(AbstractNioSelector.java:312)
at org.elasticsearch.common.netty.channel.socket.nio.AbstractNioWorker.run(AbstractNioWorker.java:90)
at org.elasticsearch.common.netty.channel.socket.nio.NioWorker.run(NioWorker.java:178)
at org.elasticsearch.common.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:108)
at org.elasticsearch.common.netty.util.internal.DeadLockProofWorker$1.run(DeadLockProofWorker.java:42)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)

Below is the code I have written.下面是我写的代码。

    BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();

    for (String documentId : documentIds)
    {
        bulkRequestBuilder.add(client.prepareUpdate("39302", "3", documentId).setScript("ctx._source.customerName=\"Ramaraj\";"));
    }

    BulkResponse bulkResponse = bulkRequestBuilder.execute().actionGet();

This the same way I have written for bulk index also.这与我为批量索引编写的方式相同。 It was working fine.它工作正常。

Thanks in advance.提前致谢。

Note:Bulk Update in java api is added only few days back.注意:几天前才添加了 java api 中的批量更新。

As the exception says, no requests were added to the BulkRequest object.正如例外所说,没有向 BulkRequest 对象添加请求。 Debug if the records you add in the for loop are actually being added to the builder object.调试您在 for 循环中添加的记录是否实际添加到构建器对象中。 Found this post recently with the same issue: ActionRequestValidationException最近发现这个帖子有同样的问题: ActionRequestValidationException

This exception happens because your documentIds collection is empty .发生此异常是因为您的 documentIds 集合为空

You should to check if your collection (list, queue, etc) does have documents before make the request.在提出请求之前,您应该检查您的集合(列表、队列等)是否有文档。 I come across the same problem yesterday, in my case the elastic search insertions happen in a given interval (lets say 5s), not very ofter the insertion collection was empty;昨天我遇到了同样的问题,在我的情况下,弹性搜索插入发生在给定的时间间隔(比如 5 秒),插入集合不是空的;

In my case this is a very rare occurrence, and would only happen rarely (in my case 3 million documents inserted every day), and might be difficult to identify before go to production.在我的情况下,这是一种非常罕见的情况,并且很少发生(在我的情况下每天插入 300 万个文档),并且在投入生产之前可能难以识别。

I would handle your exception like this:我会像这样处理你的异常:

if(!documentIds.isEmpty())
{   
     BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();

    for (String documentId : documentIds)
    {
        bulkRequestBuilder.add(client.prepareUpdate("39302", "3", documentId).setScript("ctx._source.customerName=\"Ramaraj\";"));
    }

    BulkResponse bulkResponse = bulkRequestBuilder.execute().actionGet();
}

The error is because of size of your BulkRequestBuilder , which has no requests.错误是因为您的BulkRequestBuilder大小,它没有请求。 Debug if your builder contains any Requests如果您的构建器包含任何Requests调试

在此处输入图片说明

This Exception mostly occurs when you try to save or update empty object in elastic search index.当您尝试在弹性搜索索引中保存或更新空对象时,通常会发生此异常。 For this first check whether the Object is null or not then perform updation See the snapshot of my code:为此,首先检查对象是否为空,然后执行更新查看我的代码快照:在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM