0

私はElasticsearchTemplateを初めて使用しています。私は私の質問に基づいてElasticsearchから1000の文書を得たいと思っています。 私はQueryBuilderを使用してクエリを作成しましたが、それは完全に機能しています。 以下のリンクをたどりました。スキャンとスクロールを使用して大きなデータセットを達成することは可能です。

link one
link twoElasticsearchTemplateは大きなデータセットを取得します

私はコピー上記のリンクの1、から貼り付けているコードの次のセクションでこの機能を実装しようとしています。 しかし、私は次のようなエラーになっています:

The type ResultsMapper is not generic; it cannot be parameterized with arguments <myInputDto>.

MyInputDtoは私のプロジェクトで@Documentのアノテーションを持つクラスです。 最後に、私はElasticsearchから1000の文書を検索したいだけです。 sizeパラメータを検索しようとしましたが、サポートされていないと思います。

String scrollId = esTemplate.scan(searchQuery, 1000, false); 
     List<MyInputDto> sampleEntities = new ArrayList<MyInputDto>(); 
     boolean hasRecords = true; 
     while (hasRecords) { 
      Page<MyInputDto> page = esTemplate.scroll(scrollId, 5000L, 
        new ResultsMapper<MyInputDto>() { 
         @Override 
         public Page<MyInputDto> mapResults(SearchResponse response) { 
          List<MyInputDto> chunk = new ArrayList<MyInputDto>(); 
          for (SearchHit searchHit : response.getHits()) { 
           if (response.getHits().getHits().length <= 0) { 
            return null; 
           } 
           MyInputDto user = new MyInputDto(); 
           user.setId(searchHit.getId()); 
           user.setMessage((String) searchHit.getSource().get("message")); 
           chunk.add(user); 
          } 
          return new PageImpl<MyInputDto>(chunk); 
         } 
        }); 
      if (page != null) { 
       sampleEntities.addAll(page.getContent()); 
       hasRecords = page.hasNextPage(); 
      } else { 
       hasRecords = false; 
      } 
     } 

ここで問題は何ですか? これを達成するための他の方法はありますか? 誰かがこのコード(コード)がバックエンドでどのように働いているか教えていただければ幸いです。

答えて

0

ソリューション1

あなたがElasticsearchTemplateを使用したい場合は、setPageable方法でページサイズを設定することができますよう、CriteriaQueryを使用するためにはるかに簡単で読みやすいでしょう。あなたの代わりにElasticsearchTemplateorg.elasticsearch.client.Clientを使用したい場合は、SearchResponseは、検索数が返すようにヒットを設定することができます

CriteriaQuery criteriaQuery = new CriteriaQuery(Criteria.where("productName").is("something")); 
criteriaQuery.addIndices("prods"); 
criteriaQuery.addTypes("prod"); 
criteriaQuery.setPageable(PageRequest.of(0, 1000)); 

ScrolledPage<TestDto> scroll = (ScrolledPage<TestDto>) esTemplate.startScroll(3000, criteriaQuery, TestDto.class); 
while (scroll.hasContent()) { 
    LOG.info("Next page with 1000 elem: " + scroll.getContent()); 
    scroll = (ScrolledPage<TestDto>) esTemplate.continueScroll(scroll.getScrollId(), 3000, TestDto.class); 
} 
esTemplate.clearScroll(scroll.getScrollId()); 

ソリューション2

:スクロールを使用すると、データの次のセットを取得することができます:

QueryBuilder prodBuilder = ...; 

SearchResponse scrollResp = client. 
     prepareSearch("prods") 
     .setScroll(new TimeValue(60000)) 
     .setSize(1000) 
     .setTypes("prod") 
     .setQuery(prodBuilder) 
     .execute().actionGet(); 

ObjectMapper mapper = new ObjectMapper(); 
List<TestDto> products = new ArrayList<>(); 

try { 
    do { 
     for (SearchHit hit : scrollResp.getHits().getHits()) { 
      products.add(mapper.readValue(hit.getSourceAsString(), TestDto.class)); 
     } 
     LOG.info("Next page with 1000 elem: " + products); 
     products.clear(); 
     scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()) 
       .setScroll(new TimeValue(60000)) 
       .execute() 
       .actionGet(); 
    } while (scrollResp.getHits().getHits().length != 0); 
} catch (IOException e) { 
    LOG.error("Exception while executing query {}", e); 
} 
関連する問題