Search code examples
javasolrsolr-query-syntax

Highlighting only the entire phrase in custom Solr Query builder in Java


I'm trying to let Solr highlight only the entire phrase and not every word of the query. Supposing stopwords aren't a problem, my query phrase is: "hello darkness my old friend" and the highlighted result will be: "<em>hello</em> <em>darkness</em> <em>my</em> <em>old</em> <em>friend</em>. <em>hello</em> there.". What is the Java code to have this result? "<em>hello darkness my old friend</em>. hello there."?

Here is your minimum replicable example:

import org.apache.solr.client.solrj.SolrQuery;

String query = "hello darkness my old friend";
SolrQuery q = new SolrQuery(query);
q.setHighlightSimplePre("<em>");
q.setHighlightSimplePost("</em>");
String[] queryArray = query.split(" ");

I have already tried with:

q.setHighlightFragsize(query.length());
q.set("hl.usePhraseHighlighter", true);
q.set("hl.useFastVectorHighlighter", true);
q.setTermsMinCount(queryArray.length);
q.set("hl.multiTermQuery", true);

but with no luck.

Note: for bool values eg. usePhraseHighlighter i even tried putting true as string. Nothing changed

Thank you everybody in advance


Solution

  • If you're using the unified highlighter in Solr 7 and newer, you can set the hl.weightMatches parameter to true to get the phrase highlighting exactly how you want. In Solr 8 this option is enabled by default.