Question

I'm using spring-data-solr and have data import handler(DIH) in solr. How can I call DIH by repository or SolrTemplate, or some else?

Was it helpful?

Solution

I'd recommend a Custom Respository using SolrCallback to execute desired request.

    @Override
    public SolrResponse dataImport(final String command) {

      return solrTemplate.execute(new SolrCallback<SolrResponse>() {

        @Override
        public SolrResponse doInSolr(SolrServer solrServer) throws SolrServerException, IOException {
          return new SolrRequest(METHOD.GET, "/dataimport?command=" + command) {

            //..skipped some methods to shorten

            @Override
            public SolrResponse process(SolrServer server) throws SolrServerException, IOException {
              SolrResponseBase response = new SolrResponseBase();
              response.setResponse(server.request(this));
              return response;
            }

          }.process(solrServer);

        }
      });
    }

OTHER TIPS

If someone else is struggling to get the DIH working in the current version (Solr 8.11) - this is what worked for me (be sure to adapt the core name):

        solrTemplate.execute(new SolrCallback<SolrResponse>() {
            @Override
            public SolrResponse doInSolr(SolrClient solrClient) throws SolrServerException, IOException {

                SolrRequest<SolrResponse> solrRequest = new SolrRequest<>(SolrRequest.METHOD.GET, "/<core_name>/dataimport?command=full-import&commit=true&clean=true") {
                    @Override
                    public SolrParams getParams() {
                        return null;
                    }

                    @Override
                    protected SolrResponse createResponse(SolrClient solrClient) {
                        SolrResponseBase response = new SolrResponseBase();
                        try {
                            response.setResponse(solrClient.request(this));
                        } catch (SolrServerException | IOException e) {
                            throw new RuntimeException(e);
                        }
                        return response;
                    }
                };

                solrRequest.setResponseParser(new DelegationTokenResponse.JsonMapResponseParser());
                return solrRequest.process(solrClient);

            }
        });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top