This topic describes how to call the Java API operations of open source Apache Solr to access LindormSearch and provides examples.
Prerequisites
- JDK 1.6 or later is installed on a host.
- The IP address of your client is added to the whitelist of your Lindorm instance. For more information, see Configure whitelists.
Step 1: Download solr-solrj
Add a Maven dependency. If you create a project in a Maven repository, add the solr-solrj dependency to the pom.xml file. Example:
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>8.9.0</version>
</dependency>Step 2: Create and initialize an instance
- If you want to access the instance over the Internet, use the HttpSolrClient mode. The following code provides an example on how to access the instance over the Internet:
String httpUrl = "http://***:8983/solr/"; HttpSolrClient solrClient = new HttpSolrClient.Builder(httpUrl).build(); - If you want to access the instance over a private network, use the CloudSolrClient mode. The following code provides an example on how to access the instance over a private network:
String zkHost = "zk1:2181,zk2:2181,zk3:2181/solr" CloudSolrClient solrClient = new CloudSolrClient.Builder(Collections.singletonList(zkHost), Optional.empty()).build();Note CloudSolrClient ensures thread safety and allows multiple threads of an application to share the same object.
Step 3: Access LindormSearch
- The following code provides an example on how to write data:
List 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); - The following code provides an example on how to 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"); //do something } solrClient.close();Note For more information about sample code that shows how to query data, see Sample code.