0

This is our custom component class, in which I want to get the data from one core and search data into another core and use facet query and show in descending order. means suppose we get t-shirt name from one core and same t-shirt name we will search it into another core and display the over all result with facet query

package com.shop.component;
import org.apache.solr.handler.component.ResponseBuilder;
import org.apache.solr.handler.component.SearchComponent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
public class CustomQueryComponent extends SearchComponent{
private static final Logger LOG = LoggerFactory.getLogger(CustomQueryComponent.class);
@Override
    public void prepare(ResponseBuilder responseBuilder) throws IOException {
        LOG.info("prepare method of CustomQueryComponent");
    }
@Override
    public void process(ResponseBuilder responseBuilder) throws IOException {
        LOG.info("process method of CustomQueryComponent");
    }
@Override
    public String getDescription() {
        return "CustomQueryComponent";
    }
}

  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Dec 27 '21 at 18:08

1 Answers1

0

You can use the org.apache.solr.client.solrj.embedded.EmbeddedSolrServer in your own search component to fire off a Solr-query. In my example I am doing a call to the same core, so the only thing left is to figure out how it can be done to a different core: @Override public void prepare(final ResponseBuilder responseBuilder) throws IOException { SolrParams params = responseBuilder.req.getParams();

    SolrClient solrClient = new EmbeddedSolrServer(responseBuilder.req.getCore());
    
    ModifiableSolrParams subQueryParams = new ModifiableSolrParams(params);
    if( subQueryParams.get("rows").equals("0"))//needed to prevent an infinite loop
    {
        return; 
    }
    
    subQueryParams.set("rows",0);
    subQueryParams.set("facet",true);

    subQueryParams.set("facet.field", PIM_FIELDS);

    try
    {
        QueryResponse response = solrClient.query(subQueryParams);
                
        FacetField pimFields = response.getFacetField(PIM_FIELDS);

        List<FacetField.Count> values = pimFields.getValues();

        ModifiableSolrParams newParams = new ModifiableSolrParams(params);
        for (final FacetField.Count value : values)
        {
            newParams.add("facet.field", value.getName());              
            value.getFacetField().getName();
        }
        newParams.set("facet", true);
        newParams.set("facet.mincount",1);
        responseBuilder.req.setParams(newParams);

    }
    catch (SolrServerException e)
    {
        throw new RuntimeException(e);
    }
}

You can reach other collections running on the same solr-instance via the coreContainer on the req.getCore(), as I do in this example:

private SolrDocumentList getEntries(final ResponseBuilder responseBuilder) throws IOException
{
    ModifiableSolrParams unitQueryParams = new ModifiableSolrParams();
    unitQueryParams.add("q","*:*");
    unitQueryParams.add("rows","300");

    SolrCore localDoc = responseBuilder.req.getCore().getCoreContainer().getCore("webshop_classificationattributeunit");
    EmbeddedSolrServer unitQueryClient = new EmbeddedSolrServer(localDoc);


    QueryResponse unitResponse;
    try
    {
        unitResponse = unitQueryClient.query(unitQueryParams);
    }
    catch (SolrServerException e)
    {
        throw new RuntimeException(e);
    }
    
    return unitResponse.getResults();
}
rustyfinger
  • 199
  • 2
  • 7