Setsize for java api elasticsearch


(Ramana Reddy Moole) #1

how to set fetch size for elasticseaarch Java API.
is it a good practice to setSize method of a query in elasticsearch Java API?


(Paul van der Bijl) #2

You can call two methods on the query to change which records you receive:

  • .setSize(x) will set the ammount of documents you get retrieve, if it's not specified you will get 10 records
  • .setFrom(x) will retrieve the records from x.

So for example if you add '.setSize(10).setFrom(20)' to your query you will get record 20-29.

If increase the ammount of documents retrieved via setSize(x) you should be aware that it will make your query slower. This is were the .setFrom(x) comes in handy. You can for example first of all give back the first 10 results to the user, if he/she presses the 'Show more' button you can do the query again with .setFrom(11)
I hope this helps you solve the problem :slight_smile:


(Ramana Reddy Moole) #3

Thank you for your answer @Pakoenator. I also heard that scroll is the ideal solution for fetching the huge data from elasticsearch. If you have any example of Scroll please share it here.


(Paul van der Bijl) #4

No problem :slight_smile:
Unfortunately I have never used the Scroll feature so I can't tell anything usefull about it. There always is the official documentation ofcourse:
https://www.elastic.co/guide/en/elasticsearch/reference/2.0/search-request-scroll.html
https://www.elastic.co/guide/en/elasticsearch/client/java-api/2.0/java-search-scrolling.html


(Ramana Reddy Moole) #5

Thank you very much @Pakoenator. That helped me a lot. I have another use case where i have to categorize the data based on some value and get the data using TopHits aggregation. Is there a possibility to do the same scroll option here?


(system) #6