{"id":93159,"date":"2020-08-11T11:00:00","date_gmt":"2020-08-11T08:00:00","guid":{"rendered":"https:\/\/examples.javacodegeeks.com\/?p=93159"},"modified":"2020-08-10T11:18:33","modified_gmt":"2020-08-10T08:18:33","slug":"apache-solr-in-java-using-apache-solrj","status":"publish","type":"post","link":"https:\/\/examples.javacodegeeks.com\/apache-solr-in-java-using-apache-solrj\/","title":{"rendered":"Apache Solr in Java: Using Apache SolrJ"},"content":{"rendered":"<p>In this example, we are going to show you how to use Apache SolrJ to index data in Solr and query from Solr.<\/p>\n<h2 class=\"wp-block-heading\"><a name=\"introduction\"><\/a>1. Introduction<\/h2>\n<p>Apache Solr is a popular <a href=\"https:\/\/lucene.apache.org\/solr\/\">open-source<\/a> search platform built on Apache Lucene. If we take a 30000-foot view, Solr is a web application and HTTP is the fundamental protocol used between client applications and Solr. The client sends a request to Solr and Solr does some work and returns a response. <\/p>\n<p>Other than HTTP API, SolrJ offers a Java API which encapsulates much of the work of sending requests and parsing responses. It is highly configurable and makes it much easier for applications written in Java to talk to Solr.<\/p>\n<div class=\"toc\">\n<h3>Table Of Contents<\/h3>\n<dl>\n<dt><a href=\"#introduction\">1. Introduction<\/a><\/dt>\n<dt><a href=\"#technologies_used\">2. Technologies Used<\/a><\/dt>\n<dt><a href=\"#using_apache_solrj\">3. Using Apache SolrJ<\/a><\/dt>\n<dd>\n<dl>\n<dt><a href=\"#basics\">3.1 Basics<\/a><\/dt>\n<dt><a href=\"#adding_dependencies\">3.2 Adding Dependencies<\/a><\/dt>\n<dt><a href=\"#starting_solr_instance\">3.3 Starting Solr Instance<\/a><\/dt>\n<dt><a href=\"#indexing\">3.4 Indexing Using SolrJ<\/a><\/dt>\n<dt><a href=\"#querying\">3.5 Querying Using SolrJ<\/a><\/dt>\n<dt><a href=\"#running\">3.5 Running the Example<\/a><\/dt>\n<\/dl>\n<\/dd>\n<dt><a href=\"#download\">4. Download the Source Code<\/a><\/dt>\n<\/dl>\n<\/div>\n<h2 class=\"wp-block-heading\"><a name=\"technologies_used\"><\/a>2. Technologies Used<\/h2>\n<p>The steps and commands described in this example are for <a href=\"https:\/\/lucene.apache.org\/solr\/downloads.html#solr-852\" target=\"_blank\" rel=\"noreferrer noopener\">Apache Solr 8.5<\/a> on Windows 10. The JDK version we use to run the SolrCloud in this example is <a href=\"https:\/\/jdk.java.net\/java-se-ri\/13\" target=\"_blank\" rel=\"noreferrer noopener\">OpenJDK 13<\/a>. Before we start, please make sure your computer meet the <a href=\"https:\/\/lucene.apache.org\/solr\/8_5_0\/SYSTEM_REQUIREMENTS.html\" target=\"_blank\" rel=\"noreferrer noopener\">system requirements<\/a>. Also, please download the binary release of <a href=\"https:\/\/lucene.apache.org\/solr\/downloads.html#solr-852\" target=\"_blank\" rel=\"noreferrer noopener\">Apache Solr 8.5<\/a>. <a href=\"https:\/\/maven.apache.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">Apache Maven 3.6.3<\/a> is used as the build system.<\/p>\n<h2 class=\"wp-block-heading\"><a name=\"using_apache_solrj\"><\/a>3. Using Apache SolrJ<\/h2>\n<h3 class=\"wp-block-heading\"><a name=\"basics\"><\/a>3.1 Basics<\/h3>\n<p>SolrJ provides a few simple interfaces for us to connect to and communicate with Solr. The most important one is the <code>SolrClient<\/code> which sends requests in the form of <code>SolrRequests<\/code> and returns responses as <code>SolrResponses<\/code>. There are several SolrClient implementations and we list some commonly used ones in the table below:<\/p>\n<figure class=\"wp-block-table\">\n<table>\n<tbody>\n<tr>\n<th>Client<\/th>\n<th>Description<\/th>\n<\/tr>\n<tr>\n<td><a href=\"https:\/\/lucene.apache.org\/solr\/8_5_2\/solr-solrj\/org\/apache\/solr\/client\/solrj\/impl\/HttpSolrClient.html\" target=\"_blank\" rel=\"noreferrer noopener\">HttpSolrClient<\/a><\/td>\n<td>A general-purpose <code>SolrClient<\/code> implementation that talks directly to a single Solr server via HTTP. It is better suited for query-centric workloads.<\/td>\n<\/tr>\n<tr>\n<td><a href=\"https:\/\/lucene.apache.org\/solr\/8_5_2\/solr-solrj\/org\/apache\/solr\/client\/solrj\/impl\/LBHttpSolrClient.html\" target=\"_blank\" rel=\"noreferrer noopener\">LBHttpSolrClient<\/a><\/td>\n<td>A load balancing wrapper around <code>HttpSolrClient<\/code>. Do NOT use it in Master\/Slave scenarios.<\/td>\n<\/tr>\n<tr>\n<td><a href=\"https:\/\/lucene.apache.org\/solr\/8_5_2\/solr-solrj\/org\/apache\/solr\/client\/solrj\/impl\/CloudSolrClient.html\" target=\"_blank\" rel=\"noreferrer noopener\">CloudSolrClient<\/a><\/td>\n<td>A <code>SolrClient<\/code> implementation that talks to SolrCloud. It communicates with Zookeeper to discover Solr endpoints for SolrCloud collections, and then uses the <code>LBHttpSolrClient<\/code> to issue requests.<\/td>\n<\/tr>\n<tr>\n<td><a href=\"https:\/\/lucene.apache.org\/solr\/8_5_2\/solr-solrj\/org\/apache\/solr\/client\/solrj\/impl\/ConcurrentUpdateSolrClient.html\" target=\"_blank\" rel=\"noreferrer noopener\">ConcurrentUpdateSolrClient<\/a><\/td>\n<td>A thread-safe <code>SolrClient<\/code> implementation which buffers all added documents and writes them into open HTTP connections. It is better suited for indexing-centric workloads.<\/td>\n<\/tr>\n<\/tbody>\n<\/table><figcaption>Table. 1. SolrClient Implementations<\/figcaption><\/figure>\n<p>Before we jump into the SolrJ coding part, we need to get a couple of things ready by following the steps in sections below.<\/p>\n<h3 class=\"wp-block-heading\"><a name=\"adding_dependencies\"><\/a>3.2 Adding dependencies<\/h3>\n<p>The SolrJ API ships with Solr, so a simple way to add SolrJ dependencies when running your java application is to add <code>solr-solrj-8.5.2.jar<\/code> and its dependencies to classpath as below:<\/p>\n<pre class=\"brush:bash\">java -cp .:$SOLR_HOME\/dist\/solrj-lib\/*:$SOLR_HOME\/dist\/solr-solrj-8.5.2.jar ...<\/pre>\n<p>To manage dependencies easily in this example, we use <a href=\"https:\/\/maven.apache.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">Apache Maven 3.6.3<\/a> as the build system. The following dependency declaration needs to be put in <code>pom.xml<\/code>:<\/p>\n<pre class=\"brush:xml\">&lt;dependency&gt;\n  &lt;groupId&gt;org.apache.solr&lt;\/groupId&gt;\n  &lt;artifactId&gt;solr-solrj&lt;\/artifactId&gt;\n  &lt;version&gt;8.5.2&lt;\/version&gt;\n&lt;\/dependency&gt;<\/pre>\n<h3 class=\"wp-block-heading\"><a name=\"starting_solr_instance\"><\/a>3.3 Starting Solr Instance<\/h3>\n<p>For simplicity, instead of setting up a SolrCloud on your local machine as demonstrated in Apache Solr Clustering Example, we run a single Solr instance on our local machine. Before starting, You can simply download jcg_example_configs.zip attached to this article and extract it to the directory ${solr.install.dir}\\server\\solr\\configsets\\jcg_example_configs\\conf. It contains all configurations and schema definitions required by this example. Then run the command below to start the Solr instance:<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<pre class=\"brush:bash\">bin\\solr.cmd start<\/pre>\n<p>The output would be:<\/p>\n<pre class=\"brush:bash\">D:\\Java\\solr-8.5.2&gt;bin\\solr.cmd start\nWaiting up to 30 to see Solr running on port 8983\nStarted Solr server on port 8983. Happy searching!<\/pre>\n<p>In addition, we need to create a new core named <code>jcg_example_core<\/code> with the <code>jcg_example_configs<\/code> configSet on the local machine. For example, we can do it via the CoreAdmin API:<\/p>\n<pre class=\"brush:bash\">curl -G http:\/\/localhost:8983\/solr\/admin\/cores --data-urlencode action=CREATE --data-urlencode name=jcg_example_core --data-urlencode configSet=jcg_example_configs<\/pre>\n<p>The output would be:<\/p>\n<pre class=\"brush:bash\">D:\\Java\\solr-8.5.2&gt;curl -G http:\/\/localhost:8983\/solr\/admin\/cores --data-urlencode action=CREATE --data-urlencode name=jcg_example_core --data-urlencode configSet=jcg_example_configs\n{\n  \"responseHeader\":{\n    \"status\":0,\n    \"QTime\":641},\n  \"core\":\"jcg_example_core\"}<\/pre>\n<p>If the <code>jcg_example_core<\/code> has already existed, you can remove it via the CoreAdmin API as below and start over again:<\/p>\n<pre class=\"brush:bash\">curl -G http:\/\/localhost:8983\/solr\/admin\/cores --data-urlencode action=UNLOAD --data-urlencode core=jcg_example_core --data-urlencode deleteInstanceDir=true<\/pre>\n<p>The output would be:<\/p>\n<pre class=\"brush:bash\">D:\\Java\\solr-8.5.2&gt;curl -G http:\/\/localhost:8983\/solr\/admin\/cores --data-urlencode action=UNLOAD --data-urlencode core=jcg_example_core --data-urlencode deleteInstanceDir=true\n{\n  \"responseHeader\":{\n    \"status\":0,\n    \"QTime\":37}}<\/pre>\n<h3 class=\"wp-block-heading\"><a name=\"indexing\"><\/a>3.4 Indexing Using SolrJ<\/h3>\n<h4 class=\"wp-block-heading\">3.4.1 Building a SolrClient<\/h4>\n<p>First of all, we need to build a <code>SolrClient<\/code> instance. <code>SolrClient<\/code> implementations provide builders with fluence interfaces which are very easy to use. Also, this is a good place to configure <code>SolrClient<\/code> parameters such as Solr base URL, timeouts, etc. The static method below builds a <code>HttpSolrClient<\/code> connecting to the Solr instance running on localhost with 5 seconds connection timeout and 3 seconds read timeout.<\/p>\n<p>Note that we define a static <code>SolrClient<\/code> instance in this example to reuse it everywhere instead of building a new one every time for performance consideration.<\/p>\n<pre class=\"brush:java\">\/**\n * The Solr instance URL running on localhost\n *\/\nprivate static final String SOLR_CORE_URL = \"http:\/\/localhost:8983\/solr\/jcg_example_core\";\n\n\/**\n * The static solrClient instance.\n *\/\nprivate static final SolrClient solrClient = getSolrClient();\n\n\/**\n * Configures SolrClient parameters and returns a SolrClient instance.\n * \n * @return a SolrClient instance\n *\/\nprivate static SolrClient getSolrClient() {\n    return new HttpSolrClient.Builder(SOLR_CORE_URL).withConnectionTimeout(5000).withSocketTimeout(3000).build();\n}<\/pre>\n<h4 class=\"wp-block-heading\">3.4.2 Indexing Articles by Using SolrInputDocument<\/h4>\n<p><code>SolrClient<\/code> provides a straightforward API to add documents to be indexed. <code>org.apache.solr.common.SolrInputDocument<\/code> class is used which represents the field-value information needed to construct and index a Lucene Document. The field values should match those specified in <code>managed-schema.xml<\/code>. In the method below, a list of <code>SolrInputDocument<\/code> are created from a list of sample articles. Fields are explicitly added to each document.<\/p>\n<p>Note that many <code>SolrClient<\/code> implementations have drastically slower indexing performance when documents are added individually. So in the method below, document batching is used by sending a collection of documents to Solr and then commit them for indexing. This generally leads to better indexing performance and should be used whenever possible.<\/p>\n<pre class=\"brush:java\">\/**\n * Indexing articles by using SolrInputDocument.\n *\/\npublic void indexingByUsingSolrInputDocument() {\n    \/\/ create a list of SolrInputDocument\n    List&lt;SolrInputDocument&gt; docs = new ArrayList&lt;SolrInputDocument&gt;();\n    for (Article article : getArticles()) {\n        final SolrInputDocument doc = new SolrInputDocument();\n        doc.addField(\"id\", article.getId());\n        doc.addField(\"category\", article.getCategory());\n        doc.addField(\"title\", article.getTitle());\n        doc.addField(\"author\", article.getAuthor());\n        doc.addField(\"published\", article.isPublished());\n        docs.add(doc);\n    }\n\n    System.out.printf(\"Indexing %d articles...\\n\", docs.size());\n\n    try {\n        \/\/ send the documents to Solr\n        solrClient.add(docs);\n\n        \/\/ explicit commit pending documents for indexing\n        solrClient.commit();\n\n        System.out.printf(\"%d articles indexed.\\n\", docs.size());\n    } catch (SolrServerException | IOException e) {\n        System.err.printf(\"\\nFailed to indexing articles: %s\", e.getMessage());\n    }\n}<\/pre>\n<h4 class=\"wp-block-heading\">3.4.3 Indexing Articles by Using Java Object Binding<\/h4>\n<p>To remember all the fields and add them one by one might be an unpleasant experience and error-prone. SolrJ let us work with<br \/>domain objects directly by implicitly converting documents to and from any class that has been specially marked with <code>@Field<\/code> annotation.<\/p>\n<p>The fields of the <code>Article<\/code> class below are annotated with <code>@Field<\/code> annotations. An annotated field is mapped to a corresponding Solr field. The variable name will be used as the field name in Solr by default. However, this can be overridden by providing the annotation with an explicit field name.<\/p>\n<pre class=\"brush:java\">\/**\n * The article POJO.\n *\/\nclass Article {\n    @Field\n    private String id;\n\n    @Field\n    private String category;\n\n    @Field\n    private String title;\n\n    @Field\n    private String author;\n\n    @Field\n    private boolean published;\n\n    \/\/ constructors\n    \/\/ getters and setters\n}<\/pre>\n<p>Then in the method below, we can simply send a list of articles to Solr for indexing without worrying about the field mapping.<\/p>\n<pre class=\"brush:java\">\/**\n * Indexing articles by using Java object binding.\n *\/\npublic void indexingByUsingJavaObjectBinding() {\n    try {\n        List&lt;Article&gt; articles = getArticles();\n        System.out.printf(\"Indexing %d articles...\\n\", articles.size());\n        \/\/ send articles to Solr\n        solrClient.addBeans(articles);\n\n        \/\/ explicit commit pending documents for indexing\n        solrClient.commit();\n\n        System.out.printf(\"%d articles indexed.\\n\", articles.size());\n    } catch (SolrServerException | IOException e) {\n        System.err.printf(\"\\nFailed to indexing articles: %s\", e.getMessage());\n    }\n}<\/pre>\n<h3 class=\"wp-block-heading\"><a name=\"querying\"><\/a>3.5 Querying Using SolrJ<\/h3>\n<p><code>SolrClient<\/code> has several <code>query()<\/code> methods accepting <code>SolrParams<\/code> which allow us to send a search request to Solr instance. <code>SolrParams<\/code> is designed to hold parameters to Solr and basically it is a MultiMap of String keys to one or more String values. In the method below, we use a <code>MapSolrParams<\/code> instance to hold query parameters and search articles written by Kevin Yang. Once the response is returned, we print the search results to the standard output.<\/p>\n<pre class=\"brush:java\">\/**\n * Querying articles by using SolrParams.\n *\/\npublic void queryingByUsingSolrParams() {\n    \/\/ constructs a MapSolrParams instance\n    final Map&lt;String, String&gt; queryParamMap = new HashMap&lt;String, String&gt;();\n    queryParamMap.put(\"q\", \"author:Kevin\"); \/\/ search articles written by Kevin Yang\n    queryParamMap.put(\"fl\", \"id, title, author\");\n    queryParamMap.put(\"sort\", \"id asc\");\n    MapSolrParams queryParams = new MapSolrParams(queryParamMap);\n\n    \/\/ sends search request and gets the response\n    QueryResponse response = null;\n    try {\n        response = solrClient.query(queryParams);\n    } catch (SolrServerException | IOException e) {\n        System.err.printf(\"Failed to search articles: %s\", e.getMessage());\n    }\n\n    \/\/ print results to stdout\n    if (response != null) {\n        printResults(response.getResults());\n    }\n}<\/pre>\n<p><code>SolrQuery<\/code>, a subclass of <code>SolrParams<\/code>, provides several convenient methods to set the query parameters as shown in the following method:<\/p>\n<pre class=\"brush:java\">\/**\n * Querying articles by using SolrQuery (a subclass of SolrParams).\n *\/\npublic void queryingByUsingSolrQuery() {\n    \/\/ constructs a SolrQuery instance\n    final SolrQuery solrQuery = new SolrQuery(\"author:Kevin\");\n    solrQuery.addField(\"id\");\n    solrQuery.addField(\"title\");\n    solrQuery.addField(\"author\");\n    solrQuery.setSort(\"id\", ORDER.asc);\n    solrQuery.setRows(10);\n\n    \/\/ sends search request and gets the response\n    QueryResponse response = null;\n    try {\n        response = solrClient.query(solrQuery);\n    } catch (SolrServerException | IOException e) {\n        System.err.printf(\"Failed to search articles: %s\", e.getMessage());\n    }\n\n    \/\/ print results to stdout\n    if (response != null) {\n        printResults(response.getResults());\n    }\n}<\/pre>\n<p>Similar to using Java object binding when indexing, we can directly convert search results into domain objects as shown in the method below:<\/p>\n<pre class=\"brush:java\">\/**\n * Querying articles by using SolrQuery and converting results into domain\n * objects with Java object binding.\n *\/\npublic void queryingByUsingSolrQueryAndJavaObjectBinding() {\n    \/\/ constructs a SolrQuery instance\n    final SolrQuery solrQuery = new SolrQuery(\"author:Kevin\");\n    solrQuery.addField(\"id\");\n    solrQuery.addField(\"title\");\n    solrQuery.addField(\"author\");\n    solrQuery.setSort(\"id\", ORDER.asc);\n    solrQuery.setRows(10);\n\n    \/\/ sends search request and gets the response\n    QueryResponse response = null;\n    try {\n        response = solrClient.query(solrQuery);\n    } catch (SolrServerException | IOException e) {\n        System.err.printf(\"Failed to search articles: %s\", e.getMessage());\n    }\n\n    \/\/ converts to domain objects and prints to standard output\n    if (response != null) {\n        List&lt;Article&gt; articles = response.getBeans(Article.class);\n        for (Article article : articles) {\n            System.out.println(article.toString());\n        }\n    }\n}<\/pre>\n<h3 class=\"wp-block-heading\"><a name=\"running\"><\/a>3.6 Running the Example<\/h3>\n<p>Assuming you have already had the Solr instance running locally, we can run the example and verify the results. Download the example source code and run the following command to run the <code>SolrJExample<\/code>:<\/p>\n<pre class=\"brush:bash\">mvn clean compile exec:exec<\/pre>\n<p>In case your Solr instance is not running, you will see the following error messages in the output:<\/p>\n<pre class=\"brush:bash\">======== SolrJ Example ========\nIndexing 12 articles...\n\nFailed to indexing articles: Server refused connection at: http:\/\/localhost:8983\/solr\/jcg_example_core\nFailed to search articles: Server refused connection at: http:\/\/localhost:8983\/solr\/jcg_example_core<\/pre>\n<p>If everything is working fine, you should be able to see the output as below:<\/p>\n<pre class=\"brush:bash\">======== SolrJ Example ========\nIndexing 12 articles...\n12 articles indexed.\nQuerying by using SolrParams...\nFound 6 documents\nid=0221234283, title=Java ArrayList 101, author=Kevin Yang\nid=0553573333, title=Java Array Example, author=Kevin Yang\nid=055357342Y, title=Java StringTokenizer Example, author=Kevin Yang\nid=0553579908, title=Java Remote Method Invocation Example, author=Kevin Yang\nid=0626166238, title=Java Arrays Showcases, author=Kevin Yang\nid=0818231712, title=Apache SolrCloud Example, author=Kevin Yang\nQuerying by using SolrQuery...\nFound 6 documents\nid=0221234283, title=Java ArrayList 101, author=Kevin Yang\nid=0553573333, title=Java Array Example, author=Kevin Yang\nid=055357342Y, title=Java StringTokenizer Example, author=Kevin Yang\nid=0553579908, title=Java Remote Method Invocation Example, author=Kevin Yang\nid=0626166238, title=Java Arrays Showcases, author=Kevin Yang\nid=0818231712, title=Apache SolrCloud Example, author=Kevin Yang\nQuerying by using SolrQuery and Java object binding...\nFound 6 articles\nArticle [id=0221234283, title=Java ArrayList 101, author=Kevin Yang]\nArticle [id=0553573333, title=Java Array Example, author=Kevin Yang]\nArticle [id=055357342Y, title=Java StringTokenizer Example, author=Kevin Yang]\nArticle [id=0553579908, title=Java Remote Method Invocation Example, author=Kevin Yang]\nArticle [id=0626166238, title=Java Arrays Showcases, author=Kevin Yang]\nArticle [id=0818231712, title=Apache SolrCloud Example, author=Kevin Yang]<\/pre>\n<h2 class=\"wp-block-heading\"><a name=\"download\"><\/a>4. Download the Source Code<\/h2>\n<div class=\"download\"><strong>Download<\/strong><br \/>\nYou can download the full source code of this example here: <a href=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2020\/08\/apache-solr-using-solrj.zip\"><strong>Apache Solr in Java: Using Apache SolrJ<\/strong><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this example, we are going to show you how to use Apache SolrJ to index data in Solr and query from Solr. 1. Introduction Apache Solr is a popular open-source search platform built on Apache Lucene. If we take a 30000-foot view, Solr is a web application and HTTP is the fundamental protocol used &hellip;<\/p>\n","protected":false},"author":223,"featured_media":25294,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[949],"tags":[946,1220,46628],"class_list":["post-93159","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-apache-solr","tag-apache-solr","tag-example","tag-solrj"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Apache Solr in Java: Using Apache SolrJ - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"In this example, we are going to show you how to use Apache SolrJ to index data in Solr and query from Solr. 1. Introduction Apache Solr is a popular\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/examples.javacodegeeks.com\/apache-solr-in-java-using-apache-solrj\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Apache Solr in Java: Using Apache SolrJ - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"In this example, we are going to show you how to use Apache SolrJ to index data in Solr and query from Solr. 1. Introduction Apache Solr is a popular\" \/>\n<meta property=\"og:url\" content=\"https:\/\/examples.javacodegeeks.com\/apache-solr-in-java-using-apache-solrj\/\" \/>\n<meta property=\"og:site_name\" content=\"Examples Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2020-08-11T08:00:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/07\/apache-solr-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Kevin Yang\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kevin Yang\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/apache-solr-in-java-using-apache-solrj\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/apache-solr-in-java-using-apache-solrj\/\"},\"author\":{\"name\":\"Kevin Yang\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/3f6ff013b8204dc7f5e6d2660fbc9f8f\"},\"headline\":\"Apache Solr in Java: Using Apache SolrJ\",\"datePublished\":\"2020-08-11T08:00:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/apache-solr-in-java-using-apache-solrj\/\"},\"wordCount\":1093,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/apache-solr-in-java-using-apache-solrj\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/07\/apache-solr-logo.jpg\",\"keywords\":[\"Apache Solr\",\"example\",\"SolrJ\"],\"articleSection\":[\"Apache Solr\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/apache-solr-in-java-using-apache-solrj\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/apache-solr-in-java-using-apache-solrj\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/apache-solr-in-java-using-apache-solrj\/\",\"name\":\"Apache Solr in Java: Using Apache SolrJ - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/apache-solr-in-java-using-apache-solrj\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/apache-solr-in-java-using-apache-solrj\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/07\/apache-solr-logo.jpg\",\"datePublished\":\"2020-08-11T08:00:00+00:00\",\"description\":\"In this example, we are going to show you how to use Apache SolrJ to index data in Solr and query from Solr. 1. Introduction Apache Solr is a popular\",\"breadcrumb\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/apache-solr-in-java-using-apache-solrj\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/apache-solr-in-java-using-apache-solrj\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/apache-solr-in-java-using-apache-solrj\/#primaryimage\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/07\/apache-solr-logo.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/07\/apache-solr-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/apache-solr-in-java-using-apache-solrj\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/examples.javacodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java Development\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Enterprise Java\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/enterprise-java\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Apache Solr\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/enterprise-java\/apache-solr\/\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"Apache Solr in Java: Using Apache SolrJ\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Examples and Code Snippets\",\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/javacodegeeks\",\"https:\/\/x.com\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/3f6ff013b8204dc7f5e6d2660fbc9f8f\",\"name\":\"Kevin Yang\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/2efb55f26af9d8752be93a78f2cdd9b2529df1f087c7b8901b68dbe11b7cf5ee?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/2efb55f26af9d8752be93a78f2cdd9b2529df1f087c7b8901b68dbe11b7cf5ee?s=96&d=mm&r=g\",\"caption\":\"Kevin Yang\"},\"description\":\"A software design and development professional with seventeen years\u2019 experience in the IT industry, especially with Java EE and .NET, I have worked for software companies, scientific research institutes and websites.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/in\/kevinyang2050\/\"],\"url\":\"https:\/\/examples.javacodegeeks.com\/author\/kevin-yang\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Apache Solr in Java: Using Apache SolrJ - Java Code Geeks","description":"In this example, we are going to show you how to use Apache SolrJ to index data in Solr and query from Solr. 1. Introduction Apache Solr is a popular","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/examples.javacodegeeks.com\/apache-solr-in-java-using-apache-solrj\/","og_locale":"en_US","og_type":"article","og_title":"Apache Solr in Java: Using Apache SolrJ - Java Code Geeks","og_description":"In this example, we are going to show you how to use Apache SolrJ to index data in Solr and query from Solr. 1. Introduction Apache Solr is a popular","og_url":"https:\/\/examples.javacodegeeks.com\/apache-solr-in-java-using-apache-solrj\/","og_site_name":"Examples Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2020-08-11T08:00:00+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/07\/apache-solr-logo.jpg","type":"image\/jpeg"}],"author":"Kevin Yang","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Kevin Yang","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/examples.javacodegeeks.com\/apache-solr-in-java-using-apache-solrj\/#article","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/apache-solr-in-java-using-apache-solrj\/"},"author":{"name":"Kevin Yang","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/3f6ff013b8204dc7f5e6d2660fbc9f8f"},"headline":"Apache Solr in Java: Using Apache SolrJ","datePublished":"2020-08-11T08:00:00+00:00","mainEntityOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/apache-solr-in-java-using-apache-solrj\/"},"wordCount":1093,"commentCount":0,"publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/apache-solr-in-java-using-apache-solrj\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/07\/apache-solr-logo.jpg","keywords":["Apache Solr","example","SolrJ"],"articleSection":["Apache Solr"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/examples.javacodegeeks.com\/apache-solr-in-java-using-apache-solrj\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/apache-solr-in-java-using-apache-solrj\/","url":"https:\/\/examples.javacodegeeks.com\/apache-solr-in-java-using-apache-solrj\/","name":"Apache Solr in Java: Using Apache SolrJ - Java Code Geeks","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/apache-solr-in-java-using-apache-solrj\/#primaryimage"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/apache-solr-in-java-using-apache-solrj\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/07\/apache-solr-logo.jpg","datePublished":"2020-08-11T08:00:00+00:00","description":"In this example, we are going to show you how to use Apache SolrJ to index data in Solr and query from Solr. 1. Introduction Apache Solr is a popular","breadcrumb":{"@id":"https:\/\/examples.javacodegeeks.com\/apache-solr-in-java-using-apache-solrj\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/examples.javacodegeeks.com\/apache-solr-in-java-using-apache-solrj\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/apache-solr-in-java-using-apache-solrj\/#primaryimage","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/07\/apache-solr-logo.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/07\/apache-solr-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/examples.javacodegeeks.com\/apache-solr-in-java-using-apache-solrj\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/examples.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java Development","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/"},{"@type":"ListItem","position":3,"name":"Enterprise Java","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/enterprise-java\/"},{"@type":"ListItem","position":4,"name":"Apache Solr","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/enterprise-java\/apache-solr\/"},{"@type":"ListItem","position":5,"name":"Apache Solr in Java: Using Apache SolrJ"}]},{"@type":"WebSite","@id":"https:\/\/examples.javacodegeeks.com\/#website","url":"https:\/\/examples.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/examples.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/examples.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/3f6ff013b8204dc7f5e6d2660fbc9f8f","name":"Kevin Yang","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/2efb55f26af9d8752be93a78f2cdd9b2529df1f087c7b8901b68dbe11b7cf5ee?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2efb55f26af9d8752be93a78f2cdd9b2529df1f087c7b8901b68dbe11b7cf5ee?s=96&d=mm&r=g","caption":"Kevin Yang"},"description":"A software design and development professional with seventeen years\u2019 experience in the IT industry, especially with Java EE and .NET, I have worked for software companies, scientific research institutes and websites.","sameAs":["https:\/\/www.linkedin.com\/in\/kevinyang2050\/"],"url":"https:\/\/examples.javacodegeeks.com\/author\/kevin-yang\/"}]}},"_links":{"self":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/93159","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/users\/223"}],"replies":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=93159"}],"version-history":[{"count":0,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/93159\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media\/25294"}],"wp:attachment":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=93159"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=93159"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=93159"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}