-
Notifications
You must be signed in to change notification settings - Fork 43
API
Gee edited this page Feb 12, 2018
·
14 revisions
- Kibana Console에서 사용 가능한 코드이다
- Kibana Console은 Kibana 접속 후
Dev Tools
선택하면 나온다 - 터미널에서 사용하는
curl
을 이용하려면 적당히 변형이 필요하다 - 한 번 설정한 Mapping은 변경할 수 없다
- reindex는 복사하려는 Index에 Mapping을 먼저 해두기를 권장한다
- Indicies API
- Document API
- Document 추가
- ID로 Document 조회
- Document 삭제
- Document 수정
- Documetns 복사
- Search API (Request Body Search)
- 형식 :
PUT {Index 이름}
- 예시 :
PUT test_index
- 형식 :
DELETE {Index 이름}
- 예시 :
DELETE test_index
- 형식
PUT {Index 이름}/_mapping/{Type 이름}
{
"properties": {
"{Field 이름}" : {
"type" : "{Field Type}"
}
}
}
- 예시
PUT test_index/_mapping/test_type
{
"properties": {
"가격" : {
"type" : "integer"
}
}
}
- 형식
PUT {Index 이름}
{
"mappings": {
"{Type 이름}": {
"properties": {
"{Field1 이름}" : {
"type": "{Field1 Type}"
},
"{Field2 이름}": {
"type" : "{Field2 Type}"
}
}
}
}
}
- 예시
PUT new_index
{
"mappings": {
"new_type": {
"properties": {
"가격" : {
"type": "integer"
},
"날짜": {
"type" : "date"
}
}
}
}
}
- 형식
PUT _template/{Template 이름}
{
"template": "{Index Pattern}",
"mappings": {
"{Type 이름}": {
"properties": {
"Field1 이름": {
"type": "Field1 Type"
},
"Field2 이름": {
"type": "Field2 Type"
}
}
}
}
}
- 예시
PUT _template/template_1
{
"template": "log-10.*",
"mappings": {
"my_type": {
"properties": {
"조회수(sec)": {
"type": "integer"
},
"날짜": {
"type": "date"
}
}
}
}
}
- 형식
PUT {Index 이름}/_mapping/{Type 이름}
{
"properties": {
"{Field 이름}" : {
"type" : "{Field Type}"
}
}
}
- 예시
PUT test_index/_mapping/test_type
{
"properties": {
"나이" : {
"type" : "integer"
}
}
}
- 형식 :
GET /{Index 이름}/_mapping/{Type 이름}
- 예시 :
GET /test_index/_mapping/test_type
-
Document ID 지정
- 형식
PUT {Index 이름}/{Type 이름}/{ID} { "{Field 이름}" : {Value} }
- 예시
PUT test_index/test_type/1 { "가격" : 10000, "나이" : 17, "상품" : "맥북프로" }
-
Document ID 지정하지 않음
- 형식
PUT {Index 이름}/{Type 이름}/{ID} { "{Field 이름}" : {Value} }
- 예시
PUT test_index/test_type/1 { "가격" : 10000, "나이" : 17, "상품" : "맥북프로" }
- 형식 :
GET {Index 이름}/{Type 이름}/{ID}
- 예시 :
GET test_index/test_type/1
- 형식 :
DELETE {Index 이름}/{Type 이름}/{ID}
- 예시 :
DELETE test_index/test_type/1
- 형식 (match query 예시)
POST {Index 이름}/_delete_by_query
{
"query": {
"match": {
"{Field 이름}": "{Value}"
}
}
}
- 예시
POST test_index/_delete_by_query
{
"query": {
"match": {
"상품": "아이폰"
}
}
}
- 형식
POST {Index 이름}/{Type 이름}/{ID}/_update
{
"doc": {
"{Field}" : {Value}
}
}
- 예시
POST test_index/test_type/3/_update
{
"doc": {
"가격" : 17000
}
}
- 형식
POST {Index 이름}/{Type 이름}/{ID}/_update
{
"doc" : {
"{Field 이름}" : "{Value}"
},
"doc_as_upsert" : true
}
- 예시
POST test_index/type_type/3/_update
{
"doc" : {
"나이" : "30"
},
"doc_as_upsert" : true
}
- 형식 (term query 예시)
POST {Index 이름}/{Type 이름}/_update_by_query
{
"script": {
"source": "ctx._source[{Field 이름}] = Value
},
"query": {
"term": {
"{Field 이름}": "Value"
}
}
}
- 예시
- 1개 Field
POST test_index/test_type/_update_by_query { "script": { "source": "ctx._source['가격'] = 7777", "lang": "painless" }, "query": { "term": { "상품": "아이폰" } } }
- 복수개 Field
POST test_index/test_type/_update_by_query { "script": { "source" : "ctx._source['가격'] = 7777; ctx._source['나이'] = 19;", "lang" : "painless" }, "query": { "term": { "나이": 17 } } }
- 전체 Documents 복사
- 형식
POST _reindex { "source": { "index": "{복사하려는 원본 Index 이름}" }, "dest": { "index": "{복사본을 저장할 Index 이름}" } }
- 예시
POST _reindex { "source": { "index": "test_index" }, "dest": { "index": "test_index_2" } }
- 일부 Documents 복사
- 형식
POST _reindex { "source": { "index": "{복사하려는 Index 이름}", "type" : "{복사하려는 Type 이름}", "query": { "term": { "{Field 이름}": "{Value}" } } }, "dest": { "index": "{복사본을 저장할 Index 이름}" } }
- 예시
POST _reindex { "source": { "index": "test_index", "type" : "test_type", "query": { "term": { "상품": "아이폰" } } }, "dest": { "index": "test_index_3" } }
- 외부에 있는 Elasticsearch Index 복사
- 형식
POST _reindex { "source": { "remote": { "host": "{Elasticsearch url}", "username": "{유저 이름}", "password": "{비밀번호}" }, "index": "{복사할 Index 이름}", }, "dest": { "index": "{복사본을 저장할 Index 이름}" } }
- 예시
POST _reindex { "source": { "remote": { "host": "http://otherhost:9200", "username": "user", "password": "pass" }, "index": "source", "query": { "match": { "test": "data" } } }, "dest": { "index": "dest" } }
- 설명 : Index의 모든 Documents를 보여준다
- 형식
GET {Index 이름}/{Type 이름}/_search
{
"query" : {
"match_all" : {}
}
}
- 예시
GET shopping/shopping/_search
{
"query" : {
"match_all" : {}
}
}
- 설명 : Match 조건을 충족하는 Documents를 보여준다
- 형식
GET {Index 이름}/{Type 이름}/_search
{
"query" : {
"match" : {
"{Field 이름}" : "{Value}"
}
}
}
- 예시
GET shopping/shopping/_search
{
"query" : {
"match" : {
"상품분류" : "셔츠"
}
}
}
- 파라미터
-
operator
,minimum_should_match
가 자주 쓰인다 - operator
-
and
또는or
이며 기본은or
이다 -
query
가 "상품 이상"면- and일 경우 "상품", "이상"를 모두 가진 Document가 출력되며
- or일 경우, "상품", "이상" 중 하나라도 가진 Document가 출력된다
- 예시
GET shopping/shopping/_search { "query": { "match" : { "배송메모" : { "query" : "상품 이상", "operator" : "and" } } } }
-
- minimum_should_match
-
query
가 "상품 이상 고장"이고minium_should_match
가 2라고 하면 "상품", "이상", "고장" 중 적어도 2개는 포함하는 Documents가 출력된다. (operator는 default로 or이다) - 예시
GET shopping/shopping/_search { "query": { "match" : { "배송메모" : { "query" : "상품 이상 고장", "minimum_should_match" : 2 } } } }
-
-
- 설명 : Search에서 사용했던 Lucene Query Syntax를 만족하는 Documents가 표시된다
- 형식
GET {Index 이름}/{Type 이름}/_search
{
"query" : {
"query_string" : {
"query" : "{LUCENE QUERY}"
}
}
}
- 예시
GET shopping/_search
{
"query" : {
"query_string" : {
"query": "상품가격 : [1 TO 50000]"
}
}
}
- 설명
- 다양한 종류의 혹은 복수개의 query를 묶어서 사용할 수 있게 해준다
- 예를 들어 query1, query2는 만족하면서 query3은 만족하지 않는 Documents를 찾고 싶은 경우
- 사용 가능한 Boolean Clauses
- must : 반드시 만족해야 하는 query
- must not : 반드시 제외되어야 하는 query
- should : n개 이상 만족해야 하는 query (n : minimum_should_match로 설정)
- 예시
GET shopping/shopping/_search
{
"query": {
"bool": {
"must": [
{
"term": { "결제카드": "시티" }
}
],
"must_not": [
{ "term": { "구매사이트": "11번가" }},
{ "range": {
"상품가격" : {"gte" : 20000 }}}
]
}
}
}
GET shopping/_search
{
"query": {
"bool": {
"must": [
{
"range": {
"고객나이": {
"gt": 25
}
}
}
],
"must_not": [
{
"wildcard": {
"서울주소_시도": "경?도"
}
}
],
"should": [
{
"term": {
"결제카드": "우리"
}
},
{
"script": {
"script": {
"source": "doc['주문시간'].date.hourOfDay > 18"
}
}
}
],
"minimum_should_match": 1
}
}
}
- 설명 : 검색 Term과 정확히 일치하는 Document를 표시한다
- 형식
GET {Index 이름}/{Type 이름}/_search
{
"query" : {
"term" : {
"{Field 이름}" : "{Value}"
}
}
}
- 예시
GET shopping/shopping/_search
{
"query" : {
"term" : {
"상품분류" : "셔츠"
}
}
}
- 설명 : 검색 Terms 중에서 적어도 1개의 Term과 정확히 일치하는 Document를 표시한다
- 형식
GET {Index 이름}/{Type 이름}/_search
{
"query" : {
"terms" : {
"{Field 이름}" : ["{Value}", "{Value}"]
}
}
}
- 예시
GET shopping/shopping/_search
{
"query" : {
"terms" : {
"상품분류" : ["셔츠", "스웨터"]
}
}
}
- 설명 : 특정 Field의 값이 입력값의 범위 내에 존재하는 Document를 표시한다
- 파라미터
-
gte
: 특정값보다 크거나 같다 -
gt
: 특정값보다 크다 -
lte
: 특정값보다 작거나 같다 -
lt
: 특정값보다 작다
-
- 형식
GET {Index 이름}/{Type 이름}/_search
{
"query": {
"range": {
"{Field 이름}": {
"gte": "{Value},
"lte": "{Value},
}
}
}
}
-
예시
- Date Field
GET shopping/shopping/_search { "query": { "range": { "주문시간": { "gte": "2017-02-15T07:00:00" } } } }
- Number Field
GET shopping/shopping/_search { "query": { "range": { "상품가격": { "gte" : 15000, "lte" : 20000 } } } }
- 설명 : 검색하려는 Field에 non-null value가 하나라도 있는 Document를 표시한다
- 형식
GET {Index 이름}/{Type 이름}/_search
{
"query": {
"exists" : {
"field" : "{Field 이름}"
}
}
}
- 예시
GET shopping/shopping/_search
{
"query": {
"exists" : {
"field" : "상품분류"
}
}
}
- 설명 : 특정 Field가 특정 검색어로 시작하는 Documents 표시
- 형식
GET {Index 이름}/{Type 이름}/_search
{
"query": {
"prefix" : {
"{Field 이름}" : "{Value}"
}
}
}
- 예시
GET shopping/shopping/_search
{
"query": {
"prefix" : {
"고객주소_시도" : "경상"
}
}
}
- 설명 : 특정 Field가 wildcard expression을 만족하는 Documents 표시
- 형식
GET {Index 이름}/{Type 이름}/_search
{
"query": {
"wildcard" : {
"{Field 이름}" : "{Value}"
}
}
}
- 예시
GET shopping/shopping/_search
{
"query": {
"wildcard" : {
"고객주소_시도" : "경**도"
}
}
}
- 설명 : 특정 Field의 Value가 검색어와 유사한 Document 보여준다
- 파라미터 :
fuzziness
로서 편집 거리를 나타낸다 - 형식
- 기본 (fuzziness=auto)
GET {Index 이름}/{Type 이름}/_search { "query": { "fuzzy" : { "{Field 이름}" : "{Value}" } } }
- fuzziness 할당
GET {Index 이름}/{Type 이름}/_search { "query": { "fuzzy" : { "{Field 이름}" : { "value" : "{Value}", "fuzziness" : {Fuzziness 정도} } } } }
- 예시
GET shopping/shopping/_search
{
"query": {
"fuzzy" : {
"고객주소_시도" : {
"value" : "경상북남",
"fuzziness" : 2
}
}
}
}
- 설명 : 결과 Documents의 Pagination으로 조건을 만족하는 Documents 중, 어느 위치에서부터 몇 개를 출력할지 결정한다
- 예시 : 처음 10개 출력하는 경우
GET shopping/shopping/_search
{
"from" : 0, "size" : 1,
"query" : {
"match_all" : {}
}
}
- 설명 : 결과 Documents를 특정 기준으로 정렬해준다
- 파라미터 : 정렬기준은
desc(내림차순)
또는asc(오름차순)
이 있다 - 형식
GET {Index 이름}/{Type 이름}/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"{FIELD 이름}": {
"order": "{정렬기준}"
}
}
]
}
- 예시 : 판매자평점이 큰 순으로 정렬
GET shopping/shopping/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"판매자평점": {
"order": "desc"
}
}
]
}
- 설명 : 결과 Documents에서 특정 Field만 조회하고 싶을 때 사용
- 형식
- 기본 :
_source
에 표시한 Field만 조회
GET {Index 이름}/{Type 이름}/_search { "_source": "{Field 이름}", "query" : { "match_all" : {} } }
- 응용 : 특정 필드는 제외하고, 특정 필드는 포함시키고 싶을 때
GET {Index 이름}/{Type 이름}/_search { "_source": { "includes" : "{Field 이름}", "excludes" : "{Field 이름}" }, "query" : { "match_all" : {} } }
- 기본 :
- 예시
GET shopping/shopping/_search
{
"_source": {
"includes" : ["고객*", "구매사이트"],
"excludes" : "상품*"
},
"query" : {
"match_all" : {}
}
}
- 설명 : 검색된 Documents는 한 번에 보여주지 않고 Page 별로 보여주게 하는 기능
- 사용법 예시
- 검색 Query 작성
POST shopping/shopping/_search?scroll=10m { "size": 500, "query": { "match" : { "상품분류" : "셔츠" } } }
- 결과로 나온
_scroll_id
확인 - scroll를 이용한 결과 조회
POST /_search/scroll { "scroll" : "10m", "scroll_id" : "{_scroll_id}" }