Search code examples
javaelasticsearchcollectionselastic-stackelasticsearch-java-api

Elasticsearch: Adding arraylist in QueryBuilders.termsQuery() throws error


I am trying to pass a list of values to QueryBuilders.termsQuery(), but it is throwing the error:

Suppressed: org.elasticsearch.client.ResponseException: method [GET], host [http://localhost:9200], URI [/replacement/_search?typed_keys=true&ignore_unavailable=false&expand_wildcards=open&allow_no_indices=true&search_type=query_then_fetch&batched_reduce_size=512], status line [HTTP/1.1 400 Bad Request]

This is my java code:

Replacement linkId = null;
Replacement replacement = null;
List<Replacement> linkIDList=new ArrayList<Replacement>();
for (SearchHit hit1 : searchHits1) {
    linkId = new Replacement();
     Map<String, Object> sourceAsMap1 = hit1.getSourceAsMap();
     linkId.setLink_id((Integer) sourceAsMap1.get("link_id"));
     linkIDList.add(linkId);
}

QueryBuilder qb2 = QueryBuilders.termsQuery("id",linkIDList); // this line throws error

What is wrong with this code?

Please find the complete stacktrace

    Suppressed: org.elasticsearch.client.ResponseException: method [GET], host [http://localhost:9200], URI [/replacement/_search?typed_keys=true&ignore_unavailable=false&expand_wildcards=open&allow_no_indices=true&search_type=query_then_fetch&batched_reduce_size=512], status line [HTTP/1.1 400 Bad Request]
    {"error":{"root_cause":[{"type":"query_shard_exception","reason":"failed to create query: {\n  \"terms\" : {\n    \"id\" : [\n      \"com.demo.searchengine.bean.Replacement@43262676\",\n      \"com.demo.searchengine.bean.Replacement@1c3035bc\",\n      \"com.demo.searchengine.bean.Replacement@1ae1b004\",\n      \"com.demo.searchengine.bean.Replacement@5ba72360\",\n      \"com.demo.searchengine.bean.Replacement@29c0bbf4\",\n      \"com.demo.searchengine.bean.Replacement@3440e6a5\"\n    ],\n    \"boost\" : 1.0\n  }\n}","index_uuid":"IivTlnL9QCmEUlisIilYUg","index":"replacement"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"replacement","node":"uPLyU7R5RXeirg8XzRqhnA","reason":{"type":"query_shard_exception","reason":"failed to create query: {\n  \"terms\" : {\n    \"id\" : [\n      \"com.demo.searchengine.bean.Replacement@43262676\",\n      \"com.demo.searchengine.bean.Replacement@1c3035bc\",\n      \"com.demo.searchengine.bean.Replacement@1ae1b004\",\n      \"com.demo.searchengine.bean.Replacement@5ba72360\",\n      \"com.demo.searchengine.bean.Replacement@29c0bbf4\",\n      \"com.demo.searchengine.bean.Replacement@3440e6a5\"\n    ],\n    \"boost\" : 1.0\n  }\n}","index_uuid":"IivTlnL9QCmEUlisIilYUg","index":"replacement","caused_by":{"type":"number_format_exception","reason":"For input string: \"com.demo.searchengine.bean.Replacement@43262676\""}}}]},"status":400}

Solution

  • As shown in the stacktrace, it is because of the Number Format Exception. You need to send list of Integer which is the type of id field instead of passing List<Replacement> as mentioned in your code.

    Instead of creating an object Of List<Replacement>, create a List and pass that list.

    Hope it works for you.