使用 Java 中的 Retrieve 和 Rank 进行搜索和排名

安装所需的依赖项:

'org.apache.solr:solr-solrj:5.5.1'
'org.apache.httpcomponents:httpclient:4.3.6'
'com.ibm.watson.developer_cloud:java-sdk:3.2.0'

下面的代码假设你有一个包含文档的 Solr 集合,并且你已经培训了一个排名,否则请遵循本教程

public class RetrieveAndRankSolrJExample {

  private static HttpSolrClient solrClient;
  private static RetrieveAndRank service;

  private static String USERNAME = "<username>";
  private static String PASSWORD = "<password>";
  private static String SOLR_CLUSTER_ID = "<your-solr-cluster-id>";
  private static String SOLR_COLLECTION_NAME = "<your-collection-name>";
  private static String RANKER_ID = "<ranker-id>";
  
  public static void main(String[] args) throws SolrServerException, IOException {
     
    // create the retrieve and rank instance
    service = new RetrieveAndRank();
    service.setUsernameAndPassword(USERNAME, PASSWORD);

    // create the solr client
    String solrUrl = service.getSolrUrl(SOLR_CLUSTER_ID);
    solrClient = new HttpSolrClient(solrUrl, createHttpClient(solrUrl, USERNAME, PASSWORD));

    // build the query
    SolrQuery query = new SolrQuery("*:*");
    query.setRequestHandler("/fcselect");
    query.set("ranker_id", RANKER_ID);
    
    // execute the query
    QueryResponse response = solrClient.query(SOLR_COLLECTION_NAME, query);
    System.out.println("Found " + response.getResults().size() + " documents!");
    System.out.println(response);
  }

  private static HttpClient createHttpClient(String uri, String username, String password) {
    final URI scopeUri = URI.create(uri);

    final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(new AuthScope(scopeUri.getHost(), scopeUri.getPort()),
        new UsernamePasswordCredentials(username, password));

    final HttpClientBuilder builder = HttpClientBuilder.create()
        .setMaxConnTotal(128)
        .setMaxConnPerRoute(32)
        .setDefaultRequestConfig(RequestConfig.copy(RequestConfig.DEFAULT).setRedirectsEnabled(true).build())
        .setDefaultCredentialsProvider(credentialsProvider)
        .addInterceptorFirst(new PreemptiveAuthInterceptor());
    return builder.build();
  }

  private static class PreemptiveAuthInterceptor implements HttpRequestInterceptor {
    public void process(final HttpRequest request, final HttpContext context) throws HttpException {
      final AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE);

      if (authState.getAuthScheme() == null) {
        final CredentialsProvider credsProvider = (CredentialsProvider) context
            .getAttribute(HttpClientContext.CREDS_PROVIDER);
        final HttpHost targetHost = (HttpHost) context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
        final Credentials creds = credsProvider.getCredentials(new AuthScope(targetHost.getHostName(),
            targetHost.getPort()));
        if (creds == null) {
          throw new HttpException("No creds provided for preemptive auth.");
        }
        authState.update(new BasicScheme(), creds);
      }
    }
  }
}