抑制:org.elasticsearch.client.ResponseException:方法[GET],主机[http:// localhost:9200],URI

时间:2018-06-17 03:57:04

标签: java elasticsearch elastic-stack

我试图使用java代码从elasticsearch获取文档。我正在执行两个查询,将第一个查询的结果作为输入传递给第二个查询。

第一个查询结果是一种ArrayList。所以,我将arraylist作为第二个查询的输入。

请在下面找到我的代码。

SearchRequest searchRequest = new SearchRequest(INDEX); 
SearchRequest searchRequest2 = new SearchRequest(INDEX); 
searchRequest.types(TYPE);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
SearchSourceBuilder searchSourceBuilder2 = new SearchSourceBuilder();
QueryBuilder qb=QueryBuilders.matchQuery("code", code);  /// ---> First Query
searchSourceBuilder.query(qb);
searchSourceBuilder.size(50);
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse1 = null;
try {
     searchResponse1 = restHighLevelClient.search(searchRequest);
} catch (IOException e) {
    e.getLocalizedMessage();
}
SearchHit[] searchHits1 = searchResponse1.getHits().getHits();
System.out.println("Hits size--->"+searchHits1.length);
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);
}

System.out.println("Link ID List Size --->"+linkIDList.size());
for(Replacement link_id:linkIDList) {
    System.out.println("Link ID List --->"+link_id.getLink_id());
}

        QueryBuilder qb2 = QueryBuilders.termsQuery("id",linkIDList);   // ---> second query, getting error on this line. linkIDList is my output of first query.
        searchSourceBuilder2.query(qb2);
        searchSourceBuilder2.size(50);
        searchRequest2.source(searchSourceBuilder2);
        SearchResponse searchResponse2 = null;
        try {
             searchResponse2 = restHighLevelClient.search(searchRequest2);
        } catch (IOException e) {
            e.getLocalizedMessage();
        }
        List<Replacement> replacementList=new ArrayList<Replacement>();
        SearchHit[] searchHits2 = searchResponse2.getHits().getHits();
        for (SearchHit hit2 : searchHits2) {
            Map<String, Object> sourceAsMap2 = hit2.getSourceAsMap();
            replacement=new Replacement();
            replacement.setId(sourceAsMap2.get("id").toString());
            replacement.setCode(sourceAsMap2.get("code").toString());
            replacementList.add(replacement);
          }             


return replacementList;

请在下面找到我的错误:

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}

0 个答案:

没有答案