suresh t suresh t - 21 days ago 10
Java Question

To set a size in ElasticSearch using @Query

I'm using @query to retrieve the elasticsearch data through JPQL. the default size is 10, i mentioned the size as 5 in @Query but am getting 10 records but i need 5 records.how to solve this problem

@Query("\"from\":\"0\",\"size\":\"5\",{\"bool\":{\"must\":[{\"term\":{\"brand\":\"?0\"}}]}}}")


List findByBrand(String brand);

Val Val
Answer

You simply need to add a Pageable argument to your findByBrand method:

@Query("{\"bool\":{\"must\":[{\"term\":{\"brand\":\"?0\"}}]}}")
List findByBrand(String brand, Pageable pageable);

Then you can call that method like this:

List brands = findByBrand("mybrand", new PageRequest(0, 5));

Alternatively, you can also specify how many results you want in the method name itself:

List brands = findTop5ByBrand(String brand);