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.ConcurrentUpdateHttp2SolrClient;
11  import org.apache.solr.client.solrj.impl.Http2SolrClient;
12  import org.springframework.beans.factory.annotation.Value;
13  import org.springframework.stereotype.Service;
14  
15  @Service
16  public class SolrService {
17    @Value("${tailormap-api.solr-url}")
18    private String solrUrl;
19  
20    @Value("${tailormap-api.solr-core-name:tailormap}")
21    private String solrCoreName;
22  
23    /**
24     * Get a concurrent update Solr client for bulk operations.
25     *
26     * @return the Solr client
27     */
28    public SolrClient getSolrClientForIndexing() {
29      return new ConcurrentUpdateHttp2SolrClient.Builder(
30              this.solrUrl + this.solrCoreName,
31              new Http2SolrClient.Builder()
32                  .withFollowRedirects(true)
33                  .withConnectionTimeout(10000, TimeUnit.MILLISECONDS)
34                  .withRequestTimeout(60000, TimeUnit.MILLISECONDS)
35                  .build())
36          .withQueueSize(SolrHelper.SOLR_BATCH_SIZE * 2)
37          .withThreadCount(10)
38          .build();
39    }
40  
41    /**
42     * Get a Solr client for searching.
43     *
44     * @return the Solr client
45     */
46    public SolrClient getSolrClientForSearching() {
47      return new Http2SolrClient.Builder(this.solrUrl + this.solrCoreName)
48          .withConnectionTimeout(10, TimeUnit.SECONDS)
49          .withFollowRedirects(true)
50          .build();
51    }
52  
53    public void setSolrUrl(String solrUrl) {
54      this.solrUrl = solrUrl;
55    }
56  }