Restrict highlighting to matching fields in Solr 5.5

62 views Asked by At

We are using Solr 5.5.

I would like to know if it is possible to restrict highlighting as described below.

Suppose we have this search:

doc_id:100 OR samples 

which is transformed into

doc_id:100 OR text:samples 

We want the highlighting to of "100" to be restricted to the doc_id field, and "samples" to be highlighted in the document_* fields copied to the text field.

With basic highlighting enabled we get "100" highlighted in any field (per the documentation), e.g. hl=true&hl.fl=*

  "highlighting": { 
    "2918": { 
      "document_content": [ 
        " \t\t <em>100</em> \t\t 90 \t\t 95 \t\t 90 \t\t 95 \t\t 80 \t\t 85 \t\t 85 \t\t 80 \t\t 90.42 \t\t 66.67 \t\t 90.4166666667 \t\t \t\t 1085" 
      ], 
      "doc_id": [ 
        "<em>100</em>" 
      ] 
    }, 
    "4413": { 
      "document_title": [ 
        "Purchased <em>samples</em>" 
      ], 
      "doc_id": [ 
        "<em>100</em>" 
      ], 
      "document_file_name": [ 
        "<em>Samples</em> late 2004.doc" 
      ] 
    }, 
... 

The recommended solution is to use hl.requireFieldMatch but this results in only the doc_id field being highlighted. The other (document_*) fields are no longer highlighted, I guess because they don't match text, e.g. hl=true&hl.fl=*&hl.requireFieldMatch=true

 "highlighting": { 
    "2918": { 
      "doc_id": [ 
        "<em>100</em>" 
      ] 
    }, 
    "4413": { 
      "doc_id": [ 
        "<em>100</em>" 
      ] 
    }, 
    "4415": { 
      "doc_id": [ 
        "<em>100</em>" 
      ] 
    },

How can I get the document_* fields that are copied to the text field to be highlighted while restricting highlighting of the doc_id field to its matching query terms?

EDIT: Perhaps, the key is to index and store the text field and then specify highlighting of the text and doc_id fields, i.e. change its definition to

and call hl=true&hl.fl=text,doc_id&hl.requireFieldMatch=true.

0

There are 0 answers