[ElasticSearch] 윈도우 설치 및 기본 동작 이해

서버|2017. 8. 31. 09:54

ElasticSearch는 오픈 소스이고 REST 기반의 실시간 검색 및 분석 엔진이다.

Java로 작성되어졌으며 아파치 Lucene 기반으로 검색 기능 및 REST-Based API (JSON Over HTTP Protocol)를 지원한다.

1
2
3
4
GET : 조회
POST : 저장
PUT : 수정
DELETE : 삭제
cs


https://www.elastic.co/downloads/elasticsearch 페이지에서 윈도우 버전의 zip 파일을 다운로드 받는다.

다운로드가 완료되면 압출 풀고 bin 폴더로 이동하여 elasticsearch.bat 파일을 실행한다.

설치 과정은 굉장이 쉽다. 윈도우 버전 뿐만 아니라 다른 OS 버전도 설치가 간단하다.


ElasticSearch가 실행되면 9200, 9300 포트가 오픈된다.

9200 포트는 HTTP로 바인딩 되고 9300 포트는 TCP로 바인딩 된다.


http://localhost:9200로 접속해보자.

클러스터 이름과 버전 정보를 확인할 수 있다.

cluster_name은 config/elasticsearch.yml 에서 변경 가능하다.


설치가 끝났다. 

이제 데이터를 저장, 조회, 수정, 삭제 하는 방법에 대해서 알아보자.

그 전에 ElasticSearch 저장 구조에 대해서 이해하고 있어야 한다.


ElasticSearch 저장 구조에는 Index, Type, Document, Field, Mapping 용어들이 존재한다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Index란?
RDB의 데이타베이스와 유사하다.
 
Type이란?
RDB의 테이블과 유사하다.
 
Document란?
테이블의 Row와 유사하다.
JSON 문서로 되어 있다. (key, value)
 
Field란?
엘라스틱 서치의 문서는 JSON이다. JSON의 프로퍼티를 엘라스틱 서치에서는 필드라고 부른다.
RDB 테이블의 컬럼과 유사하다.
 
Mapping이란?
RDB의 스키마와 유사하다.
Mapping은 이해하기 어려우니 아래 이미지 한장 첨부한다.
http://localhost:9200/nklee/phone/1 POST 요청과 함께 아래 JSON데이터를 전송하면 Elasticsearch에서 mapping을 자동 생성해 준다.
{
  "number": "010-1111-1111",
  "author":"nklee"
}






cs


[Mapping 구조]


저장 구조에 대해서 그림으로 표현하면 다음과 같을 것이다.

이미지 : http://dev.classmethod.jp/cloud/aws/use-elasticsearch-4-data-structure/


ElasticSearch REST API URL 포멧은 다음과 같다.

http://{Node:PortNumber}/{Index}/{Type}
cs

Index는 소문자여야 한다.

Type은 Index와 마찬가지로 소문자를 추천한다.

ex) http://localhost:9200/nklee/phone


이제 기본 동작을 이해하기 위해서 테스트를 해보자.

테스트를 위해 크롬 확장 프로그램인 Postman을 사용하였다.



저장하기



위와 같이 저장 요청을 전송하게 되면 다음과 같이 결과값이 리턴된다.

REST API URL 포멧에서 본 것처럼 nklee 인덱스의 phone 타입에 1이라는 아이디로 저장했다는 의미이다.

{
    "_index": "nklee",
    "_type": "phone",
    "_id": "1",
    "_version": 1,
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "created": true
}
cs



조회하기



위에서 저장한 데이터를 조회하면 다음과 같이 JSON 결과값이 출력된다.

{
    "_index": "nklee",
    "_type": "phone",
    "_id": "1",
    "_version": 1,
    "found": true,
    "_source": {
        "number": "010-1111-1111",
        "author": "nklee"
    }
}
cs



수정하기



이미 저장되어 있는 데이터의 author 값을 변경해서 요청해 보자.

{
    "_index": "nklee",
    "_type": "phone",
    "_id": "1",
    "_version": 3,
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "created": false
}
cs



모두 검색하기



저장되어 있는 데이터 모두가 출력된다.

{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 1,
        "hits": [
            {
                "_index": "nklee",
                "_type": "phone",
                "_id": "2",
                "_score": 1,
                "_source": {
                    "number": "010-1111-1111",
                    "author": "nklee"
                }
            },
            {
                "_index": "nklee",
                "_type": "phone",
                "_id": "1",
                "_score": 1,
                "_source": {
                    "number": "010-1111-1111",
                    "author": "nklee22"
                }
            }
        ]
    }
}
cs



조건 검색하기



q 파라미터를 이용하여 조건 검색이 가능하다.

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 0.33912507,
        "hits": [
            {
                "_index": "nklee",
                "_type": "phone",
                "_id": "1",
                "_score": 0.33912507,
                "_source": {
                    "number": "010-1111-1111",
                    "author": "nklee22"
                }
            }
        ]
    }
}
cs



삭제하기



삭제해 보자.

{
    "found": false,
    "_index": "nklee",
    "_type": "phone",
    "_id": "2",
    "_version": 3,
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    }
}
cs


'서버' 카테고리의 다른 글

[Elasticsearch] 모니터링 툴  (0) 2017.11.30
[Elasticsearch] 클러스터 구성  (2) 2017.11.30
[Elasticsearch] 용어 정리  (0) 2017.11.29
Apache2 설치 (Ubuntu 16.04)  (0) 2017.09.15
EFK Stack 구축 using Docker compose  (0) 2017.08.24
Google App Engine 환경에 Spring Boot 배포하기  (0) 2017.08.09
kubernetes 정리  (2) 2017.01.12
Kubernetes 설치  (6) 2017.01.12

댓글()