Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
ElasticSearch Cookbook - Second Edition

You're reading from   ElasticSearch Cookbook - Second Edition Over 130 advanced recipes to search, analyze, deploy, manage, and monitor data effectively with ElasticSearch

Arrow left icon
Product type Paperback
Published in Jan 2015
Publisher
ISBN-13 9781783554836
Length 472 pages
Edition 2nd Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Alberto Paro Alberto Paro
Author Profile Icon Alberto Paro
Alberto Paro
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Getting Started 2. Downloading and Setting Up FREE CHAPTER 3. Managing Mapping 4. Basic Operations 5. Search, Queries, and Filters 6. Aggregations 7. Scripting 8. Rivers 9. Cluster and Node Monitoring 10. Java Integration 11. Python Integration 12. Plugin Development Index

Using the HTTP protocol

This recipe shows us the usage of the HTTP protocol with an example.

Getting ready

You need a working instance of the ElasticSearch cluster. Using default configuration, ElasticSearch enables port number 9200 on your server to communicate in HTTP.

How to do it...

The standard RESTful protocol is easy to integrate.

We will see how easy it is to fetch the ElasticSearch greeting API on a running server on port 9200 using different programming languages:

  • In BASH, the request will be:
    curl –XGET http://127.0.0.1:9200
    
  • In Python, the request will be:
    import urllib
    result = urllib.open("http://127.0.0.1:9200")
  • In Java, the request will be:
    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.URL;
    … truncated…
      try{  // get URL content
        URL url = new URL("http://127.0.0.1:9200");
        URLConnection conn = url.openConnection();// open the stream and put it into BufferedReader
        BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    
        String inputLine;
        while ((inputLine = br.readLine()) != null){
        System.out.println(inputLine);
      }
      br.close();
      System.out.println("Done");
    }
      Catch (MalformedURLException e) {
      e.printStackTrace();
    }
    catch (IOException e){
      e.printStackTrace();
    }
  • In Scala, the request will be:
    scala.io.Source.fromURL("http://127.0.0.1:9200","utf-8").getLines.mkString("\n")

For every language sample, the response will be the same:

{
  "ok" : true,
  "status" : 200,
  "name" : "Payge, Reeva",
  "version" : {
    "number" : "1.4.0",
    "snapshot_build" : false
  },
  "tagline" : "You Know, for Search"
}

How it works...

Every client creates a connection to the server index / and fetches the answer. The answer is a valid JSON object. You can invoke the ElasticSearch server from any language that you like.

The main advantages of this protocol are:

  • Portability: This uses Web standards so that it can be integrated in different languages (Erlang, JavaScript, Python, Ruby, and so on) or called via a command-line application such as cURL.
  • Durability: The REST APIs don't change often. They don't break for minor release changes as native protocol does.
  • Simple to use: This has JSON-to-JSON interconnectivity.
  • Good support: This has much more support than other protocols. Every plugin typically supports a REST endpoint on HTTP.
  • Easy cluster scaling: You can simply put your cluster nodes behind an HTTP load balancer to balance the calls such as HAProxy or NGinx.

In this book, a lot of the examples are done by calling the HTTP API via the command-line cURL program. This approach is very fast and allows you to test functionalities very quickly.

There's more...

Every language provides drivers for best integration with ElasticSearch or RESTful web services.

The ElasticSearch community provides official drivers that support the most used programming languages.

lock icon The rest of the chapter is locked
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 $19.99/month. Cancel anytime
Banner background image