Nealv
Nealv

Reputation: 6884

Solr Highlight only in fields that have results

I have a solr query where I search for (webpage_text:*test* OR company_text:*test*)

In my highlighting I set my fields to webpage_text, company_text. But now I always get BOTH fields in the highlighting result, even when the search term is only found in webpage_text, I also get a highlight result for company_text which is just the full field.

Is it possible to only return the highlights for the fields that had a match ?

I tried requireFieldMatch but that doesn't work.

@EDIT: Might be important information: the 2 fields, are actually empty fields that are filled by copy fields. Does this have an impact ?

<field name="company_text" type="text_general" indexed="true" stored="true" multiValued="true" default="" termVectors="true" termPositions="true" termOffsets="true"/>
<field name="webpage_text" type="text_general" indexed="true" stored="true" multiValued="true" default="" termVectors="true" termPositions="true" termOffsets="true"/>

<dynamicField name="*company_*" type="string" indexed="true" stored="true" multiValued="true"/>
<dynamicField name="*talent_*" type="string" indexed="true" stored="true" multiValued="true"/>

<copyField source="company_*" dest="company_text" maxLength="30000000"/>
<copyField source="webpage_*" dest="webpage_text" maxLength="30000000"/>

@EDIT2: It appears that the copyfields ALWAYS appear in the highlight, even if they have no matches.

Upvotes: 0

Views: 1214

Answers (2)

Roman K
Roman K

Reputation: 3337

Solr always returns ALL fields if not specified otherwise. It does not matter whether the field is included in the search query. Fields with a match are highlighted.

If you have only two field, so these two/all fields are returned and one of them is highlighted.

Upvotes: 0

Paige Cook
Paige Cook

Reputation: 22555

I believe that the requireFieldMatch option is what you need to use. However, for this to work, you will need to also enable usePhraseHighlighter and then make sure you are doing PhraseQueries by wrapping your terms in quotes. (eg. webpage_text:"*test*")

So an example might be the following:

http://localhost:8983/solr/select/q=webpage_text%3A"*test*"+OR
  +company_text%3A"*test*"&wt=xml&hl=true&hl.fl=webpage_text%2Ccompany_text
  &hl.simple.pre=<em>&hl.simple.post=<%2Fem>
  &hl.requireFieldMatch=true&hl.usePhraseHighlighter=true

You will need to adjust the server settings as needed and additional parameters for your scenario.

Upvotes: 0

Related Questions