Expand my Community achievements bar.

How to get 2 excerpts at once in lucene search

Avatar

Level 3

I have used p.excerpt=true in my aem query. And while extracting the excerpts using hit.getExcerpt(), I am getting a single sentence like.. "We are specialized in <strong>health</strong> care centres..." .

 

How do I get two such sentences . eg: "We are specialized in <strong>health</strong> care centres... with the help of World <strong>Health</strong> Organization.."

2 Replies

Avatar

Level 3

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