Java源码示例:org.apache.solr.handler.component.HighlightComponent

示例1
@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);
}
 
示例2
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']"
          );
  }
 
示例3
@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());
}
 
示例4
@Override
@SuppressWarnings("rawtypes")
public void init(NamedList args) {
  maxOriginalResults = getInt(args, "maxOriginalResults");
  String tmp = (String) args.get("allComponentNames");
  if (tmp != null) {
    componentNames.addAll(Arrays.asList(tmp.split("\\s*,\\s*")));
  }
  componentNames.add(getComponentName());
  componentNames.add(FacetComponent.COMPONENT_NAME);
  componentNames.add(HighlightComponent.COMPONENT_NAME);
  componentNames.add(StatsComponent.COMPONENT_NAME);
  componentNames.add(TermsComponent.COMPONENT_NAME);
  componentNames.add(TermVectorComponent.COMPONENT_NAME);
  componentNames.add(SpellCheckComponent.COMPONENT_NAME);
  componentNames.add(MoreLikeThisComponent.COMPONENT_NAME);
  componentNames.add(GroupParams.GROUP);
  componentNames.add("queryRelaxer");
  componentNames.add("DymReSearcher");
  componentNames.add("autoComplete");
  commonMisspellingsFileLocation = (String) args.get("commonMisspellingsFile");
  commonMisspellingsMap = CommonMisspellings.loadCommonMisspellingsFile(commonMisspellingsFileLocation);
}
 
示例5
/**
 * 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);
    }

  }

}
 
示例6
/**
 * Determines if we should use the FastVectorHighlighter for this field.
 */
protected boolean useFastVectorHighlighter(SolrParams params, SchemaField schemaField) {
  boolean methodFvh =
          HighlightComponent.HighlightMethod.FAST_VECTOR.getMethodName().equals(
                  params.getFieldParam(schemaField.getName(), HighlightParams.METHOD))
                  || params.getFieldBool(schemaField.getName(), USE_FVH, false);
  if (!methodFvh) return false;
  boolean termPosOff = schemaField.storeTermPositions() && schemaField.storeTermOffsets();
  if (!termPosOff) {
    log.warn("Solr will use the standard Highlighter instead of FastVectorHighlighter because the {} field {}"
            , "does not store TermVectors with TermPositions and TermOffsets.", schemaField.getName());
  }
  return termPosOff;
}
 
示例7
/**
 * Register the default search components
 */
private void loadSearchComponents() {
  Map<String, SearchComponent> instances = createInstances(SearchComponent.standard_components);
  for (Map.Entry<String, SearchComponent> e : instances.entrySet()) e.getValue().setName(e.getKey());
  searchComponents.init(instances, this);

  for (String name : searchComponents.keySet()) {
    if (searchComponents.isLoaded(name) && searchComponents.get(name) instanceof HighlightComponent) {
      if (!HighlightComponent.COMPONENT_NAME.equals(name)) {
        searchComponents.put(HighlightComponent.COMPONENT_NAME, searchComponents.getRegistry().get(name));
      }
      break;
    }
  }
}
 
示例8
@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);
}
 
示例9
@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();
}
 
示例10
@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);
}