suresh t
suresh t

Reputation: 37

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);

Upvotes: 2

Views: 1079

Answers (2)

wmlynarski
wmlynarski

Reputation: 536

I guess the problem is in that that @Query annotation. Generates only "query" node. You would expect to have query like that.

{
  "from": "0",
  "size": "5",
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "brand": "?0"
          }
        }
      ]
    }
  }
}

Because of this your are not able to put "from" and "size" in the query string. That's why in other answer you have to use Pageable to get the results. The @Query string in the question is not a json string. It misses "query": before "bool" to be proper json.

Upvotes: 0

Val
Val

Reputation: 217274

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);

Upvotes: 1

Related Questions