'mapping'에 해당되는 글 12건

  1. 2022.07.14 [Elasticsearch] Index Template 사용 시 Mapping 정보 오류
  2. 2020.09.10 [Elasticsearch] Dynamic Templates 간단 정리
  3. 2018.07.05 [Elasticsearch] Default field type 값.
  4. 2018.06.27 [Elasticsearch] _all field 사용하지 마세요.
  5. 2017.12.06 [Elasticsearch] Refresh interval while bulk request 2
  6. 2015.12.09 [Elasticsearch - The Definitive Guide] Customizing Dynamic Mapping
  7. 2015.11.30 [Elasticsearch - The Definitive Guide] Mapping
  8. 2014.12.05 [Elasticsearch] dynamic template 이란?
  9. 2013.05.22 [elasticsearch] mapping 분석 및 이해. 5
  10. 2013.04.17 [elasticsearch] Indices API - Put Mapping

[Elasticsearch] Index Template 사용 시 Mapping 정보 오류

Elastic/Elasticsearch 2022. 7. 14. 19:30

사용하다 보면 정말 별거 아닌 건데 눈에 잘 안보일 때가 있습니다.

 

에러 메시지)

"reason"=>"failed to parse field [message] of type [text] in document with id '3UI29IEBerapX-zaQi4L'. 
Preview of field's value: 
...중략...
"caused_by"=>{"type"=>"illegal_state_exception", "reason"=>"Can't get text on a 
START_OBJECT at 1:529"}

 

이런 에러메시지를 접하게 되면 두 눈 크게 뜨고 Template 에서 정의한 type 정보와 실제 들어 오는 값이 잘 매핑이 되어 있는지 확인을 해보시기 바랍니다.

 

모니터는 큰걸로 글자 크기는 크게 해서 보세요. 

:

[Elasticsearch] Dynamic Templates 간단 정리

Elastic/Elasticsearch 2020. 9. 10. 12:18

Elasticsearch Reference 를 그대로 의역한 수준 이라고 보시면 될것 같습니다.

 

[참고문서]

www.elastic.co/guide/en/elasticsearch/reference/current/dynamic-templates.html

www.elastic.co/guide/en/elasticsearch/reference/current/dynamic-field-mapping.html

 

match_mapping_type)
JSON 파서를 통해서 data type 을 detection 합니다.
long, double 의 경우 integer 와 float 에 대한 정확한 detection 이 어렵기 때문에 항상 long 과 double 로 detection 하게 됩니다.

detection type 은 아래와 같습니다.
- boolean
- date
- double
- long
- object
- string

[Code Snippet from Elastic]
- long type 으로 matching 된 것을 모두 integer 로 설정 하는 것과
- string type 으로 matching 된 것을 모두 text 와 fields 설정을 이용해서 keyword 로 설정 하는 것입니다.

PUT my-index-000001
{
  "mappings": {
    "dynamic_templates": [
      {
        "integers": {
          "match_mapping_type": "long",
          "mapping": {
            "type": "integer"
          }
        }
      },
      {
        "strings": {
          "match_mapping_type": "string",
          "mapping": {
            "type": "text",
            "fields": {
              "raw": {
                "type":  "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    ]
  }
}


match and unmatch)
이 설정은 field명에 대한 패턴 매칭을 이용해서 data type 을 detection 합니다.
unmatch 패턴은 제외 시키기 위한 설정 이라고 생각 하면 됩니다.

[Code Snippet from Elastic]
- data type 이 string 으로 매칭 된 경우 필드명을 통해 최종 설정을 하는 것입니다.
- 필드명을 통해서 data type 을 설정 하는 것입니다.

PUT my-index-000001
{
  "mappings": {
    "dynamic_templates": [
      {
        "longs_as_strings": {
          "match_mapping_type": "string",
          "match":   "long_*",
          "unmatch": "*_text",
          "mapping": {
            "type": "long"
          }
        }
      }
    ]
  }
}


match_pattern)
이 설정은 위에서 "match":   "long_*" 과 같은 wildcard 패턴을 java 정규식으로 사용하기 위한 설정입니다.

[Code Snippet from Elastic]

  "match_pattern": "regex",
  "match": "^profit_\d+$"


path_match and path_unmatch)
이 설정은 nested 나 object field 에 대한 match, unmatch 와 동일한 설정 입니다.

% Nested field type
The nested type is a specialised version of the object data type 
that allows arrays of objects to be indexed in a way 
that they can be queried independently of each other.

{name} and {dynamic_type}
이 설정은 field명을 {name} 으로 사용하고 matching 된 data type 을 {dynamic_type} 으로 사용하는 설정입니다.

[Code Snippet from Elastic]
- analyzer 로 english analyer 를 사용 하겠다는 의미 입니다. {name} == "english"
- {dynamic_type} == long 으로 매칭이 되었고 doc_values 를 사용하지 않겠다는 의미 입니다.

PUT my-index-000001
{
  "mappings": {
    "dynamic_templates": [
      {
        "named_analyzers": {
          "match_mapping_type": "string",
          "match": "*",
          "mapping": {
            "type": "text",
            "analyzer": "{name}"
          }
        }
      },
      {
        "no_doc_values": {
          "match_mapping_type":"*",
          "mapping": {
            "type": "{dynamic_type}",
            "doc_values": false
          }
        }
      }
    ]
  }
}

PUT my-index-000001/_doc/1
{
  "english": "Some English text", 
  "count":   5 
}

지금까지 보셨다면 위 설정은 Index 에 직접 설정 한다는 것을 아실 수 있습니다.

하지만 Elasticsearch 에서는 별도 Index Template 이라는 것을 제공 하고 있습니다.

 

[참고문서]

www.elastic.co/guide/en/elasticsearch/reference/7.9/index-templates.html

www.elastic.co/guide/en/elasticsearch/reference/7.9/indices-templates-v1.html

www.elastic.co/guide/en/elasticsearch/reference/7.9/indices-component-template.html

 

이 index template 설정은 최신 버전으로 넘어 오면서 두 가지 타입이 존재 합니다.

- _template 에 설정

PUT /_template/<index-template>

 

- _component_template 에 설정
PUT /_component_template/<component-template>

 

이 설정이 dynamic template 과의 차이는 index 에 직접 하느냐 cluster 에 하느냐의 차이 입니다.

더불어 dynamic template 은 mapping 에 대한 내용이지만 index template 은 setting, mapping 등 모두 포함 하고 있습니다.

중요한 내용이니 꼭 인지 하시기 바랍니다.
더불어 주의 하셔야 하는 점은 이런 템플릿 설정은 인덱스가 생성 되는 시점에 적용이 되는 것이기 때문에,

이미 생성된 인덱스에는 영향을 끼치지 못한다는 것도 알아 두셔야 합니다.

 

[Code Example]

- 아래 template 과 dynamic mapping 을 함께 설정한 예제 입니다.

- 기존 예제만 함쳐 놓은 것이기 때문에 구성에 맞게 고쳐서 사용 하시면 됩니다.

PUT /_template/template_1
{
  "index_patterns" : ["te*"],
  "order" : 0,
  "settings" : {
    "number_of_shards" : 1
  },
  "mappings" : {
    "_source" : { "enabled" : false },
    "dynamic_templates": [
      {
        "integers": {
          "match_mapping_type": "long",
          "mapping": {
            "type": "integer"
          }
        }
      },
      {
        "strings": {
          "match_mapping_type": "string",
          "mapping": {
            "type": "text",
            "fields": {
              "raw": {
                "type":  "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    ]
  }
}

 

:

[Elasticsearch] Default field type 값.

Elastic/Elasticsearch 2018. 7. 5. 11:04

elasticsearch 문서 모델링 하면서 잘 알고 있다고 생각 하지만 늘 그렇듯 잘 잊어버립니다.

그래서 올려 봅니다. (어딘가에 글 올려 놓은게 있긴 할테지만.. )



우선, FieldMapper 클래스를 확인 합니다.

여기서 보면 MappedFieldType 클래스로 넘어 가게 됩니다.


그래서 default field type 은 아래와 같습니다.


public MappedFieldType() {
setTokenized(true);
setStored(false);
setStoreTermVectors(false);
setOmitNorms(false);
setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);
setBoost(1.0f);
}


:

[Elasticsearch] _all field 사용하지 마세요.

Elastic/Elasticsearch 2018. 6. 27. 12:23

_all 필드는 앞으로 사라질 예정입니다.

그러니 사용하지 않도록 합니다.


기본 _all 필드는 disabled 이기 때문에 별도 mapping 설정에 추가 하지 않으셔도 됩니다.

만약 사용 하실 거라면 아래 문서를 참고하세요.


Reference)

https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-all-field.html


_all enable 시 아래와 같은 경고 문구를 보실 수 있습니다.


returned 1 warnings: [299 Elasticsearch-6.3.0-424e937 "[_all] is deprecated in 6.0+ and will be

removed in 7.0. As a replacement, you can use [copy_to] on mapping fields to create your own

catch all field." "Wed, 27 Jun 2018 03:14:14 GMT"]


:

[Elasticsearch] Refresh interval while bulk request

Elastic/Elasticsearch 2017. 12. 6. 14:00

작업 하면서 이상한 현상이 발생을 해서 분석 하다 보니 누구나 경험 할 수 있는 것 같아 올려 봅니다.


참고문서)

https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-update-settings.html

https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-refresh.html


보통 bulk request 하기 전에 아래 설정을 적용하게 됩니다.

index.refresh_interval: "-1"

이 설정은 해당 index 의 settings 에서 설정 합니다.


이 설정을 하게 되면 bulk request 시 refresh action 을 수행 하지 않게 되는데요.

제가 경험한 현상은 disable 했음에도 불구하고 refresh thread 수가 증가 한다는 것이였습니다.


문제는 역시 elasticsearch 에 있었던 것이 아닌 저의 잘 못 이였습니다.

이유는 제가 정의한 mapping 정보에서 dynamic field 에 따른 template 구성이 영향을 주는 것이였습니다.

결과적으로 dynamic field 설정으로 색인 시 mapping 정보가 바뀌게 되고 이를 반영 하기위해 IndexService 가 updateMetaData() 를 수행 하게 됩니다. 이 과정에서 자동으로 refresh 가 발생을 하기 때문에 bulk request 시 왜 성능이 안나오지 하지 마시고 어떤 구성을 하셨는지 부터 분석해 보시면 더 좋을 것 같습니다.


:

[Elasticsearch - The Definitive Guide] Customizing Dynamic Mapping

Elastic/TheDefinitiveGuide 2015. 12. 9. 15:17

별건 아니고 작은 팁 정도 입니다.


원문링크)

https://www.elastic.co/guide/en/elasticsearch/guide/current/dynamic-mapping.html

https://www.elastic.co/guide/en/elasticsearch/guide/current/custom-dynamic-mapping.html


[date_detection 설정]

PUT /my_index { "mappings": { "my_type": { "date_detection": false } } }


dynamic mapping 의 경우 아래 세 가지 옵션을 가집니다.


true
Add new fields dynamically—the default
false
Ignore new fields
strict
Throw an exception if an unknown field is encountered


date_detection 설정의 경우 문서에도 있지만 처음 들어온 데이터가 date type 으로 인식이 되면 이후 데이터가 string 이더라도 date 로 처리가 된다는 이야기 입니다. 그렇기 때문에 에러가 발생을 하게 될 것이구요.


저는 기본적으로 dynamic mapping 은 false 또는 strict 로 구성 하는 것을 추천 드립니다.

이유는 위와 같은 문제도 있고 하고자 하는 것에 대한 투명성을 보장하는게 좋지 않나 생각해서 입니다.

:

[Elasticsearch - The Definitive Guide] Mapping

Elastic/TheDefinitiveGuide 2015. 11. 30. 15:26

간혹 dynamic mapping 기능으로 인한 type mismatch 오류를 경험 하시는 분들이 있습니다.

그래서 기록해 봤습니다.

The Definitive Guide 에 올라온 Note 입니다.


원문링크)


원문 Snippet)

This means that if you index a number in quotes ("123"), it will be mapped as type string, not type long. However, if the field is already mapped as type long, then Elasticsearch will try to convert the string into a long, and throw an exception if it can’t.


정리 및 작은 팁을 말씀 드리면,

- 정수형 이면 그냥 long 으로 선언 하세요.

- 실수 형이면 그냥 double 로 선언 하세요.

- ".." 로 묶으면 string 형으로 선언 됩니다.

- dynamic mapping 은 explicity 하게 사용하시는게 좋은 습관 입니다.


:

[Elasticsearch] dynamic template 이란?

Elastic/Elasticsearch 2014. 12. 5. 11:12

Elasticsearch에서 제공하는 편리한 기능 중 하나 입니다.

이해하기 위해서는 dynamic mapping 부터 보고 들어 가셔야 합니다.


[원본 링크]

Dynamic mapping

- http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/dynamic-mapping.html


☞ 간단 설명

색인 필드의 데이터에 대한 data type을 지정하지 않고 동적으로 elasticsearch 에서 적절한 type을 지정해 주는 기능 입니다.


예) 

field1:"1" 은 string 으로

field2: 1   은 long 으로 type 맵핑이 됩니다.


그럼 dynamic template 에 대해서 알아 보겠습니다.

elasticsearch에서는 customizing dynamic mapping 하위에 subset으로 기술 되어 있습니다.


[원본 링크]

Dynamic template

- http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/custom-dynamic-mapping.html#dynamic-templates


dynamic template 이란?

이 기능은 말 그대로 type mapping 을 사전에 기술해 놓지 않고 동적으로 dynamic mapping에 의해 정의 되는 시점에 동적으로 구성을 하게 되는 내용입니다.


쉽게 말해 mapping type을 미리 선언하지 않고 패턴이나 분석 특성에 맞춰 구성하게 되는 것입니다.

주로 사용하는 경우는 대략 2 가지 정도로 보입니다.


1) 같은 유형의 indice/type을 time series 로 생성 및 관리 할 때 매번 만들지 않고 자동으로 mapping 생성을 하고자 할때.

2) 특정 조건 또는 패턴에 일치하는 field에 대해 자동으로 mapping 속성을 지정하고자 할때.


[_default_]

- 이 type이 dynamic template의 기본 type이 됩니다.

- 별도 구성 없이 동적으로 type을 생성하게 되면 이 정보를 기준으로 mapping type 정의가 됩니다.


내부 properties 설명은 elasticsearch 샘플 자료로 설명을 하겠습니다.


PUT /my_index
{
   
"mappings": {
       
"my_type": {
           
"dynamic_templates": [
               
{ "es": {
                     
"match":              "*_es",
                     
"match_mapping_type": "string",
                     
"mapping": {
                         
"type":           "string",
                         
"analyzer":       "spanish"
                     
}
               
}},
               
{ "en": {
                     
"match":              "*",
                     
"match_mapping_type": "string",
                     
"mapping": {
                         
"type":           "string",
                         
"analyzer":       "english"
                     
}
               
}}
           
]
}}}

- "my_type" 기본 type 이외 사용자가 정의한 type에도 dynamic template 설정이 가능 합니다.

- "match" 는 field에 대한 동적 매칭을 정의 하는 것이며, 패턴 사용이 가능 합니다. 예제에서는 field 명에서 _es 로 끝나는 모든 필드를 의미 합니다.

- "match_mapping_type"은 dynamic mapping 을 통해서 지정된 type 이 뭔지 확인 하는 것입니다. 예제에서는 string 으로 mapping 된 것을 의미 하며, "match"와 함께 해석을 해야 합니다. 즉, *_es 필드명 이어야 하고 string type이면 아래 mapping 정보를 가진다는 의미가 됩니다.

- "mapping"은 type mapping 시 각 field 에 속성을 정의 하게 됩니다. 이 정의 하는 부분에 대한 값을 설정하게 됩니다.


전체적으로 위 예제의 의미는 모든 문자열 필드에 대한 형태소분석기는 english를 적용하고, 필드명에 _es 가 들어간 문자열 필드에 대해서는 형태소분석기로 spanish를 적용하라는 것입니다.


dynamic template 과 mapping 기능은 매우 유용하게 활용이 가능 합니다.

이해 하시는데 도움이 되면 좋겠내요.



:

[elasticsearch] mapping 분석 및 이해.

Elastic/Elasticsearch 2013. 5. 22. 18:24

elasticsearch 에서 성능 및 운영에 있어서 기본이면서 핵심 내용이 될 수 있는 정보 입니다.

사실 이 내용만 잘 이해 하고 있으면 50% 이상 먹고 들어 갈 수 있습니다. ^^


mapping section

http://www.elasticsearch.org/guide/reference/mapping/


fields 와 types 를 확인해야 함.

fields

_id

document 의 unique id 는 _uid (_id + _type) 이며, _id 는 색인 ID 로 사용될 수 있다.

기본적으로 색인되지 않고 저장 하지 않습니다.

_type

기본적으로 색인은 하지만 저장은 하지 않습니다.

_source

자동으로 field 생성을 허용할지 결정 합니다.

_all

하나 또는 더 많은 field 를 색인시 저장 할 것인지 결정을 합니다.

"simple1" : {"type" : "long", "include_in_all" : true},

"simple2" : {"type" : "long", "include_in_all" : false}

_analyzer (설정 하지 않아도 되는 field)

색인 시 등록된 analyzer 또는 index_analyzer 를 사용 합니다.

또한, 특정 field 를 지정 할 경우 해당 field 에 정의된 analyzer 를 사용하게 됩니다.

_boost

문서나 field 의 연관성을 향상시키기 위해 사용한다.

_parent

parent type 을 지시하는 child mapping 정의 입니다.

blog type 과 blog_tag type 이 있을 경우 blog_tag 의 parent type 은 blog 가 됩니다.

_routing

색인 데이터에 대한 routing 관리를 위해서 사용 합니다.

routing field 는 store : yes, index : not_analyzed 로 설정이 되어야 합니다.

_index (설정 하지 않아도 되는 field)

index 가 소유한 문서를 store 합니다.

default false 로 저장 하지 않음.

_size (설정 하지 않아도 되는 field)

_source 에 의해서 자동으로 생성된 색인 field 의 수.

default disabled 입니다.

_timestamp

색인 시 문서의 timestamp 입니다.

기본 store : no, index : not_analyzed 이며,

설정 시 field 지정이 가능 합니다.

format 은 기본 dateOptionalTime. (http://www.elasticsearch.org/guide/reference/mapping/date-format/)

_ttl

색인 시 문서의 expiration date를 설정 합니다.

기본 disabled 입니다.

설정 시 ttl 이후 문서는 자동 삭제 됩니다.

core types

string type

index_name

array type 선언 시 사용되는 항목으로 array list 항목에 대한 개별 field 명으로 사용된다.

store

default no 이며, 저장에 대한 설정을 위해서 사용 된다.

yes 시 저장

index

검색 또는 색인 시 분석관련 설정을 위해서 사용 된다.

analyzed

검색과 색인 시 analyzer 를 이용해서 분석

not_analyzed

검색가능 하다는 의미

no

검색 불가능 하다는 의미

term_vector

기본 no 설정

no

yes

with_offsets

with_positions

with_positions_offsets

boost

기본 1.0

null_value

null value 시 기본 값은 아무것도 넣지 않으나 설정한 값이 있을 경우 등록 함.

omit_norms

기본 false 로 analyzed field 설정, true 일 경우 not_analyzed field 에서 설정

index_options

색인 옵션

docs

not_analyzed field

freqs

analyzed field

positions

analyzed field

analyzer

global 설정으로 검색과 색인 시 사용된다.

index_analyzer

색인 시에 사용된다.

search_analyzer

검색 시에 사용된다.

include_in_all

기본 true 로 설정 됨.

_all field 에 저장할 것인지 지정함.

ignore_above

지정한 크기 이상의 문자열을 무시 합니다.

position_offset_gap

number type

type : "float, double, byte, short, integer, and long",

index_name

store

index

precision_step

number 의 term value 를 만들어 냅니다.

설정 값이 작을 수록 검색은 빠르게 이루어 집니다.

기본 값은 4이며, 32bits 는 4 정도, 64bits 는 6~8정도를 사용합니다.

0 은 disable 입니다.

null_value

boost

include_in_all

ignore_malformed

비정상적인 숫자를 무시 한다.

기본 false로 설정 되어 있기 때문에 true 설정 하는 것이 좋다.

date type

index_name

format

http://www.elasticsearch.org/guide/reference/mapping/date-format.html

store

index

precision_step

number 의 term value 를 만들어 냅니다.

설정 값이 작을 수록 검색은 빠르게 이루어 집니다.

기본 값은 4이며, 32bits 는 4 정도, 64bits 는 6~8정도를 사용합니다.

0 은 disable 입니다.

null_value

boost

include_in_all

ignore_malformed

비정상적인 숫자를 무시 한다.

기본 false로 설정 되어 있기 때문에 true 설정 하는 것이 좋다.

boolean type

index_name

store

index

null_value

boost

include_in_all

binary type

index_name



default mapping template

"mapping" : {

"TYPE_NAME" : {

"analyzer" : "standard",

"index_analyzer" : "stadnard",

"search_analyzer" : "standard",

"_id" : {

"index" : "not_analyzed",

"store" : "yes",

"path" : "FIELD_NAME"

},

"_type" : {

"index" : "not_analyzed",

"store" : "yes"

},

"_source" : {

"enabled" : "false"

},

"_all" : {

"enabled" : "false"

},

"_boost" : {

"name" : "_boost",

"null_value" : 1.0

},

"_parent" : {

"type" : "PARENT_TYPE_NAME"

},

"_routing" : {

"required" : true,

"path" : "TYPE_NAME.FIELD_NAME"

},

"_timestamp" : {

"enabled" : true,

"path" : "DATE_FIELD_NAME",

"format" : "dateOptionalTime"

},

"properties" : {

"FIELD_NAME" : {

"type" : "string",

"index_name" : ,

"store" : ,

"index" : ,

"term_vector" : ,

"boost" : ,

"null_value" : ,

"omit_norms" : ,

"omit_term_freq_and_positions" : ,

"index_options" : ,

"analyzer" : ,

"index_analyzer" : ,

"search_analyzer" : ,

"include_in_all" : ,

"ignore_above" : ,

"position_offset_gap" : 

},

"FIELD_NAME" : {

"type" : "float, double, byte, short, integer, and long",

"index_name" : ,

"store" : ,

"index" : ,

"precision_step" : ,

"null_value" : ,

"boost" : ,

"include_in_all" : ,

"ignore_malformed" :

},

"FIELD_NAME" : {

"type" : "date",

"index_name" : ,

"format" : ,

"store" : ,

"index" : ,

"precision_step" : ,

"null_value" : ,

"boost" : ,

"include_in_all" : ,

"ignore_malformed" :

},

"FIELD_NAME" : {

"type" : "boolean",

"index_name" : ,

"store" : ,

"index" : ,

"null_value" : ,

"boost" : ,

"include_in_all" : ,

},

"FIELD_NAME" : {

"type" : "binary",

"index_name" : ,

}

}

}

}


:

[elasticsearch] Indices API - Put Mapping

Elastic/Elasticsearch 2013. 4. 17. 11:32

본 문서는 개인적인 테스트와 elasticsearch.org 그리고 community 등을 참고해서 작성된 것이며,

정보 교환이 목적입니다.


잘못된 부분에 대해서는 지적 부탁 드립니다.

(예시 코드는 성능 및 보안 검증이 되지 않았습니다.)



[elasticsearch API 리뷰]

원문 링크 : http://www.elasticsearch.org/guide/reference/api/admin-indices-put-mapping/


이 API 는 Mapping type  을 정의 할 수 있도록 합니다.


[Merging & Conflicts]

- 이미 존재 하는 field 에 다른 type 으로 mapping 시도 시 merge & conflict 동작을 하게 됩니다.

- ignore_conflicts 설정을 true 로 할 경우 다른 type 에 대한 conflict 에러를 회피 할 수 있습니다.

- 하지만, 정상적인 동작은 하지 않는다는 점에 주의를 하셔야 합니다.

kimchy said :

You can't change the type of a field mapped, even when ignore_conflicts is set to true. Ignore conflicts simply does that, ignore the conflicts and applies what it can, it does not apply conflicts.



※ 정리 하면,

- 기정의한 field type 에 대한 변경을 하지 않도록 유의 합니다.

- 만약 변경이 필요 하다면, object type 으로 신규 추가 하거나 multi field 를 활용 하시기 바랍니다.


: