ElasticSearch 시작하기

By | 2021년 9월 30일
Table of Contents

ElasticSearch 시작하기

인덱스

인덱스 생성

PUT /restaurant_info?pretty
{
  "settings" : {
    "number_of_shards" : 1,
    "number_of_replicas" : 0,
    "index":{
      "max_ngram_diff": 50,
      "analysis":{
        "analyzer":{
          "my_ngram_analyzer": {
            "tokenizer": "my_ngram_tokenizer"
          }
        },
        "tokenizer": {
          "my_ngram_tokenizer": {
            "type": "ngram",
            "min_gram": "2",
            "max_gram": "10"
          }
        }
      }
    }
  },
  "mappings" : {
    "properties" : {
      "restaurant_name" : {
        "type" : "text",
        "analyzer": "my_ngram_analyzer"
      },
      "start_date" : {
        "type" : "date",
        "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||yyyyMMdd||epoch_millis"
      },
      "tel" : { "type" : "keyword" },
      "address" : {
        "type" : "text",
        "analyzer": "my_ngram_analyzer"
      },
      "road_address" : {
        "type" : "text",
        "analyzer": "my_ngram_analyzer"
      },
      "coordinate" : { "type" : "geo_point" },
      "longitude" : { "type" : "float" },
      "latitude" : { "type" : "float" },
      "modify_time" : {
        "type" : "date",
        "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||yyyyMMdd||epoch_millis"
      },
      "insert_time" : {
        "type" : "date",
        "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||yyyyMMdd||epoch_millis"
      }
    }
  }
}

Type 종류

참조

  • String
타입 설명
text full text 검색이 가능합니다. 형태소 분석 등 인덱스 작업을 합니다.
keyword 형태소 분석없이 검색어와 일치하는 데이타만 검색됩니다.
  • Number
타입 설명
byte 8비트 정수 (-128 ~ 127)
short 16비트 정수 (-32768 ~ 32767)
integer 32비트 정수
long 64비트 정수
float 32비트 실수
double 64비트 실수
half_float 16비트 실수
scaled_float 통화 (예: $19.99) 저장
  • Date
타입 설명
date ISO8601 형식으로 입력해야 합니다.
  • List
타입 설명
list
  • Geo Point
타입 설명
geo_point 좌표를 입력할 수 있습니다.

문서 입력

PUT /restaurant_info/_doc/2
{
  "restaurant_name": "갈비명가아라네",
  "start_date": "20140612",
  "tel": "062 369 1582",
  "address": "광주광역시 남구 월산동 1048-182번지 (1층)",
  "road_address": "광주광역시 남구 군분로 26-1, 1층 (월산동)",
  "coordinate": {
    "lat": 181905.028328,
    "lon": 190363.418899
  },
  "longitude": "190363.418899",
  "latitude": "181905.028328",
  "modify_time": "2021-09-27 15:08:42",
  "insert_time": "2021-09-27 15:08:42"
}

문서 검색

_search 를 이용해 검색할 수 있다.

GET /restaurant_info/_search
{
  "size" : 100,
  "_source" : false,
  "fields" : [
    {
      "field" : "tags"
    },
    {
      "field" : "updated_at",
      "format" : "strict_date_optional_time_nanos"
    }
  ],
  "sort" : [
    {
      "updated_at" : {
        "order" : "desc",
        "missing" : "_first",
        "unmapped_type" : "date"
      }
    }
  ]
}

SQL 이 익숙하다면, 아래와 같이 입력해서 ES 쿼리로 변환할 수 있다.

GET /_sql/translate
{
  "query": "SELECT * FROM restaurant_info ORDER BY updated_at DESC LIMIT 100"
}

문서 삭제

POST /restaurant_info/_delete_by_query
{
    "query": {
        "term" : { "itemId" : "979" }
    }
}

인덱스 삭제

DELETE restaurant_info

답글 남기기