org.apache.solr.handler.component.ResponseBuilder Java Examples

The following examples show how to use org.apache.solr.handler.component.ResponseBuilder. 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: AutoCompleteSearchComponent.java    From solr-autocomplete with Apache License 2.0 6 votes vote down vote up
public void doProcess(ResponseBuilder rb) {
  ReSearcherRequestContext ctx = new ReSearcherRequestContext(rb);
  ctx.setCore(getCore());
  ctx.setQueryOnlyComponents(getQueryOnlyComponents());
  ctx.setShardHandlerFactory(getShardHandlerFactory());
  
  try {
    doProcess(ctx, rb);
  } catch (Exception e) {
    String msg = "ReSearcher error";
    LOG.error(msg, e);
    throw new RuntimeException(msg, e);
  }

  ctx.restoreContext(rb);
}
 
Example #2
Source File: RangerSolrAuthorizer.java    From ranger with Apache License 2.0 6 votes vote down vote up
@Override
public void prepare(ResponseBuilder responseBuilder) throws IOException {

	if(LOG.isDebugEnabled()) {
		LOG.debug("==> RangerSolrAuthorizer.prepare()");
	}

	try {
		activatePluginClassLoader();

		rangerSearchComponentImpl.prepare(responseBuilder);
	} finally {
		deactivatePluginClassLoader();
	}

	if(LOG.isDebugEnabled()) {
		LOG.debug("<== RangerSolrAuthorizer.prepare()");
	}
}
 
Example #3
Source File: RewriteFacetParametersComponent.java    From SearchServices with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void prepare(ResponseBuilder rb) throws IOException
{
    SolrQueryRequest req = rb.req;
    SolrParams params = req.getParams();

    ModifiableSolrParams fixed = new ModifiableSolrParams();
    ModifiableSolrParams allParamsWithFix = new ModifiableSolrParams(params);
    fixFilterQueries(fixed, params, rb);
    fixFacetParams(fixed, params, rb);
    fixRows(fixed, params, rb);
    
    Set<String> fixedParameterNames = fixed.getParameterNames();
    for (String fixedParam : fixedParameterNames)
    {
        allParamsWithFix.set(fixedParam, fixed.getParams(fixedParam));
    }

    if (allParamsWithFix.get(CommonParams.SORT) != null)
    {
        allParamsWithFix.remove(CommonParams.RQ);
    }
    
    req.setParams(allParamsWithFix);
}
 
Example #4
Source File: DelayingSearchComponent.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public void process(ResponseBuilder rb) throws IOException {
  final long totalSleepMillis = rb.req.getParams().getLong("sleep",0);
  if (totalSleepMillis > 0) {
    final long totalSleepNanos = TimeUnit.NANOSECONDS.convert(totalSleepMillis, TimeUnit.MILLISECONDS);
    final long startNanos = System.nanoTime();
    try {
      // Thread.sleep() (and derivatives) are not garunteed to sleep the full amount:
      //   "subject to the precision and accuracy of system timers and schedulers."
      // This is particularly problematic on Windows VMs, so we do a retry loop
      // to ensure we sleep a total of at least as long as requested
      //
      // (Tests using this component do so explicitly to ensure 'timeAllowed'
      // has exceeded in order to get their expected results, we would rather over-sleep
      // then under sleep)
      for (long sleepNanos = totalSleepNanos;
           0 < sleepNanos;
           sleepNanos = totalSleepNanos - (System.nanoTime() - startNanos)) {
        TimeUnit.NANOSECONDS.sleep(sleepNanos);
      }
    } catch (InterruptedException e) {
      // Do nothing?
    }
  }
}
 
Example #5
Source File: RewriteFacetCountsComponent.java    From SearchServices with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Update the returned facet sections to reference ACS properties rather than Solr fields.
 *
 * @param rb the response builder.
 * @param mappingName The name of the mapping definition created in {@link RewriteFacetParametersComponent}.
 * @param sections The names of the sections to be updated.
 */
private void rewrite(ResponseBuilder rb, String mappingName, String ... sections)
{
    BiMap<String, String> mappings = (BiMap<String, String>) rb.rsp.getValues().get(mappingName);
    if(mappings != null)
    {
        NamedList<Object>  found = (NamedList<Object>) rb.rsp.getValues();
        for (String section : sections)
        {
            found = (NamedList<Object>) found.get(section);
            if (found == null)
            {
                return;
            }
        }
        // This found already contains the private buckets
        updateToACSNaming(mappings, found);
    }
}
 
Example #6
Source File: FacetTreeGenerator.java    From BioSolr with Apache License 2.0 6 votes vote down vote up
public List<SimpleOrderedMap<Object>> generateTree(ResponseBuilder rb, NamedList<Integer> facetValues) throws IOException {
	List<SimpleOrderedMap<Object>> retVal = null;
	
	// First get the searcher for the required collection
	RefCounted<SolrIndexSearcher> searcherRef = getSearcherReference(rb);
	
	try {
		// Build the facet tree(s)
		Collection<TreeFacetField> fTrees = treeBuilder.processFacetTree(searcherRef.get(), extractFacetValues(facetValues));
		LOGGER.debug("Extracted {} facet trees", fTrees.size());
		
		if (pruner != null) {
			// Prune the trees
			fTrees = pruner.prune(fTrees);
		}

		// Convert the trees into a SimpleOrderedMap
		retVal = convertTreeFacetFields(fTrees);
	} finally {
		// Make sure the search ref count is decreased
		searcherRef.decref();
	}
	
	return retVal;
}
 
Example #7
Source File: MtasSolrComponentPrefix.java    From mtas with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public void finishStage(ResponseBuilder rb) {
  if (rb.req.getParams().getBool(MtasSolrSearchComponent.PARAM_MTAS, false)
      && rb.stage >= ResponseBuilder.STAGE_EXECUTE_QUERY
      && rb.stage < ResponseBuilder.STAGE_GET_FIELDS) {
    for (ShardRequest sreq : rb.finished) {
      if (sreq.params.getBool(MtasSolrSearchComponent.PARAM_MTAS, false)
          && sreq.params.getBool(PARAM_MTAS_PREFIX, false)) {
        for (ShardResponse shardResponse : sreq.responses) {
          NamedList<Object> response = shardResponse.getSolrResponse()
              .getResponse();
          try {
            ArrayList<NamedList<Object>> data = (ArrayList<NamedList<Object>>) response
                .findRecursive("mtas", NAME);
            if (data != null) {
              MtasSolrResultUtil.decode(data);
            }
          } catch (ClassCastException e) {
            log.debug(e);
            // shouldnt happen
          }
        }
      }
    }
  }
}
 
Example #8
Source File: QuerqyQueryComponent.java    From querqy with Apache License 2.0 6 votes vote down vote up
@Override
public void process(final ResponseBuilder rb) throws IOException {
    
    super.process(rb);
    
    QParser parser = rb.getQparser();
    
    if (parser instanceof QuerqyDismaxQParser) {

        final SearchEngineRequestAdapter searchEngineRequestAdapter =
                ((QuerqyDismaxQParser) parser).getSearchEngineRequestAdapter();

        final Map<String, Object> context = searchEngineRequestAdapter.getContext();
        if (context != null) {

            @SuppressWarnings("unchecked")
            Set<Object> decorations = (Set<Object>) context.get(DecorateInstruction.CONTEXT_KEY);
            if (decorations != null) {
                rb.rsp.add("querqy_decorations", decorations);
            }
        }

        searchEngineRequestAdapter.getInfoLoggingContext().ifPresent(InfoLoggingContext::endOfRequest);
    }

}
 
Example #9
Source File: FacetTreeGenerator.java    From BioSolr with Apache License 2.0 6 votes vote down vote up
/**
 * Get a reference to the searcher for the required collection. If the collection is
 * not the same as the search collection, we assume it is under the same Solr instance.
 * @param rb the response builder holding the facets.
 * @return a counted reference to the searcher.
 * @throws SolrException if the collection cannot be found.
 */
private RefCounted<SolrIndexSearcher> getSearcherReference(ResponseBuilder rb) throws SolrException {
	RefCounted<SolrIndexSearcher> searcherRef;
	
	SolrCore currentCore = rb.req.getCore();
	if (StringUtils.isBlank(collection)) {
		searcherRef = currentCore.getSearcher();
	} else {
		// Using an alternative core - find it
		SolrCore reqCore = currentCore.getCoreDescriptor().getCoreContainer().getCore(collection);
		if (reqCore == null) {
			throw new SolrException(ErrorCode.BAD_REQUEST, "Collection \"" + collection
					+ "\" cannot be found");
		}
		searcherRef = reqCore.getSearcher();
	}
	
	return searcherRef;
}
 
Example #10
Source File: AutoCompleteSearchComponent.java    From solr-autocomplete with Apache License 2.0 5 votes vote down vote up
private void mergeDistributedResultsIntSingleResponse(ResponseBuilder rb, List<AcGroupResult> resultsToMerge, String responseTagName) {
  int docs = 0;
  float maxScore = 0.0f;
  
  // if groups have to be displayed in some custom order, other than the order specified in 
  // ac_grouping_field_def
  String groupingSort = rb.req.getParams().get(AC_GROUPING_SORT_PARAM_NAME);
  if (groupSorts.containsKey(groupingSort)) {
    groupSorts.get(groupingSort).sort(rb, resultsToMerge);
  }
  
  SolrDocumentList docList = new SolrDocumentList();
  // first find count of documents
  for (AcGroupResult acGroupResult : resultsToMerge) {
    // if slice contains more results than requested, take requested count; if it contains less results than
    // requested, we have to take what we got, not more
    docs += ((acGroupResult.getDistributedResultingDocs().size() > acGroupResult.getAcGroupingFieldValue().getRequestedCountOfSuggestions() == true) ? 
        acGroupResult.getAcGroupingFieldValue().getRequestedCountOfSuggestions() : acGroupResult.getDistributedResultingDocs().size());
    
    if (acGroupResult.getDistributedResultingDocs().getMaxScore() > maxScore) {
      maxScore = acGroupResult.getDistributedResultingDocs().getMaxScore();
    }
    
    docList.addAll(acGroupResult.getDistributedResultingDocs());
  }
  
  docList.setStart(0);
  docList.setNumFound(docs);

  rb.rsp.add(responseTagName, docList);
}
 
Example #11
Source File: MockShardHandlerFactory.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public ShardHandler getShardHandler() {
  return new ShardHandler() {
    @Override
    public void prepDistributed(ResponseBuilder rb) {}

    @Override
    public void submit(ShardRequest sreq, String shard,
        ModifiableSolrParams params) {}

    @Override
    public ShardResponse takeCompletedIncludingErrors() {
      return null;
    }

    @Override
    public ShardResponse takeCompletedOrError() {
      return null;
    }

    @Override
    public void cancelAll() {}

    @Override
    public ShardHandlerFactory getShardHandlerFactory() {
      return MockShardHandlerFactory.this;
    }
  };
}
 
Example #12
Source File: FacetTreeProcessorTest.java    From BioSolr with Apache License 2.0 5 votes vote down vote up
@Test
public void process_noFacetTreeParams() throws Exception {
	// Cannot mock local variable in ResponseBuilder...
	ResponseBuilder rb = new ResponseBuilder(null, null, null);
	rb.doFacets = true;
	
	SolrQueryRequest req = mock(SolrQueryRequest.class);
	FacetTreeParameters ftParams = mock(FacetTreeParameters.class);
	
	HierarchicalFacets ftp = new HierarchicalFacets(req, null, null, rb, ftParams);
	assertNull(ftp.process(null));
}
 
Example #13
Source File: MimetypeGroupingCollector.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * @param rb
 * @param mappings 
 * @param group 
 */
public MimetypeGroupingCollector(ResponseBuilder rb, HashMap<String, String> mappings, boolean doGroup)
{
    this.rb = rb;
    this.mappings = mappings;
    this.doGroup = doGroup;
    schemaFieldName = AlfrescoSolrDataModel.getInstance().mapProperty("content.mimetype", FieldUse.FACET, rb.req);
    schemaField = rb.req.getSchema().getFieldOrNull(schemaFieldName);
}
 
Example #14
Source File: PassageRankingComponent.java    From wiseowl with MIT License 5 votes vote down vote up
@Override
public void prepare(ResponseBuilder rb) throws IOException {
  SolrParams params = rb.req.getParams();
  if (!params.getBool(COMPONENT_NAME, false)) {
    return;
  }


}
 
Example #15
Source File: QueryRelaxerComponent.java    From solr-researcher with Apache License 2.0 5 votes vote down vote up
@Override
public boolean checkComponentShouldProcess(ResponseBuilder rb) {
  SolrParams params = rb.req.getParams();
  if (!params.getBool(COMPONENT_NAME, false)) {
    return false;
  }

  return true;
}
 
Example #16
Source File: FacetModule.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void handleResponses(ResponseBuilder rb, ShardRequest sreq) {
  FacetComponentState facetState = getFacetComponentState(rb);
  if (facetState == null) return;

  for (ShardResponse shardRsp : sreq.responses) {
    SolrResponse rsp = shardRsp.getSolrResponse();
    NamedList<Object> top = rsp.getResponse();
    if (top == null) continue; // shards.tolerant=true will cause this to happen on exceptions/errors
    Object facet = top.get("facets");
    if (facet == null) {
      @SuppressWarnings("rawtypes") SimpleOrderedMap shardResponseHeader = (SimpleOrderedMap) rsp.getResponse().get("responseHeader");
      if (Boolean.TRUE.equals(shardResponseHeader.getBooleanArg(SolrQueryResponse.RESPONSE_HEADER_PARTIAL_RESULTS_KEY))) {
        rb.rsp.getResponseHeader().asShallowMap().put(SolrQueryResponse.RESPONSE_HEADER_PARTIAL_RESULTS_KEY, Boolean.TRUE);
      }
      continue;
    }
    if (facetState.merger == null) {
      facetState.merger = facetState.facetRequest.createFacetMerger(facet);
      facetState.mcontext = new FacetMerger.Context(sreq.responses.size());
    }

    if ((sreq.purpose & PURPOSE_REFINE_JSON_FACETS) != 0) {
      // System.err.println("REFINE FACET RESULT FROM SHARD = " + facet);
      // call merge again with a diff flag set on the context???
      facetState.mcontext.root = facet;
      facetState.mcontext.setShard(shardRsp.getShard());  // TODO: roll newShard into setShard?
      facetState.merger.merge(facet, facetState.mcontext);
      return;
    }

    // System.err.println("MERGING FACET RESULT FROM SHARD = " + facet);
    facetState.mcontext.root = facet;
    facetState.mcontext.newShard(shardRsp.getShard());
    facetState.merger.merge(facet, facetState.mcontext);
  }
}
 
Example #17
Source File: FacetTreeProcessorTest.java    From BioSolr with Apache License 2.0 5 votes vote down vote up
@Test(expected=org.apache.solr.common.SolrException.class)
public void process_noChildParamGiven() throws Exception {
	// Cannot mock local variable in ResponseBuilder...
	ResponseBuilder rb = new ResponseBuilder(null, null, null);
	rb.doFacets = true;
	
	SolrQueryRequest req = mock(SolrQueryRequest.class);
	SolrParams params = mock(SolrParams.class);
	when(req.getParams()).thenReturn(params);
	FacetTreeParameters ftParams = mock(FacetTreeParameters.class);

	final String[] facetTrees = new String[] { "{!" + FacetTreeParameters.LOCAL_PARAM_TYPE + " x=y}uri" };
	HierarchicalFacets ftp = new HierarchicalFacets(req, null, null, rb, ftParams);
	ftp.process(facetTrees);
}
 
Example #18
Source File: AbstractReSearcherComponent.java    From solr-researcher with Apache License 2.0 5 votes vote down vote up
@Override
public void modifyRequest(ResponseBuilder rb, SearchComponent who, ShardRequest sreq) {
  if (!checkComponentShouldProcess(rb)) {
    return;
  }

  sreq.params.set(getComponentName(), "false");
}
 
Example #19
Source File: DymReSearcher.java    From solr-researcher with Apache License 2.0 5 votes vote down vote up
@Override
public boolean checkComponentShouldProcess(ResponseBuilder rb) {
  SolrParams params = rb.req.getParams();
  if (!params.getBool(COMPONENT_NAME, false)) {
    return false;
  }
  if (!params.getBool(SpellCheckComponent.COMPONENT_NAME, false)) {
    return false;
  }

  return true;
}
 
Example #20
Source File: SolrFacetService.java    From chronix.server with Apache License 2.0 5 votes vote down vote up
/**
 * @param req          the solr query request
 * @param rsp          the solr query response
 * @param matchingDocs the set of matching documents
 * @param solrParams   the solr request params
 * @return pivot processor
 */
PivotFacetProcessor pivotFacetProcessor(SolrQueryRequest req,
                                        SolrQueryResponse rsp,
                                        DocSet matchingDocs,
                                        SolrParams solrParams) {
    ResponseBuilder rb = new ResponseBuilder(req, rsp, emptyList());
    rb.doFacets = true;
    return new PivotFacetProcessor(req, matchingDocs, solrParams, rb);
}
 
Example #21
Source File: QueryLoggingComponent.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void finishStage(ResponseBuilder rb)
{
    super.finishStage(rb);
    if (rb.stage != ResponseBuilder.STAGE_GET_FIELDS)
        return;
    try
    {
        log(rb);
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
}
 
Example #22
Source File: DymReSearcher.java    From solr-researcher with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
private Set<String> createSuggestions(ResponseBuilder rb, NamedList suggestions,
    SuggestionsFoundRatioCalculator ratioCalc, int spellcheckCount, long originalQueryHits, Float highestRatio) {
  SuggestionRatioProcessor ratioCalcProcessor = new SuggestionRatioProcessor(originalQueryHits, spellcheckCount,
      ratioCalc, highestRatio);
  ReSearcherUtils.iterateOverSpellcheckerSuggestionsForAllIncorrectWords(suggestions, ratioCalcProcessor);

  Set<String> newSuggestions = null;

  if (ratioCalcProcessor.getSuggestionWithHighestRatio() != null) {
    CreateNewSuggestionsProcessor newSuggestionsProcessor = new CreateNewSuggestionsProcessor(rb.getQueryString(),
        ratioCalcProcessor.getSuggestionWithHighestRatio());
    ReSearcherUtils.iterateOverSpellcheckerSuggestionsForWord(ratioCalcProcessor.getSuggestionWithHighestRatio(),
        newSuggestionsProcessor);
    newSuggestions = newSuggestionsProcessor.getNewSuggestions();
  }

  if (ignoreCollation == false) {
    //newSuggestions.add((String) suggestions.get("collation"));
      if (rb.rsp.getValues().get("spellcheck") != null) {
          if (((SimpleOrderedMap) rb.rsp.getValues().get("spellcheck")).get("collations") != null) {
              newSuggestions.add((String) (((NamedList) ((SimpleOrderedMap) rb.rsp.getValues().get("spellcheck")).get("collations")).get("collation")));
          }
      }
  }

  return newSuggestions;
}
 
Example #23
Source File: AutoCompleteSearchComponent.java    From solr-autocomplete with Apache License 2.0 5 votes vote down vote up
@Override
public int distributedProcess(ResponseBuilder rb) throws IOException {
  if (searchHandler != null && checkComponentShouldProcess(rb)) {
    doProcess(rb);
  }
  
  return ResponseBuilder.STAGE_DONE;
}
 
Example #24
Source File: SpatialClusteringComponent.java    From solr-spatial-clustering with Apache License 2.0 5 votes vote down vote up
@Override
public void prepare(ResponseBuilder responseBuilder) throws IOException {
    if (!isEnabled(responseBuilder)) {
        return;
    }

    responseBuilder.setNeedDocSet(true);
}
 
Example #25
Source File: ReSearcherUtils.java    From solr-researcher with Apache License 2.0 5 votes vote down vote up
public static long extractOriginalQueryHits(ResponseBuilder rb) {
  long hits = 0;
  if (rb.rsp != null) {
    Number hitsInteger = (Number) rb.rsp.getToLog().get("hits");
    if (hitsInteger == null) {
      hits = rb.getNumberDocumentsFound();
    } else {
      hits = hitsInteger.longValue();
    }
  }
  return hits;
}
 
Example #26
Source File: LocalStatsCache.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
protected ShardRequest doRetrieveStatsRequest(ResponseBuilder rb) {
  log.debug("## RSR {}", rb.req);
  // already incremented the stats - decrement it now
  statsCacheMetrics.retrieveStats.decrement();
  return null;
}
 
Example #27
Source File: MtasSolrComponentKwic.java    From mtas with Apache License 2.0 5 votes vote down vote up
public void finishStage(ResponseBuilder rb) {
  if (rb.req.getParams().getBool(MtasSolrSearchComponent.PARAM_MTAS, false)
      && rb.stage >= ResponseBuilder.STAGE_EXECUTE_QUERY
      && rb.stage < ResponseBuilder.STAGE_GET_FIELDS) {
    for (ShardRequest sreq : rb.finished) {
      if (sreq.params.getBool(MtasSolrSearchComponent.PARAM_MTAS, false)
          && sreq.params.getBool(PARAM_MTAS_KWIC, false)) {
        // nothing to do
      }
    }
  }
}
 
Example #28
Source File: TextLogisticRegressionQParserPlugin.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
TextLogisticRegressionCollector(ResponseBuilder rbsp, IndexSearcher searcher,
                                TrainingParams trainingParams) {
  this.trainingParams = trainingParams;
  this.workingDeltas = new double[trainingParams.weights.length];
  this.weights = Arrays.copyOf(trainingParams.weights, trainingParams.weights.length);
  this.rbsp = rbsp;
  this.classificationEvaluation = new ClassificationEvaluation();
  this.searcher = searcher;
  positiveDocsSet = new SparseFixedBitSet(searcher.getIndexReader().numDocs());
  docsSet = new SparseFixedBitSet(searcher.getIndexReader().numDocs());
}
 
Example #29
Source File: MergeSearchComponent.java    From BioSolr with Apache License 2.0 5 votes vote down vote up
@Override
public void modifyRequest(ResponseBuilder rb, SearchComponent who, ShardRequest sreq) {
  // do the filterQParser stuff first
  super.modifyRequest(rb, who, sreq);
  
  if (! doMerge(rb)) {
    return;
  }
  
  ReturnFields rf = rb.rsp.getReturnFields();
  if (rf.wantsAllFields()) {
    // we already have what we need since we ask for everything...
    return;
  }

  IndexSchema schema = rb.req.getCore().getLatestSchema();
  for (SchemaField field : schema.getFields().values()) {
    if (! rf.wantsField(field.getName())) {
      continue;
    }
    for (String source : schema.getCopySources(field.getName())) {
      if (rf.wantsField(source)) {
        continue;
      }
      sreq.params.add(CommonParams.FL, source);
    }
  }
}
 
Example #30
Source File: AggregationWaitable.java    From semantic-knowledge-graph with Apache License 2.0 5 votes vote down vote up
public void aggregate() throws IOException {
    FacetModule mod = new FacetModule();
    SolrQueryResponse resp = new SolrQueryResponse();
    ResponseBuilder rb = getResponseBuilder(resp);
    mod.prepare(rb);
    mod.process(rb);
    parseResponse(resp);
}