Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Mastering Apache Solr 7.x

You're reading from  Mastering Apache Solr 7.x

Product type Book
Published in Feb 2018
Publisher Packt
ISBN-13 9781788837385
Pages 308 pages
Edition 1st Edition
Languages
Authors (3):
Sandeep Nair Sandeep Nair
Profile icon Sandeep Nair
Chintan Mehta Chintan Mehta
Profile icon Chintan Mehta
Dharmesh Vasoya Dharmesh Vasoya
Profile icon Dharmesh Vasoya
View More author details

Chapter 9. Client APIs – An Overview

In the previous chapter, we saw the various steps to be configured for production readiness. We also explored the fine-tuning configuration needs to be considered during production setup for better performance. We learned how to secure a Solr, take backups, and configure logs. Then we got an overview of SolrCloud. Now we have a complete configured Solr with all the required configurations to meet any search request. In this chapter, we will learn how Solr can be used with a web application and how to call APIs in different languages. We'll have an overview of various Client APIs supported by Solr.

Client API overview


Solr comes with a bunch of REST APIs, which exposes its features such as query, index, delete, commit, and optimize; it also allows a web application to connect with Solr and perform any operation by calling these APIs. Solr has taken care of these REST APIs such that any web application developed in any programming language can connect to them. A REST API is developed based on the HTTP protocol; so a web application developed in any programming language, such as Java, .NET, Python, and Ruby, can easily connect to and call this API to perform various Solr operations. Using this API, a web application asks Solr to perform some operations, such as querying and indexing. Solr performs those operations and provides a response to the application. Solr also supports various response formats based on programming languages such as Java, JavaScript/JSON, Python, Ruby, PHP, and many more (we have seen this in Chapter 6, Advanced Queries – Part I). So it becomes very easy for any...

JavaScript Client API


JavaScript client is very easy, simple, and straightforward. We don't need to create any client to connect to Solr. Also, no packages need to be installed for the JavaScript client. JavaScript sends the request to Solr using XMLHttpRequest. Solr processes the request and returns the response in JSON format, which can easily be parsed in JavaScript. We don't need to configure the wt response parameter as Solr, by default, returns the response in JSON format.

Example: Configure hostURL= http://localhost:8983/solr/techproducts/select in JavaScript as follows:

<html>
<head>
<title>Solr Javascript API Example</title>
<script language="Javascript">
//main function called when clicking on search button
function search() {
  //Solr search url
  var hostURL='http://localhost:8983/solr/techproducts/select';
    var xmlHttpReq = false;
    var xmlHttpClient = this;

  // Mozilla/Safari
    if (window.XMLHttpRequest) {
        xmlHttpClient.xmlHttpReq...

SolrJ Client API


SolrJ is built in Java technologies to connect with Solr from a Java application over HTTP.

Solr and SolrJ both are built-in Java technologies, so communication between them is easy and straightforward. While uploading a document, Solr needs all documents in XML or JSON format. SolrJ uses an internal binary protocol by default, called JavaBin. Normally, the client application sends an update request using HTTP POST with JSON or XML format, but the SolrJ client can send the update request as JSON, XML, or Solr's internal binary JavaBin format. The JavaBin protocol is more efficient than XML or JSON.

Apart from normal communication to Solr, SolrJ also supports load balancing across Solr nodes, automatically discovers locations of Solr servers in a SolrCloud mode, and easily handles bulk indexing for large amounts of data. It is also possible to embed Solr within a Java application and connect to it directly without establishing an HTTP connection to the server.

To create a SolrJ...

Ruby Client API


Like JavaScript and SolrJ, Ruby can also connect with Solr using the Solr API and performs various operations such as search, indexing, and removal over the HTTP protocol. Solr also provides sufficient API support for the Ruby programming language. So, the application that builds on a Ruby platform can also take advantage of Solr. Solr reverts a query response in Ruby format, which can be interpreted easily in Ruby programming. 

For Ruby applications, to communicate with Solr, rsolr is an extensive library. It provides all the functionalities needed to meet your expectations. To install the rsolr dependency, please refer to http://www.rubydoc.info/gems/rsolr. To install rsolr, use this command:

gem install rsolr

Now let's search using rsolr for the query q=ipod:

require 'rsolr'

solr = RSolr.connect :url => 'http://localhost:8983/solr/techproducts'

response = solr.get 'select', :params => {:q => 'ipod',:fl=>'id,name',:wt=>:ruby}

puts response

Response:

{"responseHeader...

Python Client API


Solr also comes with APIs that can be connected through applications developed in the Python programming language. Solr supports responses in Python format, which can be easily interpreted in Python programming. As we have seen before for JavaScript, SolrJ, and Ruby, Python also provides all the required packages to perform search, add, delete and so on.

Let's start with search. Here is the simple search configuration for searching a query q=ipod in techproducts:

import urllib.request

connection = urllib.request.urlopen('http://localhost:8983/solr/techproducts/select?q=ipod&fl=id,name&wt=python')

response = eval(connection.read())

print(response)

Response:

{'responseHeader': {'status': 0, 'QTime': 51, 'params': {'q': 'ipod', 'fl': 'id,name', 'wt': 'python'}}, 'response': {'numFound': 3, 'start': 0, 'docs': [{'id': 'IW-02', 'name': 'iPod & iPod Mini USB 2.0 Cable'}, {'id': 'F8V7067-APL-KIT', 'name': 'Belkin Mobile Power Cord for iPod w/ Dock'}, {'id': 'MA147LL...

Summary


In this chapter, we got a basic overview of various JavaScript, SolrJ, Ruby, and Python supports for Solr API. We explored the basic configurations required to connect with the Solr API for each one of them. Then we designed basic programs for searching, adding, adding in bulk, deleting, and deleting in bulk. We executed them and analyzed the response for each of them.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Mastering Apache Solr 7.x
Published in: Feb 2018 Publisher: Packt ISBN-13: 9781788837385
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}