Java源码示例:org.apache.solr.highlight.SolrHighlighter

示例1
@Override
public void inform(SolrCore core) {
  List<PluginInfo> children = info.getChildren("highlighting");
  if(children.isEmpty()) {
    DefaultSolrHighlighter defHighlighter = new DefaultSolrHighlighter(core);
    defHighlighter.init(PluginInfo.EMPTY_INFO);
    solrConfigHighlighter = defHighlighter;
  } else {
    solrConfigHighlighter = core.createInitInstance(children.get(0),SolrHighlighter.class,null, DefaultSolrHighlighter.class.getName());
  }

}
 
示例2
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();
  }
}
 
示例3
/**
 * 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);
    }

  }

}
 
示例4
/**
 * @deprecated instead depend on {@link #process(ResponseBuilder)} to choose the highlighter based on
 * {@link HighlightParams#METHOD}
 */
@Deprecated
public static SolrHighlighter getHighlighter(SolrCore core) {
  HighlightComponent hl = (HighlightComponent) core.getSearchComponents().get(HighlightComponent.COMPONENT_NAME);
  return hl==null ? null: hl.getHighlighter();
}
 
示例5
@Deprecated
public SolrHighlighter getHighlighter() {
  return solrConfigHighlighter;
}
 
示例6
@Override
public void process(ResponseBuilder rb) throws IOException {

  if (rb.doHighlights) {
    SolrQueryRequest req = rb.req;
    SolrParams params = req.getParams();

    SolrHighlighter highlighter = getHighlighter(params);

    //TODO: get from builder by default?
    String[] defaultHighlightFields = rb.getQparser() != null ? rb.getQparser().getDefaultHighlightFields() : null;
    
    Query highlightQuery = rb.getHighlightQuery();
    if(highlightQuery==null) {
      if (rb.getQparser() != null) {
        try {
          highlightQuery = rb.getQparser().getHighlightQuery();
          rb.setHighlightQuery( highlightQuery );
        } catch (Exception e) {
          throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);
        }
      } else {
        highlightQuery = rb.getQuery();
        rb.setHighlightQuery( highlightQuery );
      }
    }

    // No highlighting if there is no query -- consider q.alt=*:*
    if( highlightQuery != null ) {
      @SuppressWarnings({"rawtypes"})
      NamedList sumData = highlighter.doHighlighting(
              rb.getResults().docList,
              highlightQuery,
              req, defaultHighlightFields );
      
      if(sumData != null) {
        // TODO ???? add this directly to the response?
        rb.rsp.add(highlightingResponseField(), convertHighlights(sumData));
      }
    }
  }
}
 
示例7
@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);
}