Skip to content

Instantly share code, notes, and snippets.

@gnosek
Last active August 29, 2015 14:01
Show Gist options
  • Save gnosek/585a6717c570f4217168 to your computer and use it in GitHub Desktop.
Save gnosek/585a6717c570f4217168 to your computer and use it in GitHub Desktop.
Dynamic ElasticSearch mappings
MAPPING = {
'_default_': {
'_all': {'enabled': False},
'dynamic_templates': [
{
'analyzed_body': {
'match': 'body',
'match_mapping_type': 'string',
'mapping': {
'type': 'string',
'index': 'analyzed',
}
}
},
{
'default_not_analyzed': {
'match': '*',
'match_mapping_type': 'string',
'mapping': {
'type': 'string',
'index': 'not_analyzed',
}
}
},
]
}
}
_seen_indices = set()
def ensure_index(es_conn, index_format, now):
index = now.strftime(index_format)
if index in _seen_indices:
return index
_seen_indices.add(index)
es_conn.indices.create_index_if_missing(index, settings={'mappings': MAPPING})
return index
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment