solrj Getting started with solrj

30% OFF - 9th Anniversary discount on Entity Framework Extensions until December 15 with code: ZZZANNIVERSARY9


This section provides an overview of what solrj is, and why a developer might want to use it.

It should also mention any large subjects within solrj, and link out to the related topics. Since the Documentation for solrj is new, you may need to create initial versions of those related topics.

Add documents to Solr using SolrJ

If we have a schema looking like:

<field name="id" type="string" indexed="true" stored="true" required="true" /> 

<field name="name" type="string" indexed="true" stored="true" />

the solrj code to add content to Solr will look like:

 package com.stackoverflow.solrj.example;

import java.util.ArrayList;
import java.util.Collection;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.common.SolrInputDocument;

public class SolrJIndexing {

    private final static String SOLR_URL = "http://localhost:8983/solr/mycollection";

    public static void main(String[] args) {
        SolrClient solrClient = new HttpSolrClient.Builder(SOLR_URL).build();
        SolrInputDocument document1 = getDocument(1, "Document example 1");
        SolrInputDocument document2 = getDocument(2, "Document example 2");
        Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
        try {
        } catch (SolrServerException | IOException e) {

    private static SolrInputDocument getDocument(int id, String name) {
        SolrInputDocument document = new SolrInputDocument();
        document.addField("id", id);
        document.addField("name", name);
        return document;


Init SolrJ client for local STANDALONE cluster vs remote CLOUD with Kerberos

Handy method to initialise SolrJ client based on configuration properties;

private void initSolrClient() {
    if (conf.kerberos().isEnabled()) {
        System.setProperty("", conf.kerberos().confPath());
        HttpClientUtil.addConfigurer(new Krb5HttpClientConfigurer());
    if (conf.solr().getMode() == SolrProperties.Mode.STANDALONE) {
        this.solr = new HttpSolrClient.Builder(conf.solr().url()).build();
    if (conf.solr().getMode() == SolrProperties.Mode.CLOUD) {
        this.solr = new CloudSolrClient.Builder().withZkHost(conf.solr().zookeepers()).build();

REMOTE with Kerberos:


LOCAL development:


Installation or Setup

SolrJ comes as part of the Solr distribution since Solr 1.x. The latest Solr version can be downloaded here. From Solr-6.3.0 that is the latest version available, we need to get the following libraries and add them to our build path: /dist/apache-solr-solrj-.jar /dist/solrj-lib/

Once we're done, SolrJ is ready to communicate with our Solr instance. In the following example we'll see how to use SolrJ to query or add content to Solr.

Query Solr with SolrJ

If you added successfully documents to Solr using the previous example, you'll be now able to retrieve them in this way:

package com.stackoverflow.solrj.example;

import java.util.Iterator;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;

public class SolrJQueryExample {

    private final static String SOLR_URL = "http://localhost:8983/solr/mycollection";

    public static void main(String[] args) throws SolrServerException, IOException {

        SolrClient solrClient = new HttpSolrClient.Builder(SOLR_URL).build();
        SolrQuery solrQuery = new SolrQuery();

        QueryResponse queryResponse = solrClient.query(solrQuery);
        SolrDocumentList solrDocs = queryResponse.getResults();
        Iterator<SolrDocument> iterator = solrDocs.iterator();
        while (iterator.hasNext()) {
            SolrDocument solrDocument =;
            String docId = (String) solrDocument.getFieldValue("id");
            String docName = (String) solrDocument.getFieldValue("name");
            System.out.println("Document " + docId + ": " + docName);


Got any solrj Question?