Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elastic Search High Level Java Client UpdateByQueryRequest API help required

I am migrating from normal ES Http calls to ES HighLevelJavaClient.

I want to convert this old implementation POST /_update_by_query

 {
  "script": {
    "inline": "ctx._source.collibra_match_for = []"
  },
  "query": {
    "bool": {
      "filter": {
        "script": {
          "script": {
            "source": "doc['collibra_match_for.keyword'].values.length > 0",
            "lang": "painless"
          }
        }
      }
    }
  }
}

To High Level client code.

The code I tried is :

  UpdateByQueryRequest request = new UpdateByQueryRequest();
    request.setConflicts("proceed");
    request.setQuery(QueryBuilders.boolQuery().filter(QueryBuilders.scriptQuery(new Script("doc['collibra_match_for.keyword'].values.length > 0"))));
    request.setScript(new Script(ScriptType.INLINE, "painless","ctx._source.collibra_match_for = []",Collections.emptyMap()));
    request.setRefresh(true);
    try {
        BulkByScrollResponse bulkResponse = esClient.updateByQuery(request, RequestOptions.DEFAULT);
        long totalDocs = bulkResponse.getTotal();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

But I am getting this error :

Elasticsearch exception [type=exception, reason=Incorrect HTTP method for uri [/_update_by_query?requests_per_second=-1&ignore_unavailable=false&expand_wildcards=open&allow_no_indices=true&refresh=true&conflicts=proceed&timeout=1m] and method [POST], allowed: [DELETE, PUT, GET, HEAD]]

Any help will be appreciated :)

like image 875
Abhishek Sengupta Avatar asked Feb 16 '26 12:02

Abhishek Sengupta


1 Answers

I actually found the answer to this question.. We just need to add the index name in the constructor. This is the final answer to the question.Hope it will be useful to someone in future :)

    UpdateByQueryRequest request = new UpdateByQueryRequest(INDEX_NAME);
    request.setConflicts("proceed");
    request.setQuery(QueryBuilders.boolQuery().filter(QueryBuilders.scriptQuery(new Script("doc['collibra_match_for.keyword'].values.length > 0"))));
    request.setScript(new Script(ScriptType.INLINE, "painless","ctx._source.collibra_match_for = []",Collections.emptyMap()));
    request.setRefresh(true);
    try {
        BulkByScrollResponse bulkResponse = esClient.updateByQuery(request, RequestOptions.DEFAULT);
        long totalDocs = bulkResponse.getTotal();
    } catch (IOException e) {
        e.printStackTrace();
    }
like image 197
Abhishek Sengupta Avatar answered Feb 18 '26 01:02

Abhishek Sengupta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!