org.apache.solr.highlight.SolrHighlighter Java Examples

The following examples show how to use org.apache.solr.highlight.SolrHighlighter. 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: HighlightComponent.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@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());
  }

}
 
Example #2
Source File: HighlightComponent.java    From lucene-solr with 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 #3
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 #4
Source File: HighlightComponent.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 * @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();
}
 
Example #5
Source File: HighlightComponent.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Deprecated
public SolrHighlighter getHighlighter() {
  return solrConfigHighlighter;
}
 
Example #6
Source File: HighlightComponent.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@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));
      }
    }
  }
}
 
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);
}