Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
ElasticSearch Cookbook

You're reading from   ElasticSearch Cookbook As a user of ElasticSearch in your web applications you'll already know what a powerful technology it is, and with this book you can take it to new heights with a whole range of enhanced solutions from plugins to scripting.

Arrow left icon
Product type Paperback
Published in Dec 2013
Publisher Packt
ISBN-13 9781782166627
Length 422 pages
Edition 1st 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 FREE CHAPTER 2. Downloading and Setting Up ElasticSearch 3. Managing Mapping 4. Standard Operations 5. Search, Queries, and Filters 6. Facets 7. Scripting 8. Rivers 9. Cluster and Nodes Monitoring 10. Java Integration 11. Python Integration 12. Plugin Development Index

Using the HTTP protocol

This recipe shows a sample of using the HTTP protocol.

Getting ready

You need a working ElasticSearch cluster. Using default configuration the 9200 port is open in your server to communicate with.

How to do it…

The standard RESTful protocol, it's easy to integrate.

Now, I'll show how to easily fetch the ElasticSearch greeting API on a running server at 9200 port using several ways and programming languages.

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

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

In BASH:

curl –XGET http://127.0.0.1:9200

In Python:

  import urllib
  result = urllib.open("http://127.0.0.1:9200")

In Java:

import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.URL;

…
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:

scala.io.Source.fromURL("http://127.0.0.1:9200","utf-8").getLines.mkString("\n")

How it works…

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

The main advantages of this protocol are as follows:

  • Portability: It uses web standards so it can be integrated in different languages (Erlang, JavaScript, Python, Ruby, and so on) or called from command-line applications such as curl.
  • Durability: The REST APIs don't often change. They don't break for minor release changes as Native protocol does.
  • Simple to use: It speaks JSON to JSON.
  • More supported than other protocols: Every plugin typically supports a REST endpoint on HTTP.

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

There's more…

Every language provides drivers to best integrate ElasticSearch or RESTful web services.

ElasticSearch community provides official drivers that support the various services.

You have been reading a chapter from
ElasticSearch Cookbook
Published in: Dec 2013
Publisher: Packt
ISBN-13: 9781782166627
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 €18.99/month. Cancel anytime