lucene-solr-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Shawn Heisey <apa...@elyograg.org>
Subject Re: How to set credentials when querying using SolrJ - Basic Authentication
Date Mon, 01 Aug 2016 23:14:36 GMT
On 8/1/2016 1:59 PM, Susheel Kumar wrote:
> BUT how do we set the credentials when calling
>
> //HOW to set credentials before calling query method
> ???????
> ???????
> solrClient.query(collection, query);    method?

Here's an example of setting credentials on an arbitrary request object
that is then sent to a specific collectionthat should always work:

  SolrClient client = new CloudSolrClient("localhost:9893");
  String collection = "gettingstarted";
  String username = "test";
  String password = "password";

  SolrQuery query = new SolrQuery();
  query.setQuery("*:*");
  // Do any other query setup needed.

  SolrRequest<QueryResponse> req = new QueryRequest(query);
  req.setBasicAuthCredentials(username, password);
  QueryResponse rsp = req.process(client, collection);
  System.out.println("numFound: " + rsp.getResults().getNumFound());

You can use a similar approach with UpdateRequest to what I've done here
with QueryRequest.

When you use the sugar methods (like query, update, commit, etc),
SolrClient builds a request object and then uses the "process" method on
the request.  The example above just makes this more explicit.

After I wrote the above code, I discovered that there is an alternate
request method that includes the collection parameter.  This method
exists in the 5.4 version of SolrJ, which is the minimum version
required for setBasicAuthCredentials.  I think the user code would be
about the same size either way.

Thanks,
Shawn


Mime
View raw message