This recipe shows some samples of using the HTTP protocol.
You need a working Elasticsearch cluster. Using the default configuration, Elasticsearch enables the 9200
port on your server to communicate in HTTP.
The standard RESTful protocol is easy to integrate because it is the lingua franca for the Web and can be used by every programming language.
Now, I'll show how easy it is to fetch the Elasticsearch greeting API on a running server at 9200
port using several programming languages.
In Bash or Windows prompt, 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; ... 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(" ")
For every language sample, the response will be the same:
{ "name" : "elasticsearch", "cluster_name" : "elasticsearch", "cluster_uuid" : "rbCPXgcwSM6CjnX8u3oRMA", "version" : { "number" : "5.1.1", "build_hash" : "5395e21", "build_date" : "2016-12-06T12:36:15.409Z", "build_snapshot" : false, "lucene_version" : "6.3.0" }, "tagline" : "You Know, for Search" }
Every client creates a connection to the server index /
and fetches the answer. The answer is a JSON object.
You can call Elasticsearch server from any programming language that you like. The main advantages of this protocol are:
curl
In this book, a lot of examples are done calling the HTTP API via the command-line curl
program. This approach is very fast and allows you to test functionalities very quickly.
3.129.194.106