Java Code Examples for org.apache.solr.handler.component.HighlightComponent#getHighlighter()

The following examples show how to use org.apache.solr.handler.component.HighlightComponent#getHighlighter() . 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 File: HighlighterTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testConfig()
{
  DefaultSolrHighlighter highlighter = (DefaultSolrHighlighter) HighlightComponent.getHighlighter(h.getCore());

  // Make sure we loaded the one formatter
  SolrFormatter fmt1 = highlighter.formatters.get( null );
  SolrFormatter fmt2 = highlighter.formatters.get( "" );
  assertSame( fmt1, fmt2 );
  assertTrue( fmt1 instanceof HtmlFormatter );
  
  
  // Make sure we loaded the one formatter
  SolrFragmenter gap = highlighter.fragmenters.get( "gap" );
  SolrFragmenter regex = highlighter.fragmenters.get( "regex" );
  SolrFragmenter frag = highlighter.fragmenters.get( null );
  assertSame( gap, frag );
  assertTrue(gap instanceof GapFragmenter);
  assertTrue(regex instanceof RegexFragmenter);
}
 
Example 2
Source File: HighlighterConfigTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testConfig()
{
        SolrHighlighter highlighter = HighlightComponent.getHighlighter(h.getCore());
  log.info( "highlighter" );

  assertTrue( highlighter instanceof DummyHighlighter );

  // check to see that doHighlight is called from the DummyHighlighter
  HashMap<String,String> args = new HashMap<>();
  args.put("hl", "true");
  args.put("df", "t_text");
  args.put("hl.fl", "");
  TestHarness.LocalRequestFactory sumLRF = h.getRequestFactory(
    "", 0, 200, args);

  assertU(adoc("t_text", "a long day's night", "id", "1"));
  assertU(commit());
  assertU(optimize());
  assertQ("Basic summarization",
          sumLRF.makeRequest("long"),
          "//lst[@name='highlighting']/str[@name='dummy']"
          );
  }
 
Example 3
Source File: TestPostingsSolrHighlighter.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void beforeClass() throws Exception {
  initCore("solrconfig-postingshighlight.xml", "schema-postingshighlight.xml");
  
  // test our config is sane, just to be sure:
  
  // postingshighlighter should be used
  SolrHighlighter highlighter = HighlightComponent.getHighlighter(h.getCore());
  assertTrue("wrong highlighter: " + highlighter.getClass(), highlighter instanceof PostingsSolrHighlighter);
  
  // 'text' and 'text3' should have offsets, 'text2' should not
  IndexSchema schema = h.getCore().getLatestSchema();
  assertTrue(schema.getField("text").storeOffsetsWithPositions());
  assertTrue(schema.getField("text3").storeOffsetsWithPositions());
  assertFalse(schema.getField("text2").storeOffsetsWithPositions());
}
 
Example 4
Source File: SolrPluginUtils.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Pre-fetch documents into the index searcher's document cache.
 *
 * This is an entirely optional step which you might want to perform for
 * the following reasons:
 *
 * <ul>
 *     <li>Locates the document-retrieval costs in one spot, which helps
 *     detailed performance measurement</li>
 *
 *     <li>Determines a priori what fields will be needed to be fetched by
 *     various subtasks, like response writing and highlighting.  This
 *     minimizes the chance that many needed fields will be loaded lazily.
 *     (it is more efficient to load all the field we require normally).</li>
 * </ul>
 *
 * If lazy field loading is disabled, this method does nothing.
 */
public static void optimizePreFetchDocs(ResponseBuilder rb,
                                        DocList docs,
                                        Query query,
                                        SolrQueryRequest req,
                                        SolrQueryResponse res) throws IOException {
  SolrIndexSearcher searcher = req.getSearcher();
  if(!searcher.getDocFetcher().isLazyFieldLoadingEnabled()) {
    // nothing to do
    return;
  }

  ReturnFields returnFields = res.getReturnFields();
  if(returnFields.getLuceneFieldNames() != null) {
    Set<String> fieldFilter = returnFields.getLuceneFieldNames();

    if (rb.doHighlights) {
      // copy return fields list
      fieldFilter = new HashSet<>(fieldFilter);
      // add highlight fields

      SolrHighlighter highlighter = HighlightComponent.getHighlighter(req.getCore());
      for (String field: highlighter.getHighlightFields(query, req, null))
        fieldFilter.add(field);

      // fetch unique key if one exists.
      SchemaField keyField = searcher.getSchema().getUniqueKeyField();
      if(null != keyField)
        fieldFilter.add(keyField.getName());
    }

    // get documents
    DocIterator iter = docs.iterator();
    for (int i=0; i<docs.size(); i++) {
      searcher.doc(iter.nextDoc(), fieldFilter);
    }

  }

}
 
Example 5
Source File: FastVectorHighlighterTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testConfig(){
  DefaultSolrHighlighter highlighter = (DefaultSolrHighlighter) HighlightComponent.getHighlighter(h.getCore());

  // Make sure we loaded one fragListBuilder
  SolrFragListBuilder solrFlbNull = highlighter.fragListBuilders.get( null );
  SolrFragListBuilder solrFlbEmpty = highlighter.fragListBuilders.get( "" );
  SolrFragListBuilder solrFlbSimple = highlighter.fragListBuilders.get( "simple" );
  assertSame( solrFlbNull, solrFlbEmpty );
  assertTrue( solrFlbNull instanceof SimpleFragListBuilder );
  assertTrue( solrFlbSimple instanceof SimpleFragListBuilder );
      
  // Make sure we loaded two fragmentsBuilders
  SolrFragmentsBuilder solrFbNull = highlighter.fragmentsBuilders.get( null );
  SolrFragmentsBuilder solrFbEmpty = highlighter.fragmentsBuilders.get( "" );
  SolrFragmentsBuilder solrFbSimple = highlighter.fragmentsBuilders.get( "simple" );
  SolrFragmentsBuilder solrFbSO = highlighter.fragmentsBuilders.get( "scoreOrder" );
  assertSame( solrFbNull, solrFbEmpty );
  assertTrue( solrFbNull instanceof SimpleFragmentsBuilder );
  assertTrue( solrFbSimple instanceof SimpleFragmentsBuilder );
  assertTrue( solrFbSO instanceof ScoreOrderFragmentsBuilder );
  
  // Make sure we loaded two boundaryScanners
  SolrBoundaryScanner solrBsNull = highlighter.boundaryScanners.get(null);
  SolrBoundaryScanner solrBsEmpty = highlighter.boundaryScanners.get("");
  SolrBoundaryScanner solrBsSimple = highlighter.boundaryScanners.get("simple");
  SolrBoundaryScanner solrBsBI = highlighter.boundaryScanners.get("breakIterator");
  assertSame(solrBsNull, solrBsEmpty);
  assertTrue(solrBsNull instanceof SimpleBoundaryScanner);
  assertTrue(solrBsSimple instanceof SimpleBoundaryScanner);
  assertTrue(solrBsBI instanceof BreakIteratorBoundaryScanner);
}
 
Example 6
Source File: HighlighterTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testGetHighlightFields() {
  HashMap<String, String> args = new HashMap<>();
  args.put("fl", "id score");
  args.put("hl", "true");
  args.put("hl.fl", "t*");

  assertU(adoc("id", "0", "title", "test", // static stored
      "text", "test", // static not stored
      "foo_s", "test", // dynamic stored
      "foo_sI", "test", // dynamic not stored
      "bar_s", "test", // dynamic stored
      "bar_sI", "test", // dynamic not stored
      "weight", "1.0")); // stored but not text
  assertU(commit());
  assertU(optimize());

  TestHarness.LocalRequestFactory lrf = h.getRequestFactory("", 0,
      10, args);

  SolrQueryRequest request = lrf.makeRequest("test");
  SolrHighlighter highlighter = HighlightComponent.getHighlighter(h.getCore());
  List<String> highlightFieldNames = Arrays.asList(highlighter
      .getHighlightFields(null, request, new String[] {}));
  assertTrue("Expected to highlight on field \"title\"", highlightFieldNames
      .contains("title"));
  assertFalse("Expected to not highlight on field \"text\"",
      highlightFieldNames.contains("text"));
  assertFalse("Expected to not highlight on field \"weight\"",
      highlightFieldNames.contains("weight"));
  request.close();

  args.put("hl.fl", "foo_*");
  lrf = h.getRequestFactory("", 0, 10, args);
  request = lrf.makeRequest("test");
  highlighter = HighlightComponent.getHighlighter(h.getCore());
  highlightFieldNames = Arrays.asList(highlighter.getHighlightFields(null,
      request, new String[] {}));
  assertEquals("Expected one field to highlight on", 1, highlightFieldNames
      .size());
  assertEquals("Expected to highlight on field \"foo_s\"", "foo_s",
      highlightFieldNames.get(0));
  request.close();

  // SOLR-5127
  args.put("hl.fl", (random().nextBoolean() ? "foo_*,bar_*" : "bar_*,foo_*"));
  lrf = h.getRequestFactory("", 0, 10, args);
  // hl.fl ordering need not be preserved in output
  final Set<String> highlightedSetExpected = new HashSet<String>();
  highlightedSetExpected.add("foo_s");
  highlightedSetExpected.add("bar_s");
  try (LocalSolrQueryRequest localRequest = lrf.makeRequest("test")) {
    highlighter = HighlightComponent.getHighlighter(h.getCore());
    final Set<String> highlightedSetActual = new HashSet<String>(
        Arrays.asList(highlighter.getHighlightFields(null,
            localRequest, new String[] {})));
    assertEquals(highlightedSetExpected, highlightedSetActual);
  }

  // SOLR-11334
  args.put("hl.fl", "title, text"); // comma then space
  lrf = h.getRequestFactory("", 0, 10, args);
  request = lrf.makeRequest("test");
  highlighter = HighlightComponent.getHighlighter(h.getCore());
  highlightFieldNames = Arrays.asList(highlighter.getHighlightFields(null,
      request, new String[] {}));
  assertEquals("Expected one field to highlight on", 2, highlightFieldNames
      .size());
  assertTrue("Expected to highlight on field \"title\"",
      highlightFieldNames.contains("title"));
  assertTrue("Expected to highlight on field \"text\"",
      highlightFieldNames.contains("text"));
  assertFalse("Expected to not highlight on field \"\"",
      highlightFieldNames.contains(""));

  request.close();
}
 
Example 7
Source File: TestTaggedQueryHighlighterIT.java    From solr-redis with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void beforeClass() throws Exception {
  initCore("tagged-highlighting-solrconfig.xml", "schema.xml");
  SolrHighlighter highlighter = HighlightComponent.getHighlighter(h.getCore());
  assertTrue("wrong highlighter: " + highlighter.getClass(), highlighter instanceof TaggedQueryHighlighter);
}