I'm writing a Web Application that communicates with Solr and I am using Spring Data Solr + SolrJ to get the information in Java.
For the Solr query, I am using (e)DisMax with some options (for now, and maybe later I need to add other things):
SolrQuery query = new SolrQuery();
query.set("q", terms);
query.set("pf", "text^100");
query.set("qf", "text");
query.set("defType", "edismax");
query.setStart(pageable.getOffset());
query.setRows(pageable.getPageSize());
query.setHighlight(true).setHighlightSnippets(10);
query.setParam("hl.fl", "text");
For this structure I have build a bean in this way
@SolrDocument
public class Document {
@Field
private String id;
@Field
private String text;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
And I am going to execute the query:
response = solrTemplate.getSolrServer().query(query);
List<Document> beans = solrTemplate.convertQueryResponseToBeans(response, Document.class);
After that, in my Document object I find only "id" and "text". Is it possible to annotate the bean to get the Highlight too?
Looking on the net I have found very few examples of using Spring Data Solr + SorlJ.
Retrieving the highlights as bean that you defined yourself is not possible with SolrJ. You can however retrieve them as a Map
QueryResponse response = solrTemplate.getSolrServer().query(query);
Map<String, Map<String, List<String>>> highlights = response.getHighlighting();
If you don't use the SolrJ approach, and switch to Spring-Data/Spring-Boot starter then it is possible:
HighlightQuery hq = new SimpleHighlightQuery(new Criteria(... enter search criteria...));
HighlightOptions hlOptions = new HighlightOptions().setNrSnipplets(maxSnippets)
hq.setHighlightOptions(hlOptions);
HighlightPage<SolrNewspaper> highlights = solrTemplate.queryForHighlightPage(mySolrCore, hq, MySolrDocument.class);
Note that the latter is only possible if you return default Solr highlighting information. If you made customizations to highlighting in Solr it probably won't work.