@@ -301,6 +301,37 @@ public void testIncludeHits() throws IOException {
301301 Assertions .assertThat ((List <?>) ObjectPath .eval ("hits.hits" , asMap )).isEmpty ();
302302 }
303303
304+ public void testMaxHits () throws IOException {
305+ // same search with and without hits
306+ SearchRequestBuilder req =
307+ client
308+ .prepareSearch ()
309+ .setIndices (INDEX_TEST )
310+ .setSize (2 )
311+ .setQuery (QueryBuilders .termQuery ("content" , "data" ))
312+ .setFetchSource (new String [] {"content" }, null );
313+
314+ // Limit the set of hits to just top 2.
315+ ClusteringActionResponse limitedHits =
316+ new ClusteringActionRequestBuilder (client )
317+ .setQueryHint ("data mining" )
318+ .setMaxHits (2 )
319+ .setAlgorithm (STCClusteringAlgorithm .NAME )
320+ .addSourceFieldMapping ("title" , LogicalField .TITLE )
321+ .setCreateUngroupedDocumentsCluster (true )
322+ .setSearchRequest (req )
323+ .execute ()
324+ .actionGet ();
325+ checkValid (limitedHits );
326+ checkJsonSerialization (limitedHits );
327+
328+ Assertions .assertThat (limitedHits .getSearchResponse ().getHits ().getHits ()).hasSize (2 );
329+
330+ var asMap = asMap (limitedHits );
331+ Assertions .assertThat (ObjectPath .<Object >eval ("hits.total.value" , asMap )).isEqualTo (2 );
332+ Assertions .assertThat ((List <?>) ObjectPath .eval ("hits.hits" , asMap )).hasSize (2 );
333+ }
334+
304335 private Map <String , Object > asMap (ClusteringActionResponse resultWithHits ) throws IOException {
305336 XContentBuilder builder = XContentFactory .jsonBuilder ().prettyPrint ();
306337 builder .startObject ();
0 commit comments