Java Code Examples for org.apache.solr.common.params.HighlightParams

The following examples show how to use org.apache.solr.common.params.HighlightParams. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: dubbox   Source File: AbstractSolrQuery.java    License: Apache License 2.0 6 votes vote down vote up
private void appendHighlightFormatOptions(HighlightOptions options, SolrQueryMethod queryMethod) {
	String formatter = queryMethod.getHighlightFormatter();
	if (formatter != null) {
		options.setFormatter(formatter);
	}
	String highlightPrefix = queryMethod.getHighlightPrefix();
	if (highlightPrefix != null) {
		if (isSimpleHighlightingOption(formatter)) {
			options.setSimplePrefix(highlightPrefix);
		} else {
			options.addHighlightParameter(new HighlightParameter(HighlightParams.TAG_PRE, highlightPrefix));
		}
	}
	String highlightPostfix = queryMethod.getHighlightPostfix();
	if (highlightPostfix != null) {
		if (isSimpleHighlightingOption(formatter)) {
			options.setSimplePostfix(highlightPostfix);
		} else {
			options.addHighlightParameter(new HighlightParameter(HighlightParams.TAG_POST, highlightPostfix));
		}
	}
}
 
Example 2
Source Project: querqy   Source File: DefaultQuerqyDismaxQParserTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testThatHighlightingIsApplied() {
    SolrQueryRequest req = req("q", "a",
            DisMaxParams.QF, "f1",
            HighlightParams.HIGHLIGHT, "true",
            HighlightParams.FIELDS, "f1",
            HighlightParams.SIMPLE_PRE, "PRE",
            HighlightParams.SIMPLE_POST, "POST",
            "defType", "querqy",
            "debugQuery", "true");

    assertQ("Highlighting not working",
            req,
            "//lst[@name='highlighting']//arr[@name='f1']/str[text()='PREaPOST']"
    );
    req.close();

}
 
Example 3
@Test
public void highlightingSnippetsFragSizeTest()
{
    SolrServletRequest req = areq(params( "q", "name:long", "qt", "/afts", "start", "0", "rows", "5",
            HighlightParams.HIGHLIGHT, "true",
            HighlightParams.Q, "long",
            HighlightParams.FIELDS, "content,name,title",
            HighlightParams.SNIPPETS, "4",
            HighlightParams.FRAGSIZE, "40"),
            "{\"locales\":[\"en\"], \"tenants\": [ \"\" ]}");

    assertQ(req,
            "*[count(//lst[@name='highlighting']/lst)=2]",
            "//lst[@name='highlighting']/lst[1]/arr[@name='name']/str[.='some very <em>long</em> name']",
            "//lst[@name='highlighting']/lst[1]/arr[@name='title']/str[.='title1 is very <em>long</em>']",
            "//lst[@name='highlighting']/lst[2]/arr[@name='name']/str[.='this is some <em>long</em> text.  It has the']",
            "//lst[@name='highlighting']/lst[2]/arr[@name='name']/str[.=' word <em>long</em> in many places.  In fact, it has']",
            "//lst[@name='highlighting']/lst[2]/arr[@name='name']/str[.=' <em>long</em> on some different fragments.  Let us']",
            "//lst[@name='highlighting']/lst[2]/arr[@name='name']/str[.=' see what happens to <em>long</em> in this case.']");
}
 
Example 4
@Test
public void highlightingMergeContinuousFragmentsTest()
{
    SolrServletRequest req = areq(params( "q", "name:long", "qt", "/afts", "start", "0", "rows", "5",
            HighlightParams.HIGHLIGHT, "true",
            HighlightParams.Q, "'some long'",
            HighlightParams.FIELDS, "name",
            HighlightParams.MERGE_CONTIGUOUS_FRAGMENTS, "true",
            HighlightParams.SIMPLE_PRE, "{",
            HighlightParams.SIMPLE_POST, "}",
            HighlightParams.SNIPPETS, String.valueOf(4),
            HighlightParams.FRAGSIZE, String.valueOf(40)),
            "{\"locales\":[\"en\"], \"tenants\": [ \"\" ]}");

    assertQ(req,
            "*[count(//lst[@name='highlighting']/lst/arr)=1]",
            "//lst[@name='highlighting']/lst/arr[@name='name']/str[.='this is {some} {long} text.  It has the word long in many places.  In fact, it has long on some different fragments.  Let us see what happens to long in this case.']"
    );
}
 
Example 5
@Test
public void highlightingPrePostTest()
{
    SolrServletRequest req = areq(params( "q", "name:long", "qt", "/afts", "start", "0", "rows", "5",
            HighlightParams.HIGHLIGHT, "true",
            HighlightParams.Q, "long",
            HighlightParams.FIELDS, "content,name,title",
            HighlightParams.SIMPLE_PRE, "<al>",
            HighlightParams.SIMPLE_POST, "<fresco>",
            HighlightParams.SNIPPETS, String.valueOf(4),
            HighlightParams.FRAGSIZE, String.valueOf(40)),
            "{\"locales\":[\"en\"], \"tenants\": [ \"\" ]}");

    assertQ(req,
            "*[count(//lst[@name='highlighting']/lst)=2]",
            "//lst[@name='highlighting']/lst[1]/arr[@name='name']/str[.='some very <al>long<fresco> name']",
            "//lst[@name='highlighting']/lst[1]/arr[@name='title']/str[.='title1 is very <al>long<fresco>']",
            "//lst[@name='highlighting']/lst[2]/arr[@name='name']/str[.='this is some <al>long<fresco> text.  It has the']",
            "//lst[@name='highlighting']/lst[2]/arr[@name='name']/str[.=' word <al>long<fresco> in many places.  In fact, it has']",
            "//lst[@name='highlighting']/lst[2]/arr[@name='name']/str[.=' <al>long<fresco> on some different fragments.  Let us']",
            "//lst[@name='highlighting']/lst[2]/arr[@name='name']/str[.=' see what happens to <al>long<fresco> in this case.']");
}
 
Example 6
Source Project: solr-researcher   Source File: TestDymReSearcher.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testFacetAndHighlightWithCommonMisspellings() {
  assertQ(req(CommonParams.QT, "standardResWithCommonMisspellings", 
      CommonParams.Q, "foo:bobo AND foo:marley",
      SpellingParams.SPELLCHECK_COLLATE, "true", 
      SpellingParams.SPELLCHECK_BUILD, "true", 
      SpellingParams.SPELLCHECK_COUNT, "10", 
      SpellingParams.SPELLCHECK_EXTENDED_RESULTS, "true",
      FacetParams.FACET, "true", 
      FacetParams.FACET_FIELD, "foo", 
      FacetParams.FACET_FIELD, "id",
      FacetParams.FACET_FIELD, "bar", 
      FacetParams.FACET_QUERY, "id:[0 TO 20]", 
      FacetParams.FACET_QUERY, "id:[1 TO 100]",
      HighlightParams.HIGHLIGHT, "true", 
      HighlightParams.FIELDS, "foo",
      DymReSearcher.COMPONENT_NAME, "true",
      SpellCheckComponent.COMPONENT_NAME, "true")
      ,"//result[@name='spellchecked_response'][@numFound='3']"
      ,"//result[@name='response'][@numFound='0']"
      ,"//arr[@name='extended_spellchecker_suggestions']/str[1][.='foo:bono AND foo:marley']"
      ,"//lst[@name='spellchecked_facet_counts']/lst[@name='facet_fields']/lst[@name='foo']/int[@name='bob'][.='3']"
      ,"//lst[@name='spellchecked_facet_counts']/lst[@name='facet_fields']/lst[@name='foo']/int[@name='marley'][.='3']"
      ,"//lst[@name='spellchecked_highlighting']/lst[@name='9']/arr[@name='foo']/str[1]"
      );
}
 
Example 7
@Test
public void highlightingPluralsTest()
{
    SolrServletRequest req = areq(params( "q", "name:plural", "qt", "/afts", "start", "0", "rows", "5",
            HighlightParams.HIGHLIGHT, "true",
            //HighlightParams.Q, "lon*",
            HighlightParams.FIELDS, "name",
            HighlightParams.HIGHLIGHT_MULTI_TERM, "false",
            HighlightParams.SIMPLE_PRE, "{",
            HighlightParams.SIMPLE_POST, "}",
            HighlightParams.SNIPPETS, String.valueOf(1),
            HighlightParams.FRAGSIZE, String.valueOf(100)),
            "{\"locales\":[\"en\"], \"tenants\": [ \"\" ]}");

    assertQ(req,
            "*[count(//lst[@name='highlighting']/lst)=1]",
            "*[count(//lst[@name='highlighting']/lst/arr[@name='name'])=1]",
            "//lst[@name='highlighting']/lst[1]/arr[@name='name']/str[.='MixedCabbageString and {plurals} and discussion']");
}
 
Example 8
@Test
public void highlightingStemmingTest()
{
    SolrServletRequest req = areq(params( "q", "name:discuss", "qt", "/afts", "start", "0", "rows", "5",
            HighlightParams.HIGHLIGHT, "true",
            //HighlightParams.Q, "lon*",
            HighlightParams.FIELDS, "name",
            HighlightParams.HIGHLIGHT_MULTI_TERM, "false",
            HighlightParams.SIMPLE_PRE, "{",
            HighlightParams.SIMPLE_POST, "}",
            HighlightParams.SNIPPETS, String.valueOf(1),
            HighlightParams.FRAGSIZE, String.valueOf(100)),
            "{\"locales\":[\"en\"], \"tenants\": [ \"\" ]}");

    assertQ(req,
            "*[count(//lst[@name='highlighting']/lst)=1]",
            "*[count(//lst[@name='highlighting']/lst/arr[@name='name'])=1]",
            "//lst[@name='highlighting']/lst[1]/arr[@name='name']/str[.='MixedCabbageString and plurals and {discussion}']");
}
 
Example 9
@Test
public void highlightingBooleanConjunctionTest()
{
    SolrServletRequest req = areq(params( "q", "title:(is AND long)", "qt", "/afts", "start", "0", "rows", "5",
            HighlightParams.HIGHLIGHT, "true",
            HighlightParams.FIELDS, "title",
            HighlightParams.HIGHLIGHT_MULTI_TERM, "false",
            HighlightParams.SIMPLE_PRE, "{",
            HighlightParams.SIMPLE_POST, "}"),
            "{\"locales\":[\"en\"], \"tenants\": [ \"\" ]}");

    assertQ(req,
            "*[count(//lst[@name='highlighting']/lst)=1]",
            "*[count(//lst[@name='highlighting']/lst/arr[@name='title'])=1]",
            "//lst[@name='highlighting']/lst[1]/arr[@name='title']/str[.='title1 {is} very {long}']");
}
 
Example 10
Source Project: solr-researcher   Source File: TestDymReSearcher.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testFacetAndHighlight() {
  assertQ(req(CommonParams.QT, "standardGoodSuggestion", 
      CommonParams.Q, "foo:bobo AND foo:marley",
      SpellingParams.SPELLCHECK_COLLATE, "true", 
      SpellingParams.SPELLCHECK_BUILD, "true", 
      SpellingParams.SPELLCHECK_COUNT, "10", 
      SpellingParams.SPELLCHECK_EXTENDED_RESULTS, "true",
      FacetParams.FACET, "true", 
      FacetParams.FACET_FIELD, "foo", 
      FacetParams.FACET_FIELD, "id", 
      FacetParams.FACET_FIELD, "bar", 
      FacetParams.FACET_QUERY, "id:[0 TO 20]", 
      FacetParams.FACET_QUERY, "id:[1 TO 100]", 
      HighlightParams.HIGHLIGHT, "true", 
      HighlightParams.FIELDS, "foo",
      DymReSearcher.COMPONENT_NAME, "true",
      SpellCheckComponent.COMPONENT_NAME, "true")
      ,"//result[@name='spellchecked_response'][@numFound='7']"
      ,"//result[@name='response'][@numFound='0']"
      ,"//arr[@name='extended_spellchecker_suggestions']/str[1][.='foo:bob AND foo:marley']"
      ,"//lst[@name='spellchecked_facet_counts']/lst[@name='facet_fields']/lst[@name='foo']/int[@name='bob'][.='7']"
      ,"//lst[@name='spellchecked_facet_counts']/lst[@name='facet_fields']/lst[@name='foo']/int[@name='marley'][.='7']"
      ,"//lst[@name='spellchecked_highlighting']/lst[@name='2']/arr[@name='foo']/str[1]");
}
 
Example 11
Source Project: lucene-solr   Source File: HighlightComponent.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void prepare(ResponseBuilder rb) throws IOException {
  SolrParams params = rb.req.getParams();
  rb.doHighlights = solrConfigHighlighter.isHighlightingEnabled(params);
  if(rb.doHighlights){
    rb.setNeedDocList(true);
    String hlq = params.get(HighlightParams.Q);
    String hlparser = MoreObjects.firstNonNull(params.get(HighlightParams.QPARSER),
                                            params.get(QueryParsing.DEFTYPE, QParserPlugin.DEFAULT_QTYPE));
    if(hlq != null){
      try {
        QParser parser = QParser.getParser(hlq, hlparser, rb.req);
        rb.setHighlightQuery(parser.getHighlightQuery());
      } catch (SyntaxError e) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);
      }
    }
  }
}
 
Example 12
Source Project: lucene-solr   Source File: RegexFragmenter.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Fragmenter getFragmenter(String fieldName, SolrParams params )
{ 
  numRequests.inc();
  params = SolrParams.wrapDefaults(params, defaults);

  int fragsize  = params.getFieldInt(   fieldName, HighlightParams.FRAGSIZE,  LuceneRegexFragmenter.DEFAULT_FRAGMENT_SIZE );
  int increment = params.getFieldInt(   fieldName, HighlightParams.INCREMENT, LuceneRegexFragmenter.DEFAULT_INCREMENT_GAP );
  float slop    = params.getFieldFloat( fieldName, HighlightParams.SLOP,      LuceneRegexFragmenter.DEFAULT_SLOP );
  int maxchars  = params.getFieldInt(   fieldName, HighlightParams.MAX_RE_CHARS, LuceneRegexFragmenter.DEFAULT_MAX_ANALYZED_CHARS );
  String rawpat = params.getFieldParam( fieldName, HighlightParams.PATTERN,   LuceneRegexFragmenter.DEFAULT_PATTERN_RAW );

  Pattern p = rawpat == defaultPatternRaw ? defaultPattern : Pattern.compile(rawpat);

  if( fragsize <= 0 ) {
    return new NullFragmenter();
  }
  
  return new LuceneRegexFragmenter( fragsize, increment, slop, maxchars, p );
}
 
Example 13
Source Project: querqy   Source File: DefaultQuerqyDismaxQParserTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testThatHighlightingIsNotAppliedToBoostQuery() {
    SolrQueryRequest req = req("q", "o",
            DisMaxParams.QF, "f1",
            HighlightParams.HIGHLIGHT, "true",
            HighlightParams.FIELDS, "f1",
            HighlightParams.SIMPLE_PRE, "PRE",
            HighlightParams.SIMPLE_POST, "POST",
            "defType", "querqy",
            "debugQuery", "true");

    assertQ("UP token is highlighted",
            req,
            "//lst[@name='highlighting']//arr[@name='f1']/str[not(contains(.,'PREuPOST'))]"
    );
    req.close();

}
 
Example 14
Source Project: lucene-solr   Source File: DefaultSolrHighlighter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Return a {@link org.apache.lucene.search.highlight.QueryScorer} suitable for this Query and field.
 *
 * @param query       The current query
 * @param tokenStream document text tokenStream that implements reset() efficiently (e.g. CachingTokenFilter).
 *                    If it's used, call reset() first.
 * @param fieldName   The name of the field
 * @param request     The SolrQueryRequest
 */
protected QueryScorer getSpanQueryScorer(Query query, String fieldName, TokenStream tokenStream, SolrQueryRequest request) {
  QueryScorer scorer = new QueryScorer(query,
          request.getParams().getFieldBool(fieldName, HighlightParams.FIELD_MATCH, false) ? fieldName : null) {
    @Override
    protected WeightedSpanTermExtractor newTermExtractor(String defaultField) {
      return new CustomSpanTermExtractor(defaultField);
    }
  };
  scorer.setExpandMultiTermQuery(request.getParams().getBool(HighlightParams.HIGHLIGHT_MULTI_TERM, true));

  boolean defaultPayloads = true;//overwritten below
  try {
    // It'd be nice to know if payloads are on the tokenStream but the presence of the attribute isn't a good
    // indicator.
    final Terms terms = request.getSearcher().getSlowAtomicReader().terms(fieldName);
    if (terms != null) {
      defaultPayloads = terms.hasPayloads();
    }
  } catch (IOException e) {
    log.error("Couldn't check for existence of payloads", e);
  }
  scorer.setUsePayloads(request.getParams().getFieldBool(fieldName, HighlightParams.PAYLOADS, defaultPayloads));
  return scorer;
}
 
Example 15
Source Project: querqy   Source File: DefaultQuerqyDismaxQParserTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testThatHighlightingIsAppliedToSynonyms() {
    SolrQueryRequest req = req("q", "o",
            DisMaxParams.QF, "f1",
            HighlightParams.HIGHLIGHT, "true",
            HighlightParams.FIELDS, "f1",
            HighlightParams.SIMPLE_PRE, "PRE",
            HighlightParams.SIMPLE_POST, "POST",
            "defType", "querqy",
            "debugQuery", "true");

    assertQ("UP token is highlighted",
            req,
            "//lst[@name='highlighting']//arr[@name='f1']/str[contains(.,'PREsPOST')]"
    );
    req.close();

}
 
Example 16
Source Project: lucene-solr   Source File: DefaultSolrHighlighter.java    License: Apache License 2.0 6 votes vote down vote up
/** Highlights and returns the highlight object for this field -- a String[] by default.  Null if none. */
@SuppressWarnings("unchecked")
protected Object doHighlightingByFastVectorHighlighter(SolrDocument doc, int docId,
                                                       SchemaField schemaField, FvhContainer fvhContainer,
                                                       IndexReader reader, SolrQueryRequest req) throws IOException {
  SolrParams params = req.getParams();
  String fieldName = schemaField.getName();
  SolrFragmentsBuilder solrFb = getSolrFragmentsBuilder(fieldName, params);

  String[] snippets = fvhContainer.fvh.getBestFragments(fvhContainer.fieldQuery, reader, docId, fieldName,
          params.getFieldInt(fieldName, HighlightParams.FRAGSIZE, 100),
          params.getFieldInt(fieldName, HighlightParams.SNIPPETS, 1),
          getFragListBuilder(fieldName, params),
          getFragmentsBuilder(fieldName, params),
          solrFb.getPreTags(params, fieldName),
          solrFb.getPostTags(params, fieldName),
          getEncoder(fieldName, params));
  if (snippets != null && snippets.length > 0)
    return snippets;
  return null;
}
 
Example 17
Source Project: lucene-solr   Source File: PostingsSolrHighlighter.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected UnifiedHighlighter getHighlighter(SolrQueryRequest req) {
  // Adjust the highlight parameters to match what the old PostingsHighlighter had.
  ModifiableSolrParams invariants = new ModifiableSolrParams();
  invariants.set(HighlightParams.OFFSET_SOURCE, "POSTINGS");
  invariants.set(HighlightParams.FIELD_MATCH, true);
  invariants.set(HighlightParams.USE_PHRASE_HIGHLIGHTER, false);
  invariants.set(HighlightParams.FRAGSIZE, -1);

  ModifiableSolrParams defaults = new ModifiableSolrParams();
  defaults.set(HighlightParams.DEFAULT_SUMMARY, true);
  defaults.set(HighlightParams.TAG_ELLIPSIS, "... ");

  SolrParams newParams = SolrParams.wrapDefaults(
      invariants,// this takes precedence
      SolrParams.wrapDefaults(
          req.getParams(), // then this (original)
          defaults // finally our defaults
      )
  );
  try (LocalSolrQueryRequest fakeReq = new LocalSolrQueryRequest(req.getCore(), newParams)) {
    return super.getHighlighter(fakeReq);
  }
}
 
Example 18
Source Project: lucene-solr   Source File: UnifiedSolrHighlighter.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public NamedList<Object> doHighlighting(DocList docs, Query query, SolrQueryRequest req, String[] defaultFields) throws IOException {
  final SolrParams params = req.getParams();

  // if highlighting isn't enabled, then why call doHighlighting?
  if (!isHighlightingEnabled(params))
    return null;

  int[] docIDs = toDocIDs(docs);

  // fetch the unique keys
  String[] keys = getUniqueKeys(req.getSearcher(), docIDs);

  // query-time parameters
  String[] fieldNames = getHighlightFields(query, req, defaultFields);

  int maxPassages[] = new int[fieldNames.length];
  for (int i = 0; i < fieldNames.length; i++) {
    maxPassages[i] = params.getFieldInt(fieldNames[i], HighlightParams.SNIPPETS, 1);
  }

  UnifiedHighlighter highlighter = getHighlighter(req);
  Map<String, String[]> snippets = highlighter.highlightFields(fieldNames, query, docIDs, maxPassages);
  return encodeSnippets(keys, fieldNames, snippets);
}
 
Example 19
Source Project: lucene-solr   Source File: UnifiedSolrHighlighter.java    License: Apache License 2.0 6 votes vote down vote up
public SolrExtendedUnifiedHighlighter(SolrQueryRequest req) {
  super(req.getSearcher(), req.getSchema().getIndexAnalyzer());
  this.params = req.getParams();
  this.schema = req.getSchema();
  this.setMaxLength(
      params.getInt(HighlightParams.MAX_CHARS, DEFAULT_MAX_CHARS));
  this.setCacheFieldValCharsThreshold(
      params.getInt(HighlightParams.CACHE_FIELD_VAL_CHARS_THRESHOLD, DEFAULT_CACHE_CHARS_THRESHOLD));

  final RTimerTree timerTree;
  if (req.getRequestTimer() != null) { //It may be null if not used in a search context.
    timerTree = req.getRequestTimer();
  } else {
    timerTree = new RTimerTree(); // since null checks are annoying
  }
  loadFieldValuesTimer = timerTree.sub("loadFieldValues"); // we assume a new timer, state of STARTED
  loadFieldValuesTimer.pause(); // state of PAUSED now with about zero time. Will fail if state isn't STARTED.
}
 
Example 20
Source Project: lucene-solr   Source File: HighlighterTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void payloadFilteringSpanQuery() throws IOException {
  clearIndex();

  String FIELD_NAME = "payloadDelimited";
  assertU(adoc("id", "0", FIELD_NAME, "word|7 word|2"));
  assertU(commit());

  //We search at a lower level than typical Solr tests because there's no QParser for payloads

  //Create query matching this payload
  Query query = new SpanPayloadCheckQuery(new SpanTermQuery(new Term(FIELD_NAME, "word")),
      Collections.singletonList(new BytesRef(new byte[]{0, 0, 0, 7})));//bytes for integer 7

  //invoke highlight component... the hard way
  final SearchComponent hlComp = h.getCore().getSearchComponent("highlight");
  SolrQueryRequest req = req("hl", "true", "hl.fl", FIELD_NAME, HighlightParams.USE_PHRASE_HIGHLIGHTER, "true");
  try {
    SolrQueryResponse resp = new SolrQueryResponse();
    ResponseBuilder rb = new ResponseBuilder(req, resp, Collections.singletonList(hlComp));
    rb.setHighlightQuery(query);
    rb.setResults(req.getSearcher().getDocListAndSet(query, (DocSet) null, null, 0, 1));
    //highlight:
    hlComp.prepare(rb);
    hlComp.process(rb);
    //inspect response
    final String[] snippets = (String[]) resp.getValues().findRecursive("highlighting", "0", FIELD_NAME);
    assertEquals("<em>word|7</em> word|2", snippets[0]);
  } finally {
    req.close();
  }
}
 
Example 21
private void rewriteLocalFieldParameters(ModifiableSolrParams newParams, SolrParams previousParams, String fieldName, String schemaFieldName)
{
	rewriteHighlightFieldOptions(newParams, previousParams, HighlightParams.SIMPLE_PRE, fieldName, schemaFieldName);
	rewriteHighlightFieldOptions(newParams, previousParams, HighlightParams.SIMPLE_POST, fieldName, schemaFieldName);
	rewriteHighlightFieldOptions(newParams, previousParams, HighlightParams.FRAGSIZE, fieldName, schemaFieldName);
	rewriteHighlightFieldOptions(newParams, previousParams, HighlightParams.MERGE_CONTIGUOUS_FRAGMENTS, fieldName, schemaFieldName);
	rewriteHighlightFieldOptions(newParams, previousParams, HighlightParams.SNIPPETS, fieldName, schemaFieldName);
}
 
Example 22
@Test
public void highlightingPhraseQueriesTest()
{
    //Phrase hightling is on by default
    SolrServletRequest req = areq(params( "q", "name:long", "qt", "/afts", "start", "0", "rows", "5",
            HighlightParams.HIGHLIGHT, "true",
            HighlightParams.Q, "\"some long\"",
            HighlightParams.FIELDS, "name",
            HighlightParams.SIMPLE_PRE, "(",
            HighlightParams.SIMPLE_POST, ")",
            HighlightParams.SNIPPETS, String.valueOf(1),
            HighlightParams.FRAGSIZE, String.valueOf(100)),
            "{\"locales\":[\"en\"], \"tenants\": [ \"\" ]}");

    assertQ(req,
            "//lst[@name='highlighting']/lst/arr/str[.='this is (some) (long) text.  It has the word long in many places.  In fact, it has long on some']");

    req = areq(params( "q", "name:long", "qt", "/afts", "start", "0", "rows", "5",
            HighlightParams.HIGHLIGHT, "true",
            HighlightParams.Q, "\"some long\"",
            HighlightParams.FIELDS, "name",
            HighlightParams.USE_PHRASE_HIGHLIGHTER, "false",
            HighlightParams.SIMPLE_PRE, "(",
            HighlightParams.SIMPLE_POST, ")",
            HighlightParams.SNIPPETS, String.valueOf(1),
            HighlightParams.FRAGSIZE, String.valueOf(100)),
            "{\"locales\":[\"en\"], \"tenants\": [ \"\" ]}");

    assertQ(req,
            "//lst[@name='highlighting']/lst/arr/str[.='(some) very (long) name']",
            "//lst[@name='highlighting']/lst/arr/str[.='this is (some) (long) text.  It has the word (long) in many places.  In fact, it has (long) on (some)']");
}
 
Example 23
Source Project: lucene-solr   Source File: UnifiedSolrHighlighter.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected PassageFormatter getFormatter(String fieldName) {
  String preTag = params.getFieldParam(fieldName, HighlightParams.TAG_PRE,
      params.getFieldParam(fieldName, HighlightParams.SIMPLE_PRE, "<em>")
  );

  String postTag = params.getFieldParam(fieldName, HighlightParams.TAG_POST,
      params.getFieldParam(fieldName, HighlightParams.SIMPLE_POST, "</em>")
  );
  String ellipsis = params.getFieldParam(fieldName, HighlightParams.TAG_ELLIPSIS, SNIPPET_SEPARATOR);
  String encoder = params.getFieldParam(fieldName, HighlightParams.ENCODER, "simple");
  return new DefaultPassageFormatter(preTag, postTag, ellipsis, "html".equals(encoder));
}
 
Example 24
@Test
public void highlightingRequiredFieldsTest()
{
   SolrServletRequest req = areq(params( "q", "name:long", "qt", "/afts", "start", "0", "rows", "5",
            HighlightParams.HIGHLIGHT, "true",
            HighlightParams.Q, "long",
            HighlightParams.FIELDS, "name,title",
            HighlightParams.SIMPLE_PRE, "{",
            HighlightParams.SIMPLE_POST, "}"),
            "{\"locales\":[\"en\"], \"tenants\": [ \"\" ]}");

    assertQ(req,
            "*[count(//lst[@name='highlighting']/lst)=2]",
            "*[count(//lst[@name='highlighting']/lst/arr[@name='title'])=1]",
            "//lst[@name='highlighting']/lst[1]/arr[@name='title']/str[.='title1 is very {long}']");

    req = areq(params( "q", "name:long OR title:long", "qt", "/afts", "start", "0", "rows", "5",
            HighlightParams.HIGHLIGHT, "true",
            HighlightParams.Q, "title:long",
            HighlightParams.FIELDS, "name,title",
            HighlightParams.FIELD_MATCH, "true",
            HighlightParams.SIMPLE_PRE, "{",
            HighlightParams.SIMPLE_POST, "}"),
            "{\"locales\":[\"en\"], \"tenants\": [ \"\" ]}");

    assertQ(req,
            "*[count(//lst[@name='highlighting']/lst)=2]",
            "*[count(//lst[@name='highlighting']/lst/arr[@name='title'])=1]",
            "*[count(//lst[@name='highlighting']/lst/arr[@name='name'])=0]",
            "//lst[@name='highlighting']/lst[1]/arr[@name='title']/str[.='title1 is very {long}']");
}
 
Example 25
Source Project: lucene-solr   Source File: UnifiedSolrHighlighter.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * parse custom separator char for {@link CustomSeparatorBreakIterator}
 */
protected char parseBiSepChar(String sepChar) {
  if (sepChar == null) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, HighlightParams.BS_SEP + " not passed");
  }
  if (sepChar.length() != 1) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, HighlightParams.BS_SEP +
        " must be a single char but got: '" + sepChar + "'");
  }
  return sepChar.charAt(0);
}
 
Example 26
Source Project: lucene-solr   Source File: SolrQuery.java    License: Apache License 2.0 5 votes vote down vote up
/** remove a field for highlighting
 * 
 * @param f field name to not highlight
 * @return <i>true</i>, if removed, <br> <i>false</i>, otherwise
 */
public boolean removeHighlightField(String f) {
  boolean b = this.remove(HighlightParams.FIELDS, f);
  if (this.get(HighlightParams.FIELDS) == null) {
    this.setHighlight(false);
  }
  return b;
}
 
Example 27
Source Project: lucene-solr   Source File: UnifiedSolrHighlighter.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected OffsetSource getOffsetSource(String field) {
  String sourceStr = params.getFieldParam(field, HighlightParams.OFFSET_SOURCE);
  if (sourceStr != null) {
    return OffsetSource.valueOf(sourceStr.toUpperCase(Locale.ROOT));
  } else {
    return super.getOffsetSource(field);
  }
}
 
Example 28
Source Project: lucene-solr   Source File: SolrQuery.java    License: Apache License 2.0 5 votes vote down vote up
public SolrQuery setHighlight(boolean b) {
  if (b) {
    this.set(HighlightParams.HIGHLIGHT, true);
  } else {
    this.remove(HighlightParams.HIGHLIGHT);
    this.remove(HighlightParams.FIELD_MATCH);
    this.remove(HighlightParams.FIELDS);
    this.remove(HighlightParams.FORMATTER);
    this.remove(HighlightParams.FRAGSIZE);
    this.remove(HighlightParams.SIMPLE_POST);
    this.remove(HighlightParams.SIMPLE_PRE);
    this.remove(HighlightParams.SNIPPETS);
  }
  return this;
}
 
Example 29
Source Project: lucene-solr   Source File: HighlightComponent.java    License: Apache License 2.0 5 votes vote down vote up
protected SolrHighlighter getHighlighter(SolrParams params) {
  HighlightMethod method = HighlightMethod.parse(params.get(HighlightParams.METHOD));
  if (method == null) {
    return solrConfigHighlighter;
  }

  switch (method) {
    case UNIFIED:
      if (solrConfigHighlighter instanceof UnifiedSolrHighlighter) {
        return solrConfigHighlighter;
      }
      return new UnifiedSolrHighlighter(); // TODO cache one?
    case POSTINGS:
      if (solrConfigHighlighter instanceof PostingsSolrHighlighter) {
        return solrConfigHighlighter;
      }
      return new PostingsSolrHighlighter(); // TODO cache one?
    case FAST_VECTOR: // fall-through
    case ORIGINAL:
      if (solrConfigHighlighter instanceof DefaultSolrHighlighter) {
        return solrConfigHighlighter;
      } else {
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,
            "In order to use " + HighlightParams.METHOD + "=" + method.getMethodName() + " the configured" +
                " highlighter in solrconfig must be " + DefaultSolrHighlighter.class);
      }
    default: throw new AssertionError();
  }
}
 
Example 30
Source Project: lucene-solr   Source File: HighlightComponent.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void modifyRequest(ResponseBuilder rb, SearchComponent who, ShardRequest sreq) {
  if (!rb.doHighlights) return;

  // Turn on highlighting only only when retrieving fields
  if ((sreq.purpose & ShardRequest.PURPOSE_GET_FIELDS) != 0) {
      sreq.purpose |= ShardRequest.PURPOSE_GET_HIGHLIGHTS;
      // should already be true...
      sreq.params.set(HighlightParams.HIGHLIGHT, "true");      
  } else {
    sreq.params.set(HighlightParams.HIGHLIGHT, "false");      
  }
}