Using Dynamic Templates

Hey there, i am a beginner and want to use a dynamic template
i wanted to test dynamic templates by using the example in the documentation.

https://www.elastic.co/guide/en/elasticsearch/reference/7.0/dynamic-templates.html

PUT my_index
{
"mappings": {
"dynamic_templates": [
{
"longs_as_strings": {
"match_mapping_type": "string",
"match": "long_",
"unmatch": "
_text",
"mapping": {
"type": "long"
}
}
}
]
}
}

PUT my_index/_doc/1
{
"name": {
"first": "Alice",
"middle": "Mary",
"last": "White"
}
}

GET my_index/_search
{
"query": { "match_all": {} }
}

This is the result for my query:

{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : {
"first" : "Alice",
"middle" : "Mary",
"last" : "White"
}
}
}
]
}
}

My question is shouldnt there be a field "full_name" containing the information "Alice" and "White" ??

Anything that you configure in your mappings does not change the actual documents. It only changes how Elasticsearch will treat the documents. So, none of it impacts the _source that Elasticsearch returns.

In your example, Elasticsearch will create an inverted index for a field full_name and it will copy the values of name.first and name.last to that inverted index, so you can query that field, even though that field does not actually exist in your document. You can think of full_name as a "virtual field". For example, the following query works as a result of your dynamic template:

GET my_index/_search
{
  "query": {
    "match": {
      "full_name": "alice"
    }
  }
}

If you want to change your actual documents, take a look at ingest pipelines.

Ah okay.
Thanks a lot for the information. Yes i want to actual change the document so i am going to
take a look at ingest pipelines.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.