@kiran_22_12
commentUtil.getExcerpts will help you like below
SearchResult result = query.getResult();
List<Hit> hits = result.getHits();
// Get the ExcerptProvider
ExcerptProvider excerptProvider = resourceResolver.adaptTo(ExcerptProvider.class);
// Retrieve and output excerpts from the hits
int numExcerpts = 2; // Number of excerpts you want to retrieve
for (Hit hit : hits) {
Resource resource = hit.getResource();
// Get the excerpts
List<String> excerpts = excerptProvider.getExcerpts(resource, searchTerm, numExcerpts);
// Output the excerpts
System.out.println("Excerpts for " + resource.getPath() + ":");
for (String excerpt : excerpts) {
System.out.println("- " + excerpt);
}
}