Skip to content

Commit

Permalink
Added 'aliases' support to Index
Browse files Browse the repository at this point in the history
  • Loading branch information
Liam Costello authored and honzakral committed May 14, 2015
1 parent 7683c6c commit d2ddfa6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
5 changes: 5 additions & 0 deletions docs/persistence.rst
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,11 @@ in a migration:
number_of_replicas=0
)
# define aliases
blogs.aliases(
old_blogs={}
)
# register a doc_type with the index
blogs.doc_type(Post)
Expand Down
7 changes: 7 additions & 0 deletions elasticsearch_dsl/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def __init__(self, name, using='default'):
self._mappings = {}
self._using = using
self._settings = {}
self._aliases = {}

def _get_connection(self):
return connections.get_connection(self._using)
Expand All @@ -26,6 +27,10 @@ def settings(self, **kwargs):
self._settings.update(kwargs)
return self

def aliases(self, **kwargs):
self._aliases.update(kwargs)
return self

def search(self):
return Search(
using=self._using,
Expand All @@ -49,6 +54,8 @@ def to_dict(self):
out = {}
if self._settings:
out['settings'] = self._settings
if self._aliases:
out['aliases'] = self._aliases
mappings, analysis = self._get_mappings()
if mappings:
out['mappings'] = mappings
Expand Down
25 changes: 24 additions & 1 deletion test_elasticsearch_dsl/test_index.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from elasticsearch_dsl import DocType, Index, String, Date

from random import choice

import string

class Post(DocType):
title = String()
published_from = Date()
Expand Down Expand Up @@ -35,7 +39,7 @@ def test_registered_doc_type_included_in_to_dict():
'title': {'type': 'string'},
'published_from': {'type': 'date'},
}
}
}
}
} == i.to_dict()

Expand All @@ -47,3 +51,22 @@ def test_registered_doc_type_included_in_search():

assert s._doc_type_map == {'post': Post.from_es}


def test_aliases_add_to_object():
random_alias = ''.join((choice(string.ascii_letters) for _ in range(100)))
alias_dict = {random_alias: {}}

index = Index('i', using='alias')
index.aliases(**alias_dict)

assert index._aliases == alias_dict


def test_aliases_returned_from_to_dict():
random_alias = ''.join((choice(string.ascii_letters) for _ in range(100)))
alias_dict = {random_alias: {}}

index = Index('i', using='alias')
index.aliases(**alias_dict)

assert index._aliases == index.to_dict()['aliases'] == alias_dict

0 comments on commit d2ddfa6

Please sign in to comment.