Skip to content

Instantly share code, notes, and snippets.

@occidere
Created March 12, 2019 00:04
Show Gist options
  • Save occidere/38a7bbf1d1775533e7a6cf26b15b4bba to your computer and use it in GitHub Desktop.
Save occidere/38a7bbf1d1775533e7a6cf26b15b4bba to your computer and use it in GitHub Desktop.
################### Study #########################
# 01. 인덱스 생성
PUT shopping_products-190303
{
"settings": {
"number_of_shards": 5,
"number_of_replicas": 1
},
"mappings": {
"_doc": {
"properties": {
"product_id": {
"type": "keyword",
"store": true
},
"name": {
"type": "text",
"store": true
}
}
}
}
}
GET shopping_products-190303
# 02. 인덱스 삭제
DELETE shopping_products-190303
# elasticsearch.yml 에 추가 안하고도 동적으로 처리 가능
PUT _cluster/settings
{
"persistent": {
"action.destructive_requires_name": false
}
}
# 03. 인덱스 open & close
POST shopping_products-190303/_close
POST shopping_products-190303/_open
# 04. 인덱스에 mappings 입력
GET shopping_products-190303
PUT shopping_products-190303/_doc/_mapping?ignore_unavailable=true
{
"_doc": {
"properties": {
"product_id": {
"type": "keyword",
"store": true
},
"name": {
"type": "text", // keyword
"store": true
},
"price": {
"type": "long",
"store": true
}
}
}
}
GET shopping_products-190303
# 05. 매핑 조회
GET shopping_products-190303/_mapping
GET _mapping
# 06. Reindex
PUT shopping_products-190303/_doc/0
{
"name": "노트북",
"product_id": "1234",
"price": 1500000
}
GET shopping_products*/_search
POST _reindex?wait_for_completion=false&refresh
{
"source": {
"index": "shopping_products-190303"
},
"dest": {
"index": "new_shopping_products-190303",
"version_type": "internal"
},
"script": {
"source": "ctx._source.productName = ctx._source['name']; ctx._source.remove(\"name\");",
"lang": "painless"
}
}
GET _tasks/I2mpTyAkSYm0Jr5bc1cCCg:8026603
GET new_shopping_products-190303/_search
DELETE new_shopping_products-190303
# 11. 인덱스 및 타입 존재 여부 확인
HEAD not_exist_index // 없음
HEAD shopping_products-190303 // 있음
# 12. 인덱스 settings
PUT shopping_products-190303/_settings
{
"settings": {
"index.number_of_replicas": 1, // replica 개수
"index.refresh_interval": "-1", // 인덱스 refresh 주기
"index.routing.allocation.require.box_type": "hot", // hot/warm 아키텍쳐에서 어떤 타입의 노드에 색인할 것인지 지정
"index.blocks.read_only": null, // 읽기 전용 설정 & 디스크 full 로 인해 읽기전용이 된 경우 이 값을 null 로 바꿔 해제
"index.routing.allocation.exclude._name": "*warm*", // 특정 노드에 색인을 안 하고 싶을 때 지정
"index.max_result_window": 50000, // 한번에 가져올 수 있는 문서 수 설정 (기본: 10000)
"index.auto_expand_replicas": "0-5" // 지정한 범위 내에서 replica 를 자동으로 조정
}
}
# 13. alias
## alias 확인
GET _cat/aliases?v
GET _cat/aliases?v&alias=products
## alias 추가
POST _aliases
{
"actions": [
{
"add": {
"indices": [
"new_shopping_products-*",
"shopping_products-190303"
],
"alias": "products"
}
}
]
}
## 결과 확인
GET shopping_products-190303/_count
GET new_shopping_products-190303/_count
GET products/_count
# alias 삭제 (데이터 말고 alias 만 삭제)
DELETE */_alias/products
POST _aliases
{
"actions": [
{
"remove": {
"index": "*",
"aliases" : [
"products"
]
}
}
]
}
# 14. 인덱스 rollover
# 1개의 인덱스를 alias 에 연결
# POST <shopping_logs-000001>
PUT shopping_logs-000001
{
"aliases": {
"shopping_logs": {}
}
}
DELETE */_alias/shopping_logs
# rollover 생성 및 적용
POST shopping_logs/_rollover
{
"conditions": {
"max_age": "10s"
}
}
POST shopping_logs/_doc/1
{
"id": "1"
}
DELETE shopping_logs-000001
# 30 초마다 갱신되는 것 확인
GET shopping_logs/_search
# 15. Document 색인
## ID 지정 없이 색인
POST test-190310/_doc
{
"name": "test"
}
## ID 지정하여 색인
POST test-190310/_doc/0
{
"name": "test0"
}
## operation type 으로 제어
POST test-190310/_doc/0?op_type=create
{
"name": "test0"
}
POST test-190310/_doc/0?op_type=index
{
"name": "test1"
}
GET test-190310/_doc/0
## version 으로 제어
POST test-190310/_doc/0?version=1
{
"name": "test2"
}
# 16. Document 검색
## 기본 검색 방법
GET shopping_products-190303/_doc/0
## 특정 필드만 반환
GET shopping_products-190303/_doc/0?stored_fields=price
## routing
GET shopping_products-190303/_doc/0?routing=0
## refresh
GET shopping_products-190303/_doc/0?refresh=true
## 문서만 반환
GET shopping_products-190303/_doc/0/_source
# 17. Document 삭제
POST test_index/_doc/0
{
"name": "test"
}
## 인덱스 삭제
DELETE test_index
## 쿼리 결과만 삭제
POST test_index/_delete_by_query
{
"query": {
"bool": {
"filter": {
"term": {
"name": "test"
}
}
}
}
}
# 18. Document 업데이트
DELETE shopping_products-190310
PUT shopping_products-190310/_doc/0
{
"name": "핸드폰",
"price": 1000000,
"id": "1111",
"checker": "occidere"
}
GET shopping_products-190310/_doc/0
## 값을 덮어씌워서 업데이트
PUT shopping_products-190310/_doc/0
{
"name": "핸드폰",
"price": 1200000,
"id": "1111",
"checker": "occidere"
}
GET shopping_products-190310/_doc/0
## 값 10만원 할인
POST shopping_products-190310/_doc/0/_update
{
"script": {
"lang": "painless",
"source": "ctx._source.price -= params.discount",
"params": {
"discount": 100000
}
}
}
GET shopping_products-190310/_doc/0
## id 변경
POST shopping_products-190310/_update_by_query
{
"query": {
"bool": {
"filter": {
"term": {
"id": "1111"
}
}
}
},
"script": {
"lang": "painless",
"source": "ctx._source.id = \"1234\""
}
}
GET shopping_products-190310/_doc/0
## 스크립트 사용하지 않고 값을 덮어씌우는 방식
POST shopping_products-190310/_doc/0/_update
{
"doc": {
"category": "computers"
},
"doc_as_upsert": true
}
GET shopping_products-190310/_doc/0
# 19. Bulk API
DELETE bulk_test
POST bulk_test/_doc/_bulk
{"index": {"_id": "0"}}
{"name": "test0"}
{"create": {"_id": "1"}}
{"name": "test1"}
{"delete": {"_id": "1"}}
{"update": {"_id": "0"}}
{"doc": {"name": "test000", "count": 100}}
GET bulk_test/_search
# 20. MultiGet API
GET _mget
{
"docs" : [
{
"_index": "bulk_test",
"_id" : "0",
"_source": ["name", "count"]
},
{
"_index": "shopping_products-190303",
"_id" : "0"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment