OpenSearch is text based search engine in which we can store and query the entries extremely fast.
The time to read is explicity proportional to the complexity of the search query, thus we don’t to fetch everything at once.
For this, opensearch provides us an option to limit the number of results we want and from where we should begin the search, aka, we can paginate the result.
In the JSON structure we can do it the following way.
GET sample-index/_search { "from": 0, "size": 10, "query": { "match": { "movie": "avengers" } } }
This will return the 10 results starting from 0. You can configure it as per your requirement and get the result.
This same functionality we can achieve in the Java client SDK as well, while doing the search operation, we have to pass the size and from.
MatchQuery matchQuery = new MatchQuery.Builder().field("movies").query(FieldValue.of("\"avengers\"")).build(); FieldSort fieldSort = new FieldSort.Builder().field("release-date").order(SortOrder.Asc).build(); SortOptions sortOptions = new SortOptions.Builder().field(fieldSort).build(); SearchResponse<T> searchResponse = openSearchClient.search(s -> { s.index("sample-index"); s.query(rangeQuery._toQuery()); s.sort(sortOptions); s.size("0")); s.from("10"); return s; }, T.class); for (int i = 0; i < searchResponse.hits().hits().size(); i++) { System.out.println(searchResponse.hits().hits().get(i).source()); }
While searching we have to pass all the parameters.