This guide helps you to understand how to add documents to Solr using SolrJ.

Add Documents to Solr:

In previous tutorials, we have seen how to install Apache Solr on windows 10 and created solr core (sample-items). Now as part of this example I am going to add some items to solr.

Technologies:

  • Java 8
  • Apache Solr 7.5.0
  • Apache SolrJ 7.5.0
  • Apache Http Core 4.4.6
  • Apache Http Client 4.5.3
  • Maven

Maven Dependency:

Add the below solrj dependency on your pom.xml, it will get all the necessary dependencies to connect with solr.

pom.xml
<dependency>
     <groupId>org.apache.solr</groupId>
     <artifactId>solr-solrj</artifactId>
     <version>7.5.0</version>
</dependency>

Add Documents to Solr Example:

  • Initiating the HttpSolrClient by connecting the Solr server.
  • Defining the data needs to be indexed using a SolrInputDocument and adding it to index using the add() method
  • Commit the indexed data using the commit() method.
AddDocuments.java
package com.onlinetutorilspoint.solr;

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

public class AddDocuments {
    public static void main(String[] args) throws Exception{
        String SOLR_URL="http://localhost:8983/solr/sample-items";
        SolrClient solrClient = new HttpSolrClient.Builder(SOLR_URL).build();

        SolrInputDocument document = new SolrInputDocument();
        document.addField("item_id",101);
        document.addField("item_name","Spring in action");
        document.addField("item_category","Books");

        solrClient.add(document);
        solrClient.commit();
    }

}

Runit:

Note: In order to run this example, the solar should up and running on the same machine.

Terminal
>javac AddDocuments.java
>java AddDocuments

Check Solr server:

Apache Solr - How to Add Documents to Solr using SolrJ 1-min

Adding Documents with Beans:

Here we are going to see how to add documents to Solr with java beans. Create Item.java class representing an item and define each property with @Field annotation.

Item.java
package com.onlinetutorilspoint.solr;

import org.apache.solr.client.solrj.beans.Field;

public class Item {
    private int item_id;
    private String item_name;
    private String category;

    public Item(int item_id, String item_name, String category) {
        this.item_id = item_id;
        this.item_name = item_name;
        this.category = category;
    }

    @Field("item_id")
    public void setItem_id(int item_id) {
        this.item_id = item_id;
    }
    @Field("item_name")
    public void setItem_name(String item_name) {
        this.item_name = item_name;
    }
    @Field("category")
    public void setCategory(String category) {
        this.category = category;
    }

    public int getItem_id() {
        return item_id;
    }

    public String getItem_name() {
        return item_name;
    }

    public String getCategory() {
        return category;
    }
}

Create as much item bean instances you want and add it to the List and finally the list to be added to addBeans() method.

AddDocumentsWithBean.java
package com.onlinetutorilspoint.solr;

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

import java.util.Arrays;
import java.util.List;

public class AddDocumentsWithBean {
    public static void main(String[] args) throws Exception{
        String SOLR_URL="http://localhost:8983/solr/sample-items";
        SolrClient solrClient = new HttpSolrClient.Builder(SOLR_URL).build();

        List<Item> itemList = Arrays.asList(new Item(102,"iPhoneX","Mobiles"),
                new Item(103,"Apple Mac Book Pro","Laptop"));

        solrClient.addBeans(itemList);
        solrClient.commit();
    }

}

Run:

Terminal
>javac AddDocumentsWithBean.java
>java AddDocumentsWithBean

Check Solr Server:

Apache Solr - How to Add Documents to Solr using SolrJ 2-min

References:

Solr Installation and creating the core

SolrJ HttpSolrClient

Happy Learning 🙂