SOLR-2030: FVH uses SolrEncoder

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@983212 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Koji Sekiguchi 2010-08-07 11:34:39 +00:00
parent c2ad126364
commit 6fbdff181a
9 changed files with 85 additions and 164 deletions

View File

@ -222,6 +222,8 @@ New Features
* SOLR-2021: Add SolrEncoder plugin to Highlighter. (koji)
* SOLR-2030: Make FastVectorHighlighter use of SolrEncoder. (koji)
Optimizations
----------------------

View File

@ -1018,8 +1018,21 @@
<!-- Configure the single fragListBuilder -->
<fragListBuilder name="single" class="org.apache.solr.highlight.SingleFragListBuilder"/>
<!-- default tag FragmentsBuilder -->
<fragmentsBuilder name="default" class="org.apache.solr.highlight.ScoreOrderFragmentsBuilder" default="true"/>
<!-- multi-colored tag FragmentsBuilder -->
<fragmentsBuilder name="colored" class="org.apache.solr.highlight.MultiColoredScoreOrderFragmentsBuilder" default="true"/>
<fragmentsBuilder name="colored" class="org.apache.solr.highlight.ScoreOrderFragmentsBuilder">
<lst name="defaults">
<str name="hl.tag.pre"><![CDATA[
<b style="background:yellow">,<b style="background:lawgreen">,
<b style="background:aquamarine">,<b style="background:magenta">,
<b style="background:palegreen">,<b style="background:coral">,
<b style="background:wheat">,<b style="background:khaki">,
<b style="background:lime">,<b style="background:deepskyblue">]]></str>
<str name="hl.tag.post"><![CDATA[</b>]]></str>
</lst>
</fragmentsBuilder>
</highlighting>
</searchComponent>

View File

@ -42,7 +42,9 @@ public interface HighlightParams {
public static final String MERGE_CONTIGUOUS_FRAGMENTS = HIGHLIGHT + ".mergeContiguous";
public static final String USE_FVH = HIGHLIGHT + ".useFastVectorHighlighter";
public static final String USE_FVH = HIGHLIGHT + ".useFastVectorHighlighter";
public static final String TAG_PRE = HIGHLIGHT + ".tag.pre";
public static final String TAG_POST = HIGHLIGHT + ".tag.post";
// Formatter
public static final String SIMPLE = "simple";

View File

@ -311,12 +311,16 @@ public class DefaultSolrHighlighter extends SolrHighlighter implements PluginInf
}
protected FragmentsBuilder getFragmentsBuilder( SolrParams params ){
return getSolrFragmentsBuilder( params ).getFragmentsBuilder( params );
}
private SolrFragmentsBuilder getSolrFragmentsBuilder( SolrParams params ){
String fb = params.get( HighlightParams.FRAGMENTS_BUILDER );
SolrFragmentsBuilder solrFb = fragmentsBuilders.get( fb );
if( solrFb == null ){
throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, "Unknown fragmentsBuilder: " + fb );
}
return solrFb.getFragmentsBuilder( params );
return solrFb;
}
/**
@ -361,6 +365,7 @@ public class DefaultSolrHighlighter extends SolrHighlighter implements PluginInf
getFragListBuilder( params ),
getFragmentsBuilder( params ) );
FieldQuery fieldQuery = fvh.getFieldQuery( query );
SolrFragmentsBuilder solrFb = getSolrFragmentsBuilder( params );
// Highlight each document
DocIterator iterator = docs.iterator();
@ -371,7 +376,7 @@ public class DefaultSolrHighlighter extends SolrHighlighter implements PluginInf
for (String fieldName : fieldNames) {
fieldName = fieldName.trim();
if( useFastVectorHighlighter( params, schema, fieldName ) )
doHighlightingByFastVectorHighlighter( fvh, fieldQuery, req, docSummaries, docId, doc, fieldName );
doHighlightingByFastVectorHighlighter( fvh, fieldQuery, solrFb, req, docSummaries, docId, doc, fieldName );
else
doHighlightingByHighlighter( query, req, docSummaries, docId, doc, fieldName );
}
@ -499,11 +504,15 @@ public class DefaultSolrHighlighter extends SolrHighlighter implements PluginInf
}
private void doHighlightingByFastVectorHighlighter( FastVectorHighlighter highlighter, FieldQuery fieldQuery,
SolrQueryRequest req, NamedList docSummaries, int docId, Document doc, String fieldName ) throws IOException {
SolrFragmentsBuilder solrFb, SolrQueryRequest req, NamedList docSummaries, int docId, Document doc,
String fieldName ) throws IOException {
SolrParams params = req.getParams();
String[] snippets = highlighter.getBestFragments( fieldQuery, req.getSearcher().getReader(), docId, fieldName,
params.getFieldInt( fieldName, HighlightParams.FRAGSIZE, 100 ),
params.getFieldInt( fieldName, HighlightParams.SNIPPETS, 1 ) );
params.getFieldInt( fieldName, HighlightParams.SNIPPETS, 1 ),
solrFb.getPreTags( params, fieldName ),
solrFb.getPostTags( params, fieldName ),
getEncoder( fieldName, params ) );
if( snippets != null && snippets.length > 0 )
docSummaries.add( fieldName, snippets );
else

View File

@ -1,62 +0,0 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.solr.highlight;
import org.apache.lucene.search.vectorhighlight.BaseFragmentsBuilder;
import org.apache.lucene.search.vectorhighlight.FragmentsBuilder;
import org.apache.lucene.search.vectorhighlight.ScoreOrderFragmentsBuilder;
import org.apache.solr.common.params.DefaultSolrParams;
import org.apache.solr.common.params.SolrParams;
public class MultiColoredScoreOrderFragmentsBuilder extends
HighlightingPluginBase implements SolrFragmentsBuilder {
public FragmentsBuilder getFragmentsBuilder(SolrParams params) {
numRequests++;
if( defaults != null ) {
params = new DefaultSolrParams( params, defaults );
}
return new ScoreOrderFragmentsBuilder(
BaseFragmentsBuilder.COLORED_PRE_TAGS, BaseFragmentsBuilder.COLORED_POST_TAGS );
}
///////////////////////////////////////////////////////////////////////
//////////////////////// SolrInfoMBeans methods ///////////////////////
///////////////////////////////////////////////////////////////////////
@Override
public String getDescription() {
return "MultiColoredScoreOrderFragmentsBuilder";
}
@Override
public String getSource() {
return "$URL$";
}
@Override
public String getSourceId() {
return "$Id$";
}
@Override
public String getVersion() {
return "$Revision$";
}
}

View File

@ -1,62 +0,0 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.solr.highlight;
import org.apache.lucene.search.vectorhighlight.BaseFragmentsBuilder;
import org.apache.lucene.search.vectorhighlight.FragmentsBuilder;
import org.apache.lucene.search.vectorhighlight.SimpleFragmentsBuilder;
import org.apache.solr.common.params.DefaultSolrParams;
import org.apache.solr.common.params.SolrParams;
public class MultiColoredSimpleFragmentsBuilder extends HighlightingPluginBase
implements SolrFragmentsBuilder {
public FragmentsBuilder getFragmentsBuilder(SolrParams params) {
numRequests++;
if( defaults != null ) {
params = new DefaultSolrParams( params, defaults );
}
return new SimpleFragmentsBuilder(
BaseFragmentsBuilder.COLORED_PRE_TAGS, BaseFragmentsBuilder.COLORED_POST_TAGS );
}
///////////////////////////////////////////////////////////////////////
//////////////////////// SolrInfoMBeans methods ///////////////////////
///////////////////////////////////////////////////////////////////////
@Override
public String getDescription() {
return "MultiColoredSimpleFragmentsBuilder";
}
@Override
public String getSource() {
return "$URL$";
}
@Override
public String getSourceId() {
return "$Id$";
}
@Override
public String getVersion() {
return "$Revision$";
}
}

View File

@ -18,19 +18,13 @@
package org.apache.solr.highlight;
import org.apache.lucene.search.vectorhighlight.FragmentsBuilder;
import org.apache.solr.common.params.DefaultSolrParams;
import org.apache.solr.common.params.SolrParams;
public class ScoreOrderFragmentsBuilder extends HighlightingPluginBase
implements SolrFragmentsBuilder {
public class ScoreOrderFragmentsBuilder extends SolrFragmentsBuilder {
public FragmentsBuilder getFragmentsBuilder(SolrParams params) {
numRequests++;
if( defaults != null ) {
params = new DefaultSolrParams( params, defaults );
}
return new org.apache.lucene.search.vectorhighlight.ScoreOrderFragmentsBuilder();
@Override
protected FragmentsBuilder getFragmentsBuilder(String[] preTags,
String[] postTags) {
return new org.apache.lucene.search.vectorhighlight.ScoreOrderFragmentsBuilder( preTags, postTags );
}
///////////////////////////////////////////////////////////////////////

View File

@ -18,21 +18,19 @@
package org.apache.solr.highlight;
import org.apache.lucene.search.vectorhighlight.FragmentsBuilder;
import org.apache.solr.common.params.DefaultSolrParams;
import org.apache.solr.common.params.SolrParams;
public class SimpleFragmentsBuilder extends HighlightingPluginBase implements
SolrFragmentsBuilder {
public class SimpleFragmentsBuilder extends SolrFragmentsBuilder {
public FragmentsBuilder getFragmentsBuilder(SolrParams params) {
numRequests++;
if( defaults != null ) {
params = new DefaultSolrParams( params, defaults );
}
return new org.apache.lucene.search.vectorhighlight.SimpleFragmentsBuilder();
@Override
protected FragmentsBuilder getFragmentsBuilder(String[] preTags,
String[] postTags) {
return new org.apache.lucene.search.vectorhighlight.SimpleFragmentsBuilder( preTags, postTags );
}
///////////////////////////////////////////////////////////////////////
//////////////////////// SolrInfoMBeans methods ///////////////////////
///////////////////////////////////////////////////////////////////////
@Override
public String getDescription() {
return "SimpleFragmentsBuilder";

View File

@ -18,26 +18,53 @@
package org.apache.solr.highlight;
import org.apache.lucene.search.vectorhighlight.FragmentsBuilder;
import org.apache.solr.common.params.DefaultSolrParams;
import org.apache.solr.common.params.HighlightParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.core.SolrInfoMBean;
import org.apache.solr.util.plugin.NamedListInitializedPlugin;
public interface SolrFragmentsBuilder extends SolrInfoMBean, NamedListInitializedPlugin {
/** <code>init</code> will be called just once, immediately after creation.
* <p>The args are user-level initialization parameters that
* may be specified when declaring a request handler in
* solrconfig.xml
*/
public void init( NamedList args);
public abstract class SolrFragmentsBuilder extends HighlightingPluginBase
implements SolrInfoMBean, NamedListInitializedPlugin {
public static final String DEFAULT_PRE_TAGS = "<em>";
public static final String DEFAULT_POST_TAGS = "</em>";
/**
* Return a FragmentsBuilder appropriate for this field.
* Return a {@link org.apache.lucene.search.vectorhighlight.FragmentsBuilder} appropriate for this field.
*
* @param fieldName The name of the field
* @param params The params controlling Highlighting
* @return An appropriate FragmentsBuilder.
* @return An appropriate {@link org.apache.lucene.search.vectorhighlight.FragmentsBuilder}.
*/
public FragmentsBuilder getFragmentsBuilder( SolrParams params );
public FragmentsBuilder getFragmentsBuilder(SolrParams params) {
numRequests++;
return getFragmentsBuilder( getPreTags( params, null ), getPostTags( params, null ) );
}
public String[] getPreTags( SolrParams params, String fieldName ){
return getTags( params, HighlightParams.TAG_PRE, fieldName, DEFAULT_PRE_TAGS );
}
public String[] getPostTags( SolrParams params, String fieldName ){
return getTags( params, HighlightParams.TAG_POST, fieldName, DEFAULT_POST_TAGS );
}
private String[] getTags( SolrParams params, String paramName, String fieldName, String def ){
if( defaults != null ) {
params = new DefaultSolrParams( params, defaults );
}
String value = null;
if( fieldName == null )
value = params.get( paramName, def );
else
value = params.getFieldParam( fieldName, paramName, def );
String[] tags = value.split( "," );
for( int i = 0; i < tags.length; i++ ){
tags[i] = tags[i].trim();
}
return tags;
}
protected abstract FragmentsBuilder getFragmentsBuilder( String[] preTags, String[] postTags );
}