问题
i'm using solr 7.7.2 and I wrote a Java program in Solr using solrj that searches for a word in a huge text file. I use following code to show the search results that represent the whole text.
SolrQuery params = new SolrQuery();
params.setQuery("content:word for search");
How to display only one line of text where the word is in that line?
All code is like this
public static void main(String args[]) throws SolrServerException, IOException
{
String urlString = "http://localhost:8983/solr/test_core";
SolrClient Solr = new HttpSolrClient.Builder(urlString).build();
SolrQuery params = new SolrQuery();
params.setQuery("content:word for search");
params.setSort("score", SolrQuery.ORDER.desc);
QueryResponse queryResponse = Solr.query(params);
SolrDocumentList result = queryResponse.getResults();
for (int i = 0 ; i < result.size(); i++ )
{
System.out.println(result.get(i) + " \n");
}
}
回答1:
Highlighting
is one of solr feature. You have to pass query Parameters to achieve highlighting are as follows:-
hl
– set to true, it enables highlighted snippets to be generated in the query response.
hl.fl
– mention a list of fields to highlight. char * will highlight all the fields
hl.fragsize
– The size, in characters, of the snippets (aka fragments) created by the highlighter. In the original Highlighter, “0” indicates that the whole field value should be used with no fragmenting. By default fragment is of size 100 characters
Check by adding the below code.
params.setHighlight(true).setHighlightSnippets(1);
params.setParam("hl.fl", "*");
params.setParam("hl.fragsize", "0");
Here is the full code for you to try.
Note : Please ignore the hardcoded things from the code, like hardcoded url "solrUrl = "http://localhost:8983/solr" and string "return "Success"". Those should be read from the properties file and from a constant file. Expect you do the same. Never use System out in your production code.
public String getResult() throws SolrServerException, IOException {
final SolrClient client = getSolrClient();
ModifiableSolrParams params = new ModifiableSolrParams ();
params.set ("q", "comment_t:pizza");
params.set ("fl", "id, comment_t");
params.set ("sort", "id asc");
params.set("hl", true);
params.set("hl.q", "pizza");
params.set("hl.simple.pre", "<strong>");
params.set("hl.simple.post", "</strong>");
params.set("hl.fl", "comment_t");
params.set("hl.fragsize", "100");
final QueryResponse response = client.query("demo", params);
response.getHighlighting();
final SolrDocumentList documents = response.getResults();
System.out.println("Found " + documents.getNumFound() + " documents");
for (SolrDocument document : documents) {
final String id = (String) document.getFirstValue("id");
final String name = (String) document.getFirstValue("comment_t");
System.out.println("id: " + id + "; comment_t: " + name);
if(response.getHighlighting() != null){
System.out.println("highlighted text :: " + response.getHighlighting());
}
}
return "Success";
}
private SolrClient getSolrClient() {
final String solrUrl = "http://localhost:8983/solr";
return new HttpSolrClient.Builder(solrUrl).withConnectionTimeout(10000).withSocketTimeout(60000).build();
}
Please find the screenshot of the output :
来源:https://stackoverflow.com/questions/60145838/need-searched-text-and-a-few-lines-around-it-in-solr-search-in-java-api