2.X/1. Getting Started

1-01-02. Talking to Elasticsearch

drscg 2017. 10. 1. 12:24

How you talk to Elasticsearch depends on whether you are using Java.

Elasticsearch와 통신하는 방법은 Java 사용 여부에 달려 있다.

Java APIedit

If you are using Java, Elasticsearch comes with two built-in clients that you can use in your code:

만약 Java 를 사용한다면, Elasticsearch는, 코드에서 사용할 수 있는, 두 종류의 기본 클라이언트를 제공한다.

Node client

The node client joins a local cluster as a non data node. In other words, it doesn’t hold any data itself, but it knows what data lives on which node in the cluster, and can forward requests directly to the correct node.

node client는 데이터 node가 아닌 node 로 local cluster와 결합한다. 즉, 자체적으로 데이터를 가질 수 없다. 그러나, cluster내의 어느 node에 데이터가 있는지 알고 있고, 적절한 node로 직접 request를 보낼 수 있다.

Transport client

The lighter-weight transport client can be used to send requests to a remote cluster. It doesn’t join the cluster itself, but simply forwards requests to a node in the cluster.

transport client는 원격 cluster에 request를 보내는데 사용될 수 있다. cluster 자체와 결합하지 않고, 단순히 cluster내의 node에 request를 보낸다.

Both Java clients talk to the cluster over port 9300, using the native Elasticsearch transport protocol. The nodes in the cluster also communicate with each other over port 9300. If this port is not open, your nodes will not be able to form a cluster.

위의 두 Java 클라이언트는 Elasticsearch 고유의 transport protocol을 사용하여, port 9300 을 통해 cluster와 통신한다. cluster내의 node들은 port 9300을 통해, 다른 node들과 통신한다. 이 port가 열려있지 않으면, cluster를 형성할 수 없다.

Tip

The Java client must be from the same major version of Elasticsearch as the nodes; otherwise, they may not be able to understand each other.

Java 클라이언트는 반드시 node의 Elasticsearch와 동일한 major 버전을 사용해야 한다. 그렇지 않으면, 서로 통신할 수 없다.

More information about the Java clients can be found in Elasticsearch Clients.

Java 클라이언트에 대한 좀 더 자세한 정보는 Elasticsearch Clients를 참고하자.

RESTful API with JSON over HTTPedit

All other languages can communicate with Elasticsearch over port 9200 using a RESTful API, accessible with your favorite web client. In fact, as you have seen, you can even talk to Elasticsearch from the command line by using the curl command.

Java가 아닌 모든 언어는 가장 익숙한 Web 클라이언트에서, RESTful API를 사용하여, port 9200 을 통해, Elasticsearch와 통신할 수 있다. 위에서 살펴 보았듯이, 심지어 command line에서조차 curl 명령어를 이용해 Elasticsearch와 통신할 수 있다.

Note

Elasticsearch provides official clients for several languages—Groovy, JavaScript, .NET, PHP, Perl, Python, and Ruby—and there are numerous community-provided clients and integrations, all of which can be found in Elasticsearch Clients.

Elasticsearch는 다수의 언어(Groovy, Javascript, .NET, PHP, Perl, Python, Ruby 등)를 위하여, 공식적인 클라이언트를 제공한다. 그리고, Elasticsearch Clients에서 확인할 수 있는, 수많은 Community에서 제공하는 클라이언트와 통합요소가 있다.

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

Elasticsearch에 대한 request는 HTTP request와 동일한 형태로 구성되어 있다.

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

The parts marked with < > above are:

위에서 < > 로 표시된 부분은:

VERB

The appropriate HTTP method or verbGETPOSTPUTHEAD, or DELETE.

적절한 HTTP method 나 verbGETPOSTPUTHEAD, or DELETE.

PROTOCOL

Either http or https (if you have an https proxy in front of Elasticsearch.)

http 나 https (Elasticsearch 앞에 https proxy가 있으면)

HOST

The hostname of any node in your Elasticsearch cluster, or localhost for a node on your local machine.

Elsticsearch cluster에 있는 특정 node의 hostname, local machine에 있는 node는 localhost

PORT

The port running the Elasticsearch HTTP service, which defaults to 9200.

Elasticsearch HTTP Service가 실행중인 port, 기본은 9200.

PATH

API Endpoint (for example _count will return the number of documents in the cluster). Path may contain multiple components, such as _cluster/stats or _nodes/stats/jvm

API로 끝나야 함(예를 들어, _count 는 cluster에 있는 document의 수를 반환할 것이다). Path는 _cluster/stats 나 _node/stats/jvm 같은 다수의 component를 가질 수 있다.

QUERY_STRING

Any optional query-string parameters (for example ?pretty will pretty-print the JSON response to make it easier to read.)

선택적으로 사용하는 query-string 매개변수 (예를 들어, ?pretty 는 JSON response를 읽기에 편하도록 출력해준다.)

BODY

A JSON-encoded request body (if the request needs one.)

JSON으로 encode된 request body (필요한 경우에만.)

For instance, to count the number of documents in the cluster, we could use this:

예를 들면, cluster에 있는 document의 수를 검색하기 위해서는, 아래처럼 사용한다.

curl -XGET 'http://localhost:9200/_count?pretty' -d '
{
    "query": {
        "match_all": {}
    }
}
'

Elasticsearch returns an HTTP status code like 200 OK and (except for HEAD requests) a JSON-encoded response body. The preceding curl request would respond with a JSON body like the following:

Elasticsearch는 200 OK 같은 HTTP status code와, (HEAD request를 제외하고) JSON으로 encode된 response body를 반환한다. 위의 curl request는 아래와 같은 JSON body를 response한다.

{
    "count" : 0,
    "_shards" : {
        "total" : 5,
        "successful" : 5,
        "failed" : 0
    }
}

We don’t see the HTTP headers in the response because we didn’t ask curl to display them. To see the headers, use the curl command with the -i switch:

HTTP header를 보여주라고 curl 에 request하지 않았기 때문에, response에서 HTTP header를 볼 수 없다. header를 보기 위해서는, curl 에 –i 매개변수를 사용해야 한다.

curl -i -XGET 'localhost:9200/'

For the rest of the book, we will show these curl examples using a shorthand format that leaves out all the bits that are the same in every request, like the hostname and port, and the curl command itself. Instead of showing a full request like

앞으로 이 책에서는, 모든 request에서 공통적인 부분(hostname, port, curl command 자체 등)을 제거한, 생략된 형태의 curl 예제로 표시할 것이다. 아래처럼 전체 request를 보여주는 대신,

curl -XGET 'localhost:9200/_count?pretty' -d '
{
    "query": {
        "match_all": {}
    }
}'

we will show it in this shorthand format:

아래처럼 생략된 형태로 표시할 것이다

GET /_count
{
    "query": {
        "match_all": {}
    }
}

In fact, this is the same format that is used by the Sense console. If you’re viewing the online version of this book, you can open and run this code example in Sense by clicking the View in Sense link above.

사실, 위 예제는 Sense console 에서 사용되는 형식과 같다. 이 책의 online version을 보고 있다면, 위의 View in Sense link를 클릭하여, sense에서 예제 code를 실행할 수 있다.


'2.X > 1. Getting Started' 카테고리의 다른 글

1-01. You Know, for Search…  (0) 2017.10.01
1-01-01. Installing and Running Elasticsearch  (0) 2017.10.01
1-01-03. Document Oriented  (0) 2017.10.01
1-01-04. Finding Your Feet  (0) 2017.10.01
1-01-05. Indexing Employee Documents  (0) 2017.10.01