SOLR-376: hl.alternateField

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@583979 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Mike Klaas 2007-10-11 22:32:48 +00:00
parent 0ef485498a
commit ed5ff37639
4 changed files with 47 additions and 4 deletions

View File

@ -87,8 +87,9 @@ New Features
13. SOLR-225: Enable pluggable highlighting classes. Allow configurable 13. SOLR-225: Enable pluggable highlighting classes. Allow configurable
highlighting formatters and Fragmenters. (ryan) highlighting formatters and Fragmenters. (ryan)
14. SOLR-273: Added hl.maxAnalyzedChars highlighting parameter, defaulting to 14. SOLR-273/376: Added hl.maxAnalyzedChars highlighting parameter, defaulting to
50k (klaas) 50k. Also add hl.alternateField, which allows the specification of a backup
field to use as summary if no keywords are matched. (klaas)
15. SOLR-291: Control maximum number of documents to cache for any entry 15. SOLR-291: Control maximum number of documents to cache for any entry
in the queryResultCache via queryResultMaxDocsCached solrconfig.xml in the queryResultCache via queryResultMaxDocsCached solrconfig.xml
@ -135,7 +136,6 @@ New Features
to the detailed field information from the solrj client API. to the detailed field information from the solrj client API.
(Grant Ingersoll via ehatcher) (Grant Ingersoll via ehatcher)
Changes in runtime behavior Changes in runtime behavior
Optimizations Optimizations

View File

@ -31,6 +31,7 @@ public interface HighlightParams {
public static final String FORMATTER = HIGHLIGHT+".formatter"; public static final String FORMATTER = HIGHLIGHT+".formatter";
public static final String FRAGMENTER = HIGHLIGHT+".fragmenter"; public static final String FRAGMENTER = HIGHLIGHT+".fragmenter";
public static final String FIELD_MATCH = HIGHLIGHT+".requireFieldMatch"; public static final String FIELD_MATCH = HIGHLIGHT+".requireFieldMatch";
public static final String ALTERNATE_FIELD = HIGHLIGHT+".alternateField";
// Formatter // Formatter
public static final String SIMPLE = "simple"; public static final String SIMPLE = "simple";

View File

@ -275,7 +275,7 @@ public class SolrHighlighter
Highlighter highlighter = getHighlighter(query, fieldName, req); Highlighter highlighter = getHighlighter(query, fieldName, req);
int numFragments = getMaxSnippets(fieldName, params); int numFragments = getMaxSnippets(fieldName, params);
String[] summaries; String[] summaries = null;
TextFragment[] frag; TextFragment[] frag;
if (docTexts.length == 1) { if (docTexts.length == 1) {
// single-valued field // single-valued field
@ -309,6 +309,16 @@ public class SolrHighlighter
if (summaries.length > 0) if (summaries.length > 0)
docSummaries.add(fieldName, summaries); docSummaries.add(fieldName, summaries);
} }
// no summeries made, copy text from alternate field
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 printId = schema.printableUniqueKey(doc); String printId = schema.printableUniqueKey(doc);
fragments.add(printId == null ? null : printId, docSummaries); fragments.add(printId == null ? null : printId, docSummaries);

View File

@ -387,4 +387,36 @@ public class HighlighterTest extends AbstractSolrTestCase {
"//lst[@name='1']/arr[@name='tv_text']/str[.='a <em>long</em> days night this should be a piece of text which is is is is is is is is is is is is is is is is is is is is is is is is isis is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is sufficiently lengthly to produce multiple fragments which are not concatenated at all']" "//lst[@name='1']/arr[@name='tv_text']/str[.='a <em>long</em> days night this should be a piece of text which is is is is is is is is is is is is is is is is is is is is is is is is isis is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is is sufficiently lengthly to produce multiple fragments which are not concatenated at all']"
); );
} }
public void testAlternateSummary() {
//long document
assertU(adoc("tv_text", "keyword is only here",
"t_text", "a piece of text to be substituted",
"id", "1"));
assertU(commit());
assertU(optimize());
// do summarization
HashMap<String,String> args = new HashMap<String,String>();
args.put("hl", "true");
args.put("hl.fragsize","0");
args.put("hl.fl", "t_text");
TestHarness.LocalRequestFactory sumLRF = h.getRequestFactory(
"standard", 0, 200, args);
// no alternate
assertQ("Alternate summarization",
sumLRF.makeRequest("tv_text:keyword"),
"//lst[@name='highlighting']/lst[@name='1']",
"//lst[@name='highlighting']/lst[@name='1' and count(*)=0]"
);
// with an alternate
args.put("hl.alternateField", "id");
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[.='1']"
);
}
} }