Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

SOLR-516: add hl.maxAlternateFieldLength

Mike Klaase1aa232a5173
diff --git a/CHANGES.txt b/CHANGES.txt
index c4c8662a899..e5b5c2ad04b 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -111,11 +111,11 @@ New Features
13. SOLR-225: Enable pluggable highlighting classes. Allow configurable
highlighting formatters and Fragmenters. (ryan)
-14. SOLR-273/376/452: Added hl.maxAnalyzedChars highlighting parameter, defaulting
+14. SOLR-273/376/452/516: Added hl.maxAnalyzedChars highlighting parameter, defaulting
to 50k, hl.alternateField, which allows the specification of a backup
field to use as summary if no keywords are matched, and hl.mergeContiguous,
which combines fragments if they are adjacent in the source document.
- (klaas, Grant Ingersoll via klaas)
+ (klaas, Grant Ingersoll, Koji Sekiguchi via klaas)
15. SOLR-291: Control maximum number of documents to cache for any entry
in the queryResultCache via queryResultMaxDocsCached solrconfig.xml
diff --git a/src/java/org/apache/solr/common/params/HighlightParams.java b/src/java/org/apache/solr/common/params/HighlightParams.java
index 78510ff83b6..91aa12048f0 100644
--- a/src/java/org/apache/solr/common/params/HighlightParams.java
+++ b/src/java/org/apache/solr/common/params/HighlightParams.java
@@ -32,6 +32,7 @@ public interface HighlightParams {
public static final String FRAGMENTER = HIGHLIGHT+".fragmenter";
public static final String FIELD_MATCH = HIGHLIGHT+".requireFieldMatch";
public static final String ALTERNATE_FIELD = HIGHLIGHT+".alternateField";
+ public static final String ALTERNATE_FIELD_LENGTH = HIGHLIGHT+".maxAlternateFieldLength";
public static final String MERGE_CONTIGUOUS_FRAGMENTS = HIGHLIGHT + ".mergeContiguous";
// Formatter
diff --git a/src/java/org/apache/solr/highlight/DefaultSolrHighlighter.java b/src/java/org/apache/solr/highlight/DefaultSolrHighlighter.java
index 86c4ce90a6f..aa7793dffc5 100644
--- a/src/java/org/apache/solr/highlight/DefaultSolrHighlighter.java
+++ b/src/java/org/apache/solr/highlight/DefaultSolrHighlighter.java
@@ -23,6 +23,7 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
+import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;
@@ -273,9 +274,24 @@ public class DefaultSolrHighlighter extends SolrHighlighter
if (summaries == null || summaries.length == 0) {
String alternateField = req.getParams().getFieldParam(fieldName, HighlightParams.ALTERNATE_FIELD);
if (alternateField != null && alternateField.length() > 0) {
- String[] altTexts = doc.getValues(alternateField);
- if (altTexts != null && altTexts.length > 0)
- docSummaries.add(fieldName, altTexts);
+ String[] altTexts = doc.getValues(alternateField);
+ if (altTexts != null && altTexts.length > 0){
+ int alternateFieldLen = req.getParams().getFieldInt(fieldName, HighlightParams.ALTERNATE_FIELD_LENGTH,0);
+ if( alternateFieldLen <= 0 ){
+ docSummaries.add(fieldName, altTexts);
+ }
+ else{
+ List<String> altList = new ArrayList<String>();
+ int len = 0;
+ for( String altText: altTexts ){
+ altList.add( len + altText.length() > alternateFieldLen ?
+ altText.substring( 0, alternateFieldLen - len ) : altText );
+ len += altText.length();
+ if( len >= alternateFieldLen ) break;
+ }
+ docSummaries.add(fieldName, altList);
+ }
+ }
}
}
diff --git a/src/test/org/apache/solr/highlight/HighlighterTest.java b/src/test/org/apache/solr/highlight/HighlighterTest.java
index 897c7f4afdf..ad8519ac1b1 100755
--- a/src/test/org/apache/solr/highlight/HighlighterTest.java
+++ b/src/test/org/apache/solr/highlight/HighlighterTest.java
@@ -470,5 +470,15 @@ public class HighlighterTest extends AbstractSolrTestCase {
"//lst[@name='highlighting']/lst[@name='1' and count(*)=1]",
"//lst[@name='highlighting']/lst[@name='1']/arr[@name='t_text']/str[.='1']"
);
+
+ // with an alternate + max length
+ args.put("hl.alternateField", "t_text");
+ args.put("hl.maxAlternateFieldLength", "15");
+ sumLRF = h.getRequestFactory("standard", 0, 200, args);
+ assertQ("Alternate summarization",
+ sumLRF.makeRequest("tv_text:keyword"),
+ "//lst[@name='highlighting']/lst[@name='1' and count(*)=1]",
+ "//lst[@name='highlighting']/lst[@name='1']/arr[@name='t_text']/str[.='a piece of text']"
+ );
}
}