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 java.util.concurrent.TimeUnit;
9   import org.apache.solr.client.solrj.SolrClient;
10  import org.apache.solr.client.solrj.impl.Http2SolrClient;
11  import org.springframework.beans.factory.annotation.Value;
12  import org.springframework.stereotype.Service;
13  
14  @Service
15  public class SolrService {
16    @Value("${tailormap-api.solr-url}")
17    private String solrUrl;
18  
19    @Value("${tailormap-api.solr-core-name:tailormap}")
20    private String solrCoreName;
21  
22    @Value("${tailormap-api.solr-connection-timeout-seconds:60}")
23    private int solrConnectionTimeout;
24  
25    @Value("${tailormap-api.solr-request-timeout-seconds:240}")
26    private int solrRequestTimeout;
27  
28    @Value("${tailormap-api.solr-idle-timeout-seconds:10}")
29    private int solrIdleTimeout;
30  
31    /**
32     * Get a concurrent update Solr client for bulk operations.
33     *
34     * @return the Solr client
35     */
36    public SolrClient getSolrClientForIndexing() {
37      return new Http2SolrClient.Builder(this.solrUrl + this.solrCoreName)
38          .withFollowRedirects(true)
39          .withConnectionTimeout(solrConnectionTimeout, TimeUnit.SECONDS)
40          .withRequestTimeout(solrRequestTimeout, TimeUnit.SECONDS)
41          // Set maxConnectionsPerHost for http1 connections,
42          // maximum number http2 connections is limited to 4
43          // .withMaxConnectionsPerHost(10)
44          .withIdleTimeout(solrIdleTimeout, TimeUnit.SECONDS)
45          .build();
46    }
47  
48    /**
49     * Get a Solr client for searching.
50     *
51     * @return the Solr client
52     */
53    public SolrClient getSolrClientForSearching() {
54      return new Http2SolrClient.Builder(this.solrUrl + this.solrCoreName)
55          .withConnectionTimeout(solrConnectionTimeout, TimeUnit.SECONDS)
56          .withFollowRedirects(true)
57          .build();
58    }
59  
60    public void setSolrUrl(String solrUrl) {
61      this.solrUrl = solrUrl;
62    }
63  }