From 6fbdff181abc4b0c6a12c063d852a2aaca011025 Mon Sep 17 00:00:00 2001 From: Koji Sekiguchi Date: Sat, 7 Aug 2010 11:34:39 +0000 Subject: [PATCH] SOLR-2030: FVH uses SolrEncoder git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@983212 13f79535-47bb-0310-9956-ffa450edef68 --- solr/CHANGES.txt | 2 + solr/example/solr/conf/solrconfig.xml | 15 ++++- .../solr/common/params/HighlightParams.java | 4 +- .../highlight/DefaultSolrHighlighter.java | 17 +++-- ...ultiColoredScoreOrderFragmentsBuilder.java | 62 ------------------- .../MultiColoredSimpleFragmentsBuilder.java | 62 ------------------- .../highlight/ScoreOrderFragmentsBuilder.java | 16 ++--- .../highlight/SimpleFragmentsBuilder.java | 20 +++--- .../solr/highlight/SolrFragmentsBuilder.java | 51 +++++++++++---- 9 files changed, 85 insertions(+), 164 deletions(-) delete mode 100644 solr/src/java/org/apache/solr/highlight/MultiColoredScoreOrderFragmentsBuilder.java delete mode 100644 solr/src/java/org/apache/solr/highlight/MultiColoredSimpleFragmentsBuilder.java diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index d6effe55afc..662b6011801 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -222,6 +222,8 @@ New Features * SOLR-2021: Add SolrEncoder plugin to Highlighter. (koji) +* SOLR-2030: Make FastVectorHighlighter use of SolrEncoder. (koji) + Optimizations ---------------------- diff --git a/solr/example/solr/conf/solrconfig.xml b/solr/example/solr/conf/solrconfig.xml index 9d54b246d54..f41897dc8c4 100755 --- a/solr/example/solr/conf/solrconfig.xml +++ b/solr/example/solr/conf/solrconfig.xml @@ -1018,8 +1018,21 @@ + + + - + + + ,, + ,, + ,, + ,, + ,]]> + ]]> + + diff --git a/solr/src/common/org/apache/solr/common/params/HighlightParams.java b/solr/src/common/org/apache/solr/common/params/HighlightParams.java index ca60f0691ee..50fdb875522 100644 --- a/solr/src/common/org/apache/solr/common/params/HighlightParams.java +++ b/solr/src/common/org/apache/solr/common/params/HighlightParams.java @@ -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"; diff --git a/solr/src/java/org/apache/solr/highlight/DefaultSolrHighlighter.java b/solr/src/java/org/apache/solr/highlight/DefaultSolrHighlighter.java index 6225ac3ddb8..d067456ac44 100644 --- a/solr/src/java/org/apache/solr/highlight/DefaultSolrHighlighter.java +++ b/solr/src/java/org/apache/solr/highlight/DefaultSolrHighlighter.java @@ -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 diff --git a/solr/src/java/org/apache/solr/highlight/MultiColoredScoreOrderFragmentsBuilder.java b/solr/src/java/org/apache/solr/highlight/MultiColoredScoreOrderFragmentsBuilder.java deleted file mode 100644 index ce9e6987c93..00000000000 --- a/solr/src/java/org/apache/solr/highlight/MultiColoredScoreOrderFragmentsBuilder.java +++ /dev/null @@ -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$"; - } -} diff --git a/solr/src/java/org/apache/solr/highlight/MultiColoredSimpleFragmentsBuilder.java b/solr/src/java/org/apache/solr/highlight/MultiColoredSimpleFragmentsBuilder.java deleted file mode 100644 index 39ffb38f4ab..00000000000 --- a/solr/src/java/org/apache/solr/highlight/MultiColoredSimpleFragmentsBuilder.java +++ /dev/null @@ -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$"; - } -} diff --git a/solr/src/java/org/apache/solr/highlight/ScoreOrderFragmentsBuilder.java b/solr/src/java/org/apache/solr/highlight/ScoreOrderFragmentsBuilder.java index 165e37fda2e..adf6364bacd 100644 --- a/solr/src/java/org/apache/solr/highlight/ScoreOrderFragmentsBuilder.java +++ b/solr/src/java/org/apache/solr/highlight/ScoreOrderFragmentsBuilder.java @@ -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 ); } /////////////////////////////////////////////////////////////////////// diff --git a/solr/src/java/org/apache/solr/highlight/SimpleFragmentsBuilder.java b/solr/src/java/org/apache/solr/highlight/SimpleFragmentsBuilder.java index ac9a35411bf..19c756adb69 100644 --- a/solr/src/java/org/apache/solr/highlight/SimpleFragmentsBuilder.java +++ b/solr/src/java/org/apache/solr/highlight/SimpleFragmentsBuilder.java @@ -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"; diff --git a/solr/src/java/org/apache/solr/highlight/SolrFragmentsBuilder.java b/solr/src/java/org/apache/solr/highlight/SolrFragmentsBuilder.java index 2af5b110ab5..7ca00769aa7 100644 --- a/solr/src/java/org/apache/solr/highlight/SolrFragmentsBuilder.java +++ b/solr/src/java/org/apache/solr/highlight/SolrFragmentsBuilder.java @@ -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 { - - /** init will be called just once, immediately after creation. - *

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 = ""; + public static final String DEFAULT_POST_TAGS = ""; /** - * 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 ); }