Search code examples
solrlucene

How to highlight a searched text inside a document which is indexed in Apache Solr


I am using Apache Solr 8.6 to index the documents using POST Tool in Linux as mentioned in the Apache Solr Reference Guide.

POST Tool Command

bin/post -c testcore /testdocs/

The documents are getting indexed successfully. Now when I searched the string eg: hello in Solr Admin UI, It is returning the matching documents.

Now I want to highlight this string "hello" which is present inside the document as content. But I am unable to achieve the same. I have tried putting highlighting enabled and entering text_* in the hl.fl=text_*. Yet I am unable to achieve the result. However if this searched string is present in any of the indexed fields it is correctly highlighting in the results.

In managed-schema.xml i have added content and text Fields as shown below.

<field name="content" type="text_general" indexed="false" stored="true" multiValued="false"/> 

<field name="text" type="text_general" indexed="true" stored="false" multiValued="true"/>

I have tried below queries:

http://localhost:8983/solr/testcore/select?q=_text_%3a*hello*&hl=true&hl.fl=content&hl.usephrasehighlighter=true

http://localhost:8983/solr/testcore/select?q=_text_%3a*hello*&hl=true&hl.fl=text_*&hl.usephrasehighlighter=true

Will be grateful if you could guide me further.

Edit: As per recommendation

I have changed the fields of content and text as shown below, but it is not working either. That means when I am searching the text string it is not highlighting the searched string inside the document as content.

<field name="content" type="text_general" indexed="true" stored="true" multiValued="false"/> 
    
<field name="text" type="text_general" indexed="true" stored="true" multiValued="true"/>

Solution

  • Finally the issue resolved by modifying _text _ field in manage-schema.xml file. I have changed the value of stored="false" to stored="true" as shown in below syntax.

    <field name="_text_" type="text_general" indexed="true" stored="true" multiValued="false"/>
    

    Then I have restarted the solr and refindexed the document.

    Now i have fired the query to highlight the text as shown below. In this query i have used hl=on & hl.fl=_text _ to highlight the comment.

    Query to highlight the searched string

    http://localhost:8983/solr/testcore/select?q=_text_%3a*hello*&hl=on&hl.fl=_text_