LindormSearch is compatible with the Apache Solr Java API. If your application already uses SolrJ, you can connect to LindormSearch with minimal code changes.
Apache Solr API operations only work within a Virtual Private Cloud (VPC). Internet access is not supported.
Prerequisites
Before you begin, ensure that you have:
-
JDK 1.6 or later installed on your client host
-
Your client IP address added to the Lindorm instance whitelist. See Configure whitelists
Step 1: Add the solr-solrj dependency
Add solr-solrj to your project.
Maven — add to pom.xml:
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>8.9.0</version>
</dependency>
Step 2: Choose a client and initialize
LindormSearch supports two SolrJ client types:
| Client | Connection |
|---|---|
HttpSolrClient |
VPC HTTP endpoint (httpUrl) |
CloudSolrClient |
LindormSearch endpoint for Solr (zkHost) |
HttpSolrClient
Contact Alibaba Cloud technical support (DingTalk ID: s0s3eg3) to get the VPC HTTP endpoint (httpUrl) for LindormSearch.
String httpUrl = "http://<host>:<port>/solr/";
HttpSolrClient solrClient = new HttpSolrClient.Builder(httpUrl).build();
CloudSolrClient
Get the LindormSearch endpoint for Solr from the Lindorm console. See View endpoints for the steps.
String zkHost = "ld-bp16cf8611a4r****-proxy-zk.lindorm.rds.aliyuncs.com:2***/solr";
CloudSolrClient solrClient = new CloudSolrClient.Builder(
Collections.singletonList(zkHost),
Optional.empty()
).build();
CloudSolrClient is thread-safe. Multiple threads in the same application can share a single instance.
Step 3: Write and query data
Write data
List<SolrInputDocument> docs = new ArrayList<>();
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", 1);
doc.addField("name_s", "bob");
doc.addField("age_i", 18);
docs.add(doc);
solrClient.add(docs);
Query data
SolrQuery solrQuery = new SolrQuery("name_s:bob");
QueryResponse response = solrClient.query("your_index", solrQuery);
SolrDocumentList documentList = response.getResults();
for (SolrDocument doc : documentList) {
String id = (String) doc.getFieldValue("id");
// process each document
}
solrClient.close();
For a complete query example, see SolrQueryDemo.java on GitHub.