penguin-elasticsearch



penguin-elasticsearch

0 0


penguin-elasticsearch


On Github deekim / penguin-elasticsearch

Introduction to Elasticsearch

Talking to elasticsearch

All operations are completely asynchronous in nature.

Meaning they either accept a listener, or return a future.

  • Java API
    • Node client
    • Transport client
  • RESTful API with JSON over HTTP

Java API

Using the Java client, one can:

Perform standard index, get, delete and search operations on an existing cluster Perform administrative tasks on a running cluster Start full nodes when you want to run Elasticsearch embedded in your own application or when you want to launch unit or integration tests

Node Client

import static org.elasticsearch.node.NodeBuilder.*;

// on startup

Node node = nodeBuilder().node();
Client client = node.client();

// on shutdown

node.close();

When you start a Node, it joins an elasticsearch cluster.

Node node = nodeBuilder().clusterName("cluster.name").node();
Client client = node.client();

Downsides of using a Node Client

Frequently starting and stopping one or more node clients creates unnecessary noise across the cluster. Embedded node client will respond to outside requests, just like any other client. You almost always want to disable HTTP for an embedded node client.

Transport Client

The transport client connects remotely to an Elasticsearch cluster using the transport module.

It does not join the cluster. Instead, it gets one or more initial transport addresses and communicates with them in round robin fashion on each action.

// on startup

Client client = new TransportClient()
        .addTransportAddress(
                    new InetSocketTransportAddress("host1", 9300))
        .addTransportAddress(
                    new InetSocketTransportAddress("host2", 9300));

// on shutdown

client.close();

RESTful API with JSON over HTTP

A request to Elasticsearch consists of the same parts as any HTTP request:

curl -X<VERB> '<PROTOCOL>://<HOST>:<PORT>/<PATH>?<QUERY_STRING>' \
-d '<BODY>'

VERB

HTTP method or verb GET, POST, PUT, HEAD, or DELETE

PROTOCOL

HOST

PORT

PATH

QUERY_STRING

BODY