Created
May 8, 2019 21:19
-
-
Save sponomarev/1b50484a8aeae9e01b39564e28721001 to your computer and use it in GitHub Desktop.
Multi-tenand ES data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DELETE users | |
DELETE users_1_dedicated | |
# Create users index template | |
PUT _template/users | |
{ | |
"index_patterns": ["users*"], | |
"settings": { | |
"number_of_shards": 1, | |
"number_of_replicas": 0 | |
}, | |
"mappings": { | |
"_doc": { | |
"properties": { | |
"name": { | |
"type": "text" | |
}, | |
"school_id": { | |
"type": "integer" | |
} | |
} | |
} | |
} | |
} | |
GET _template/users | |
# Create global index | |
PUT users | |
# All settings are applied from the template | |
GET users | |
# Create tenant aliases | |
POST /_aliases | |
{ | |
"actions": [ | |
{ | |
"add": { | |
"index": "users", | |
"alias": "users_1", | |
"filter": { | |
"term": { | |
"school_id": 1 | |
} | |
} | |
} | |
}, | |
{ | |
"add": { | |
"index": "users", | |
"alias": "users_2", | |
"filter": { | |
"term": { | |
"school_id": 2 | |
} | |
} | |
} | |
}, | |
{ | |
"add": { | |
"index": "users", | |
"alias": "users_3", | |
"filter": { | |
"term": { | |
"school_id": 3 | |
} | |
} | |
} | |
} | |
] | |
} | |
# Aliases were applied | |
GET /users | |
# Write directly to the global index | |
PUT /users/_doc/1 | |
{ | |
"name": "Vova", | |
"school_id": "1" | |
} | |
PUT /users/_doc/2 | |
{ | |
"name": "Sergey", | |
"school_id": "1" | |
} | |
PUT /users/_doc/3 | |
{ | |
"name": "Nick", | |
"school_id": "2" | |
} | |
# Records are available in global index | |
GET /users/_search | |
# Check that tenant aliases work for reading | |
GET /users_1/_search | |
GET /users_2/_search | |
GET /users_3/_search | |
# Write to the tenant alias | |
PUT /users_1/_doc/4 | |
{ | |
"name": "Alex", | |
"school_id": "1" | |
} | |
# Check that tenant aliases work for writing | |
GET /users_1/_search | |
# Migrate a fat tenant to the dedicated index | |
# Create dedicated tenant index | |
PUT users_1_dedicated | |
# Create tenant aliases, this operation is atomic | |
# We don't need filter here since we assume that only one tenant would be there | |
POST /_aliases | |
{ | |
"actions": [ | |
{ | |
"add": { | |
"index": "users_1_dedicated", | |
"alias": "users_1", | |
"is_write_index" : true | |
} | |
} | |
] | |
} | |
# Write to the tenant alias | |
PUT /users_1/_doc/5 | |
{ | |
"name": "Tanira", | |
"school_id": "1" | |
} | |
# Check that Tanira is available from the tenant alias. Pay attention about the record index. | |
GET /users_1/_search | |
# The dedicated index contains new records only | |
GET /users_1_dedicated/_search | |
# Copy tenant data from the global index to the dedicated one | |
POST _reindex | |
{ | |
"source": { | |
"index": "users", | |
"query": { | |
"term": { | |
"school_id": "1" | |
} | |
} | |
}, | |
"dest": { | |
"index": "users_1_dedicated" | |
} | |
} | |
# Check that reindex task is done | |
GET _tasks?detailed=true&actions=*reindex | |
# All tenant data is copied | |
GET /users_1_dedicated/_search | |
# Copies are presented | |
GET /users_1/_search | |
# Remove tenant data from the global index | |
POST users/_delete_by_query | |
{ | |
"query": { | |
"term": { | |
"school_id": "1" | |
} | |
} | |
} | |
# Copies are destroyed | |
GET /users_1/_search | |
# No tenand data in the global index | |
GET /users/_search | |
{ | |
"query": { | |
"term": { | |
"school_id": "1" | |
} | |
} | |
} | |
# We can remove global index from the tenant alias | |
POST /_aliases | |
{ | |
"actions": [ | |
{ | |
"remove": { | |
"index": "users", | |
"alias": "users_1" | |
} | |
} | |
] | |
} | |
GET _alias/users* | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment