How do I return only a truncated portion of a field in SOLR?
Asked Answered
D

2

7

I have a really large (5000+ characters) text field in SOLR named Description. So far it works great for searching and highlighting. If I perform a search and there are no highlighted portions then I just show the first 300 characters. What I would like to do is just return the 300 characters in the result from SOLR.

I would like to do this because when testing I get improved performance if I return a smaller result. This is probably because the XML doc is smaller so less time on the wire and then the processing is faster because the doc is smaller.

I have thought of using a new field that just stored the first 300 characters. I think this would work, but I was wondering if there was a better or more native solution.

Darwindarwinian answered 10/8, 2010 at 19:27 Comment(0)
K
8

What you're looking for is the highlighting hl.maxAlternateFieldLength (http://wiki.apache.org/solr/HighlightingParameters#hl.maxAlternateFieldLength).

You will need to define the field as its own alternate field. If you want to highlight the field Description, the highlight query parameters would be:

hl=true
hl.fl=Description
f.Description.hl.alternateField=Description
hl.maxAlternateFieldLength=300

Finally, to omit the Description field from the query result, you will have to exclude it from the fl query parameter:

fl=score,url,title,date,othermetadata
Kemberlykemble answered 11/8, 2010 at 9:46 Comment(0)
I
0

When using the Unified Highlighter, hl.alternateField is not available as a query parameter. Instead you can use the hl.defaultSummary query parameter (available since Solr 4.5)

hl.defaultSummary
If true, use the leading portion of the text as a snippet if a proper highlighted snippet can’t otherwise be generated. The default is false.

Illyria answered 8/11, 2019 at 7:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.