Issue
- Snippet field doesn't get returned after using SearchRequestBuilderFactory to create the Search Request following Liferay's official documentation.
Environment
- DXP 7.4
Resolution
- In order to get a summary, the below code is suggested by https://help.liferay.com/hc/en-us/articles/360032611211-Returning-Results#creating-a-results-summary:
Summary summary = new Summary(
document.get(prefix + titleField, titleField),
document.get(prefix + contentField, contentField));
Here, the prefix is :-
String prefix = Field.SNIPPET + StringPool.UNDERLINE;
- The goal would be to generate the exact same thing that is shown here: https://learn.liferay.com/w/dxp/using-search/search-pages-and-widgets/search-results/search-results#result-summaries.
- Your code might look like this:
...
SearchResponse searchResponse = searcher.search(searchRequest);
SearchHits searchHits = searchResponse.getSearchHits();
List<SearchHit> searchHitsList = searchHits.getSearchHits();
for (SearchHit hit : searchHitsList) {
Document document = hit.getDocument();
...
- But in order to achieve this goal, you must use the SearchResponse function getDocuments71(). This function will return a list of documents that contains the snippet title field and the snippet content field. The code example below may be useful:
...
SearchResponse searchResponse = searcher.search(searchRequest);
List<Document> documents = searchResponse.getDocuments71();
for (Document document : documents) {
...