SOLR-2021: Add SolrEncoder plugin to Highlighter

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@981936 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Koji Sekiguchi 2010-08-03 15:45:07 +00:00
parent 0c0fb8bb5a
commit ac95528aab
5 changed files with 78 additions and 34 deletions

View File

@ -220,6 +220,8 @@ New Features
to dates. to dates.
(Gijs Kunze, hossman) (Gijs Kunze, hossman)
* SOLR-2021: Add SolrEncoder plugin to Highlighter. (koji)
Optimizations Optimizations
---------------------- ----------------------

View File

@ -978,45 +978,49 @@
<str name="echoHandler">true</str> <str name="echoHandler">true</str>
</lst> </lst>
</requestHandler> </requestHandler>
<searchComponent class="solr.HighlightComponent" name="highlight"> <searchComponent class="solr.HighlightComponent" name="highlight">
<highlighting> <highlighting>
<!-- Configure the standard fragmenter --> <!-- Configure the standard fragmenter -->
<!-- This could most likely be commented out in the "default" case --> <!-- This could most likely be commented out in the "default" case -->
<fragmenter name="gap" class="org.apache.solr.highlight.GapFragmenter" default="true"> <fragmenter name="gap" class="org.apache.solr.highlight.GapFragmenter" default="true">
<lst name="defaults"> <lst name="defaults">
<int name="hl.fragsize">100</int> <int name="hl.fragsize">100</int>
</lst> </lst>
</fragmenter> </fragmenter>
<!-- A regular-expression-based fragmenter (f.i., for sentence extraction) --> <!-- A regular-expression-based fragmenter (f.i., for sentence extraction) -->
<fragmenter name="regex" class="org.apache.solr.highlight.RegexFragmenter"> <fragmenter name="regex" class="org.apache.solr.highlight.RegexFragmenter">
<lst name="defaults"> <lst name="defaults">
<!-- slightly smaller fragsizes work better because of slop --> <!-- slightly smaller fragsizes work better because of slop -->
<int name="hl.fragsize">70</int> <int name="hl.fragsize">70</int>
<!-- allow 50% slop on fragment sizes --> <!-- allow 50% slop on fragment sizes -->
<float name="hl.regex.slop">0.5</float> <float name="hl.regex.slop">0.5</float>
<!-- a basic sentence pattern --> <!-- a basic sentence pattern -->
<str name="hl.regex.pattern">[-\w ,/\n\&quot;&apos;]{20,200}</str> <str name="hl.regex.pattern">[-\w ,/\n\&quot;&apos;]{20,200}</str>
</lst> </lst>
</fragmenter> </fragmenter>
<!-- Configure the standard formatter --> <!-- Configure the standard formatter -->
<formatter name="html" class="org.apache.solr.highlight.HtmlFormatter" default="true"> <formatter name="html" class="org.apache.solr.highlight.HtmlFormatter" default="true">
<lst name="defaults"> <lst name="defaults">
<str name="hl.simple.pre"><![CDATA[<em>]]></str> <str name="hl.simple.pre"><![CDATA[<em>]]></str>
<str name="hl.simple.post"><![CDATA[</em>]]></str> <str name="hl.simple.post"><![CDATA[</em>]]></str>
</lst> </lst>
</formatter> </formatter>
<!-- Configure the standard fragListBuilder --> <!-- Configure the standard encoder -->
<fragListBuilder name="simple" class="org.apache.solr.highlight.SimpleFragListBuilder" default="true"/> <encoder name="html" class="org.apache.solr.highlight.HtmlEncoder" default="true"/>
<!-- Configure the single fragListBuilder --> <!-- Configure the standard fragListBuilder -->
<fragListBuilder name="single" class="org.apache.solr.highlight.SingleFragListBuilder"/> <fragListBuilder name="simple" class="org.apache.solr.highlight.SimpleFragListBuilder" default="true"/>
<!-- multi-colored tag FragmentsBuilder --> <!-- Configure the single fragListBuilder -->
<fragmentsBuilder name="colored" class="org.apache.solr.highlight.MultiColoredScoreOrderFragmentsBuilder" default="true"/> <fragListBuilder name="single" class="org.apache.solr.highlight.SingleFragListBuilder"/>
</highlighting>
<!-- multi-colored tag FragmentsBuilder -->
<fragmentsBuilder name="colored" class="org.apache.solr.highlight.MultiColoredScoreOrderFragmentsBuilder" default="true"/>
</highlighting>
</searchComponent> </searchComponent>
<!-- An example dedup update processor that creates the "id" field on the fly <!-- An example dedup update processor that creates the "id" field on the fly

View File

@ -29,6 +29,7 @@ public interface HighlightParams {
public static final String INCREMENT = HIGHLIGHT+".increment"; public static final String INCREMENT = HIGHLIGHT+".increment";
public static final String MAX_CHARS = HIGHLIGHT+".maxAnalyzedChars"; public static final String MAX_CHARS = HIGHLIGHT+".maxAnalyzedChars";
public static final String FORMATTER = HIGHLIGHT+".formatter"; public static final String FORMATTER = HIGHLIGHT+".formatter";
public static final String ENCODER = HIGHLIGHT+".encoder";
public static final String FRAGMENTER = HIGHLIGHT+".fragmenter"; public static final String FRAGMENTER = HIGHLIGHT+".fragmenter";
public static final String FRAG_LIST_BUILDER = HIGHLIGHT+".fragListBuilder"; public static final String FRAG_LIST_BUILDER = HIGHLIGHT+".fragListBuilder";
public static final String FRAGMENTS_BUILDER = HIGHLIGHT+".fragmentsBuilder"; public static final String FRAGMENTS_BUILDER = HIGHLIGHT+".fragmentsBuilder";

View File

@ -80,6 +80,7 @@ public class DefaultSolrHighlighter extends SolrHighlighter implements PluginInf
public void init(PluginInfo info) { public void init(PluginInfo info) {
formatters.clear(); formatters.clear();
encoders.clear();
fragmenters.clear(); fragmenters.clear();
fragListBuilders.clear(); fragListBuilders.clear();
fragmentsBuilders.clear(); fragmentsBuilders.clear();
@ -96,6 +97,12 @@ public class DefaultSolrHighlighter extends SolrHighlighter implements PluginInf
formatters.put("", fmt); formatters.put("", fmt);
formatters.put(null, fmt); formatters.put(null, fmt);
// Load the formatters
SolrEncoder enc = solrCore.initPlugins(info.getChildren("encoder"), encoders,SolrEncoder.class,null);
if (enc == null) enc = new DefaultEncoder();
encoders.put("", enc);
encoders.put(null, enc);
// Load the FragListBuilders // Load the FragListBuilders
SolrFragListBuilder fragListBuilder = solrCore.initPlugins(info.getChildren("fragListBuilder"), SolrFragListBuilder fragListBuilder = solrCore.initPlugins(info.getChildren("fragListBuilder"),
fragListBuilders, SolrFragListBuilder.class, null ); fragListBuilders, SolrFragListBuilder.class, null );
@ -126,6 +133,10 @@ public class DefaultSolrHighlighter extends SolrHighlighter implements PluginInf
formatters.put("", fmt); formatters.put("", fmt);
formatters.put(null, fmt); formatters.put(null, fmt);
SolrEncoder enc = new DefaultEncoder();
encoders.put("", enc);
encoders.put(null, enc);
SolrFragListBuilder fragListBuilder = new SimpleFragListBuilder(); SolrFragListBuilder fragListBuilder = new SimpleFragListBuilder();
fragListBuilders.put( "", fragListBuilder ); fragListBuilders.put( "", fragListBuilder );
fragListBuilders.put( null, fragListBuilder ); fragListBuilders.put( null, fragListBuilder );
@ -147,7 +158,10 @@ public class DefaultSolrHighlighter extends SolrHighlighter implements PluginInf
SolrParams params = request.getParams(); SolrParams params = request.getParams();
Highlighter highlighter = null; Highlighter highlighter = null;
highlighter = new Highlighter(getFormatter(fieldName, params), getSpanQueryScorer(query, fieldName, tokenStream, request)); highlighter = new Highlighter(
getFormatter(fieldName, params),
getEncoder(fieldName, params),
getSpanQueryScorer(query, fieldName, tokenStream, request));
highlighter.setTextFragmenter(getFragmenter(fieldName, params)); highlighter.setTextFragmenter(getFragmenter(fieldName, params));
@ -164,6 +178,7 @@ public class DefaultSolrHighlighter extends SolrHighlighter implements PluginInf
SolrParams params = request.getParams(); SolrParams params = request.getParams();
Highlighter highlighter = new Highlighter( Highlighter highlighter = new Highlighter(
getFormatter(fieldName, params), getFormatter(fieldName, params),
getEncoder(fieldName, params),
getQueryScorer(query, fieldName, request)); getQueryScorer(query, fieldName, request));
highlighter.setTextFragmenter(getFragmenter(fieldName, params)); highlighter.setTextFragmenter(getFragmenter(fieldName, params));
return highlighter; return highlighter;
@ -249,6 +264,24 @@ public class DefaultSolrHighlighter extends SolrHighlighter implements PluginInf
return formatter.getFormatter( fieldName, params ); return formatter.getFormatter( fieldName, params );
} }
/**
* Return an {@link org.apache.lucene.search.highlight.Encoder} appropriate for this field. If an encoder
* has not been configured for this field, fall back to the configured
* default or the solr default ({@link org.apache.lucene.search.highlight.DefaultEncoder}).
*
* @param fieldName The name of the field
* @param params The params controlling Highlighting
* @return An appropriate {@link org.apache.lucene.search.highlight.Encoder}.
*/
protected Encoder getEncoder(String fieldName, SolrParams params){
String str = params.getFieldParam( fieldName, HighlightParams.ENCODER );
SolrEncoder encoder = encoders.get( str );
if( encoder == null ) {
throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, "Unknown encoder: "+str );
}
return encoder.getEncoder( fieldName, params );
}
/** /**
* Return a {@link org.apache.lucene.search.highlight.Fragmenter} appropriate for this field. If a fragmenter * Return a {@link org.apache.lucene.search.highlight.Fragmenter} appropriate for this field. If a fragmenter
* has not been configured for this field, fall back to the configured * has not been configured for this field, fall back to the configured

View File

@ -43,6 +43,10 @@ public abstract class SolrHighlighter
protected final Map<String,SolrFormatter> formatters = protected final Map<String,SolrFormatter> formatters =
new HashMap<String, SolrFormatter>(); new HashMap<String, SolrFormatter>();
// Thread safe registry
protected final Map<String,SolrEncoder> encoders =
new HashMap<String, SolrEncoder>();
// Thread safe registry // Thread safe registry
protected final Map<String,SolrFragmenter> fragmenters = protected final Map<String,SolrFragmenter> fragmenters =
new HashMap<String, SolrFragmenter>() ; new HashMap<String, SolrFragmenter>() ;