View Javadoc
1   /*
2    * Copyright (C) 2024 B3Partners B.V.
3    *
4    * SPDX-License-Identifier: MIT
5    */
6   package org.tailormap.api.solr;
7   
8   import io.micrometer.core.instrument.Metrics;
9   import java.io.IOException;
10  import java.util.concurrent.TimeUnit;
11  import org.apache.solr.client.solrj.SolrClient;
12  import org.apache.solr.client.solrj.SolrServerException;
13  import org.apache.solr.client.solrj.impl.HttpJdkSolrClient;
14  import org.apache.solr.client.solrj.request.HealthCheckRequest;
15  import org.apache.solr.client.solrj.response.HealthCheckResponse;
16  import org.springframework.beans.factory.annotation.Value;
17  import org.springframework.stereotype.Service;
18  
19  @Service
20  public class SolrService {
21    @Value("${tailormap-api.solr-url}")
22    private String solrUrl;
23  
24    @Value("${tailormap-api.solr-core-name:tailormap}")
25    private String solrCoreName;
26  
27    @Value("${tailormap-api.solr-connection-timeout-seconds:60}")
28    private int solrConnectionTimeout;
29  
30    @Value("${tailormap-api.solr-request-timeout-seconds:240}")
31    private int solrRequestTimeout;
32  
33    @Value("${tailormap-api.solr-idle-timeout-seconds:10}")
34    private int solrIdleTimeout;
35  
36    /**
37     * Get a concurrent update Solr client for bulk operations.
38     *
39     * @return the Solr client
40     */
41    public SolrClient getSolrClientForIndexing() {
42      return new HttpJdkSolrClient.Builder(this.solrUrl + this.solrCoreName)
43          .withFollowRedirects(true)
44          .withConnectionTimeout(solrConnectionTimeout, TimeUnit.SECONDS)
45          .withRequestTimeout(solrRequestTimeout, TimeUnit.SECONDS)
46          // Set maxConnectionsPerHost for http1 connections,
47          // maximum number http2 connections is limited to 4
48          // .withMaxConnectionsPerHost(10)
49          .withIdleTimeout(solrIdleTimeout, TimeUnit.SECONDS)
50          .build();
51    }
52  
53    /**
54     * Get a Solr client for searching.
55     *
56     * @return the Solr client
57     */
58    public SolrClient getSolrClientForSearching() {
59      return new HttpJdkSolrClient.Builder()
60          .withBaseSolrUrl(this.solrUrl)
61          .withDefaultCollection(this.solrCoreName)
62          .withConnectionTimeout(solrConnectionTimeout, TimeUnit.SECONDS)
63          .withFollowRedirects(true)
64          .build();
65    }
66  
67    /**
68     * Get a Solr client without a core name or collection. This can be used for e.g. getting the Solr status, health or
69     * creating a new core.
70     *
71     * @return the Solr client configured for the base Solr URL without a core name or collection
72     */
73    public SolrClient getBaseUrlSolrClient() {
74      return new HttpJdkSolrClient.Builder()
75          .withConnectionTimeout(solrConnectionTimeout / 2, TimeUnit.SECONDS)
76          .withFollowRedirects(false)
77          .withBaseSolrUrl(this.solrUrl)
78          .build();
79    }
80  
81    public void setSolrUrl(String solrUrl) {
82      this.solrUrl = solrUrl;
83    }
84  
85    public boolean isSolrServiceAvailable() {
86      try (SolrClient client = getBaseUrlSolrClient()) {
87        HealthCheckRequest request = new HealthCheckRequest();
88        HealthCheckResponse response = request.process(client);
89        Metrics.timer("tailormap_solr_ping").record(response.getElapsedTime(), TimeUnit.MILLISECONDS);
90        return true;
91      } catch (SolrServerException | IOException e) {
92        return false;
93      }
94    }
95  }