How do you make a Solr query to return a range of words around the specific word you queried?

Viewed 37

Say I have the following solr/lucene query:

https://some_website.com//api/myapi/search?profile=myprofile&fl=&fq=batchid:,bodytextsize_i*&q=word_i_want_to_search&partner=mypartnerid&rows=10

I would like to know what I can add to this in order to have the sentence or a certain range of words around the query word returned to me in the response. For instance say a document has the following sentence:

The computer's word_i_want_to_search is broken.

I would like to be able to query word_i_want_to_search and have the response show me the entire sentence. Is this possible?

Thank you!

1 Answers

This is known as highlighting:

Highlighting in Solr allows fragments of documents that match the user’s query to be included with the query response.

The fragments are included in a special section of the query response (the highlighting section), and the client uses the formatting clues also included to determine how to present the snippets to users. Fragments are a portion of a document field that contains matches from the query and are sometimes also referred to as "snippets" or "passages".

Use the parameters hl=true&hl.fl=field_you_are_searching and go from there. There's a lot of small things you can tweak to get different behaviors, such as how much data is being included in the response.

Related