2

I have a situation where I'll need to import a bunch of different data that may end up having conflicting data types. I have decided to convert everything to a string and then convert back later if the data is needed. I can't figure out how to do this with Elasticsearches (ES) dynamic mapping using the javascript client.

What ES says in their docs:

{
    "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"
                      }
                }}
            ]
}}}

in their docs it says " Match string fields whose name ends in _es".
"Match all other string fields": https://www.elastic.co/guide/en/elasticsearch/guide/current/custom-dynamic-mapping.html

This is what I've tried, but doesn't convert all to string (also tried without quotes around wildcard):

event.mappings = {
        "mytype": {
            "match": "*",
            "mapping": {
                "type": "string"
            }
        }
    }

I've also tried "match_mapping_type" : "*".

I've tried: esClient.indices.putMapping({index:"myindex", type:"mytype", body:mybody}) in the response and outside of the .create function. Any tips?

benishky
  • 901
  • 1
  • 11
  • 23

1 Answers1

0

Your mapping should look like this

PUT /test
{
  "mappings": {
    "test": {
      "dynamic_templates": [
        {
          "en": {
            "match": "*",
            "mapping": {
              "type": "string"
            }
          }
        }
      ]
    }
  }
}

Test data:

POST /test/test/1
{
  "nr": 1,
  "jsonDate":"2015-06-08T03:41:12-05:00",
  "bool": true
}

The resulting mapping, as seen by ES:

{
   "test": {
      "mappings": {
         "test": {
            "dynamic_templates": [
               {
                  "en": {
                     "mapping": {
                        "type": "string"
                     },
                     "match": "*"
                  }
               }
            ],
            "properties": {
               "bool": {
                  "type": "string"
               },
               "jsonDate": {
                  "type": "string"
               },
               "nr": {
                  "type": "string"
               }
            }
         }
      }
   }
}
Andrei Stefan
  • 51,654
  • 6
  • 98
  • 89
  • Thank you @Andrei Stefan. I am using the ES javascript client, which is part of the issue. I forgot to mention this in my post. I've tried adding: esClient.indices.putMapping({index:"myindex", type:"mytype", body:mybody}); in the response and directly after the create function, but still doesn't work. – benishky Oct 21 '15 at 14:20
  • I can help you from ES point of view, haven't used the javascript client, though. – Andrei Stefan Oct 21 '15 at 14:41