일래스틱서치에서 저장된 index가 월별일 때 특정 월만 검색하고 싶다면 body나 request에 포함할 수 있다.
curl -X GET "http://inhouse.google.com:9200/reqlog-*/_search?ignore_unavailable=true" -H 'Content-Type: application/json' -d'
{
"query": {
"terms" : {
"_index" : ["reqlog-2019-09, reqlog-2019-10"]
}
}
}'
또는
curl -X GET http://inhouse.google.com:9200/reqlog-*/_search/reqlog-2019-09, reqlog-2019-10/_search?ignore_unavailable=true
{
"query": {
...
}
}
ignore_unavailable의 기본 값은 false이다. 검색시 인덱스가 없으면 에러가 발생한다.
true로 설정해서 검색시 인덱스 없으면 에러가 발생하지 않는다.
Posted by 김용환 '김용환'
파이프라인 프로세서(pipeline processor)를 사용하면 특정 인덱스로 저장 또는 검색할 수 있다.
먼저 템플릿을 저장한다.
$ curl -X PUT "localhost:9200/_template/reglog?pretty" -H 'Content-Type: application/json' -d'
{
"template": "reqlog_*",
"mappings": {
"properties": {
"id": {
"type": "long"
},
"date1": {
"type": "date"
}
}
}
}
'
파이프라인을 등록한다. 파이프라인을 월별로 저장하기 위해 설정한 부분은 아래와 같다.
$ curl -X PUT "http://localhost:9200/_ingest/pipeline/reqlog-monthly-index" -H 'Content-Type: application/json' -d'
{
"description": "monthly index naming for reqlog",
"processors" : [
{
"date_index_name" : {
"field" : "date1",
"index_name_prefix" : "reqlog_",
"index_name_format" : "yyyy-MM",
"date_rounding" : "M"
}
}
]
}'
파이프에 템플릿을 저장하는 방식을 사용해 데이터를 인덱스에 저장한다.
$ curl -X PUT "localhost:9200/reqlog/_doc/2?pipeline=reqlog-monthly-index&pretty" -H 'Content-Type: application/json' -d'
{
"date1" : "2019-01-25T20:16:55.000Z"
}
'
데이터를 확인하면 제대로 있다. 이렇게 월별 인덱스에 저장한다.
$ curl http://localhost:9200/reqlog_2019-01/_doc/2
이렇게 다르게 저장해도 제대로 데이터를 검색할 수 있다.
$ curl -X PUT "localhost:9200/reqlog/_doc/4?pipeline=reqlog-monthly-index&pretty" -H 'Content-Type: application/json' -d'
{
"date1" : "2019-02-25T20:16:55.000Z"
}
'
$ curl http://localhost:9200/reqlog_2019-02/_doc/4
연도별 인덱스를 생성하려면 다음과 같이 파이프라인을 정의할 수 있다.
curl -X PUT "http://localhost:9200/_ingest/pipeline/reqlog_yearly_index" -H 'Content-Type: application/json' -d'
{
"description": "yearly index naming for reqlog_yearly",
"processors" : [
{
"date_index_name" : {
"field" : "date1",
"index_name_prefix" : "reqlog_yearly_",
"index_name_format" : "yyyy",
"date_rounding" : "y"
}
}
]
}'
그런데 문제는 UTC 기준으로 데이터를 인덱싱하지 못한다는 점이다.
timezone: +09:00을 사용하면 잘 동작한다.
$ curl -X PUT "http://localhost:9200/_ingest/pipeline/reqlog-monthly-index" -H 'Content-Type: application/json' -d'
{
"description": "monthly index naming for reqlog",
"processors" : [
{
"date_index_name" : {
"field" : "date1",
"index_name_prefix" : "reqlog_",
"index_name_format" : "yyyy-MM",
"date_rounding" : "M",
"timezone": "+09:00"
}
}
]
}'
만약 java timemillis 타입으로 데이터를 저장했다면
date_formats의 값을 [UNIX_MS]로 변경한다.
$ curl -X PUT "http://localhost:9200/_ingest/pipeline/reqlog-monthly-index" -H 'Content-Type: application/json' -d'
{
"description": "monthly index naming for reqlog",
"processors" : [
{
"date_index_name" : {
"field" : "date1",
"index_name_prefix" : "reqlog_",
"index_name_format" : "yyyy-MM",
"date_formats" : [ "UNIX_MS" ],
"date_rounding" : "M",
"timezone": "+09:00"
}
}
]
}'
참고 자료
https://github.com/elastic/elasticsearch/blob/7.4/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DateIndexNameFactoryTests.java
Posted by 김용환 '김용환'
kibana 5.x에서는 index-pattern id가 index 이름이었는데.
kibana 6.x에서는 index-pattern id가 uuid로 바뀌었다.
키바나 내부 api를 사용해 어떤 save object가 있는지 확인할 수 있다.
curl -s http://kibana.internal.google.io:5601/api/saved_objects/_find?type=index-pattern
{
"type": "index-pattern",
"id": "b1d1bed0-464d-11e9-9577-0b28abc59fe5",
"attributes": {
"title": "google_search_admin*",
"timeFieldName": "customer",
"fields": "[{\"name\":\"_id\",\"type\":\
...
}
kibana index 변경을 하고 싶으면 index_pattern을 수정해야 한다.
https://qiita.com/NAO_MK2/items/2d03d9db1cd7b0ceae04
Posted by 김용환 '김용환'
일래스틱서치에서 인덱스에 대해서만 앨리어싱을 사용할 수 있었는데,
일래스틱서치 6.4 부터 필드에 앨리어싱을 추가할 수 있다.
https://www.elastic.co/guide/en/elasticsearch/reference/6.4/alias.html
PUT trips
{
"mappings": {
"_doc": {
"properties": {
"distance": {
"type": "long"
},
"route_length_miles": {
"type": "alias",
"path": "distance" //
},
"transit_mode": {
"type": "keyword"
}
}
}
}
}
GET _search
{
"query": {
"range" : {
"route_length_miles" : {
"gte" : 39
}
}
}
}
그러나 요청은 가능하지만 json 응답은 앨리어스의 원본으로 결과가 나온다.
또한, https://github.com/elastic/elasticsearch/issues/37212 에 따른대로 percolator(elasticsearch - alert)에서도 적용되지 않는다.
유의할 필요가 있다.
Posted by 김용환 '김용환'
elasticsearch에 /를 포함하는 id를 저장하고 싶을때..
/를 사용하면 path 구분자에 걸린다.
따라서 url에 / 대신 %2F를 사용하면 정상적으로 작동한다.
curl -X PUT "es.google.io:9200/google-host-staging/host/service.dos%2F0" -H 'Content-Type: application/json' -d'
{
"service":"0",
"host":"service.dos"
}
'
Posted by 김용환 '김용환'
elasticsearch5.4에서 _id만 저장하는 테스트를 진행했다.
testcase 시나리오는 다음과 같다.
index를 생성한다.
_id만 저장한다.
_id를 읽는다. 정상적이면 성공, 그러나 실패
성공한 것처럼 보인다... 그러나, 실제 curl로 _id를 document를 요청하면 데이터는 없다고 나온다!!
(사실 문법 에러는 아니지만,, 저장은 안해.. 라는 것으로 보인다)
따라서 아무리 간단한 데이터를 저장하더라도 _id와 field 데이터 하나 정도는 저장해야 디스크에 저장하길 바란다.
(당연하지만..)
Posted by 김용환 '김용환'
curl을 이용하다가 다음과 같은 es 에러가 발생할 수 있다.
{"error":"Content-Type header [application/x-www-form-urlencoded] is not supported","status":406}
curl 다음에 다음 매개 변수를 주면 에러가 발생하지 않을 것이다. 5에 비해 많이 strict 해졌다.
--header 'content-type: application/json'
-H 'Content-Type: application/json'
Posted by 김용환 '김용환'
일래스틱서치(elasticsearch) 6에서 템플릿 목록 보기
$ curl -s "http://es-server:9200/_cat/templates?v&s=order:desc,template"
name template order version
slowlog * 0
template_name google_api-* 0
특정 템플릿을 살펴보기
$ curl -s http://es-server:9200/_template/템플릿_이름 | python -mjson.tool
자세한 정보 나옴.
특정 템플릿 저장하기
$ curl -XPUT -H "Content-type:application/json" http://es-server:9200/_template/템플릿_이름 -d @파일이름
Posted by 김용환 '김용환'
댓글을 달아 주세요