Skip to content

Commit

Permalink
Basic, naive, implementation of Index class
Browse files Browse the repository at this point in the history
For now usable mostly during creation and to group doc_types for search
  • Loading branch information
honzakral committed Feb 11, 2015
1 parent 4a405ed commit 3642187
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 0 deletions.
1 change: 1 addition & 0 deletions elasticsearch_dsl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from .field import *
from .document import DocType
from .mapping import Mapping
from .index import Index

VERSION = (0, 0, 4, 'dev')
__version__ = VERSION
Expand Down
55 changes: 55 additions & 0 deletions elasticsearch_dsl/index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from .connections import connections
from .search import Search

class Index(object):
def __init__(self, name, using='default'):
self._name = name
self._doc_types = {}
self._mappings = {}
self._using = 'default'
self._settings = {}

def _get_connection(self):
return connections.get_connection(self._using)
connection = property(_get_connection)

def doc_type(self, doc_type):
name = doc_type._doc_type.name
self._doc_types[name] = doc_type
self._mappings[name] = doc_type._doc_type.mapping

if not doc_type._doc_type.index:
doc_type._doc_type.index = self._name
return doc_type # to use as decorator???

def settings(self, **kwargs):
self._settings.update(kwargs)
return self

def search(self):
return Search(
using=self._using,
index=self._name,
doc_type=[self._doc_types.get(k, k) for k in self._mappings]
)

def _get_mappings(self):
mappings = {}
for mapping in self._mappings.values():
mappings.update(mapping.to_dict())
return mappings

def to_dict(self):
out = {}
if self._settings:
out['settings'] = self._settings
mappings = self._get_mappings()
if mappings:
out['mappings'] = mappings
return out

def create(self):
self.connection.indices.create(index=self._name, body=self.to_dict())

def delete(self):
self.connection.indices.delete(index=self._name)
49 changes: 49 additions & 0 deletions test_elasticsearch_dsl/test_index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from elasticsearch_dsl import DocType, Index, String, Date

class Post(DocType):
title = String()
published_from = Date()


def test_search_is_limited_to_index_name():
i = Index('my-index')
s = i.search()

assert s._index == ['my-index']

def test_settings_are_saved():
i = Index('i')
i.settings(number_of_replicas=0)
i.settings(number_of_shards=1)

assert {
'settings': {
'number_of_shards': 1,
'number_of_replicas': 0,
}
} == i.to_dict()

def test_registered_doc_type_included_in_to_dict():
i = Index('i', using='alias')
i.doc_type(Post)

assert Post._doc_type.index == 'i'
assert {
'mappings': {
'post': {
'properties': {
'title': {'type': 'string'},
'published_from': {'type': 'date'},
}
}
}
} == i.to_dict()

def test_registered_doc_type_included_in_search():
i = Index('i', using='alias')
i.doc_type(Post)

s = i.search()

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

49 changes: 49 additions & 0 deletions test_elasticsearch_dsl/test_integration/test_index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from elasticsearch_dsl import DocType, Index, String, Date

class Post(DocType):
title = String()
published_from = Date()

class User(DocType):
username = String(index='not_analyzed')
joined_date = Date()

def test_index_can_be_created_with_settings_and_mappings(write_client):
i = Index('test-blog', using=write_client)
i.doc_type(Post)
i.doc_type(User)
i.settings(number_of_replicas=0, number_of_shards=1)
i.create()

assert {
'test-blog': {
'mappings': {
'post': {
'properties': {
'title': {'type': 'string'},
'published_from': {'type': 'date', 'format': 'dateOptionalTime',},
}
},
'user': {
'properties': {
'username': {'type': 'string', 'index': 'not_analyzed'},
'joined_date': {'type': 'date', 'format': 'dateOptionalTime',},
}
},
}
}
} == write_client.indices.get_mapping(index='test-blog')

settings = write_client.indices.get_settings(index='test-blog')
assert settings['test-blog']['settings']['index']['number_of_replicas'] == '0'
assert settings['test-blog']['settings']['index']['number_of_shards'] == '1'

def test_delete(write_client):
write_client.indices.create(
index='test-index',
body={'settings': {'number_of_replicas': 0, 'number_of_shards': 1}}
)

i = Index('test-index', using=write_client)
i.delete()
assert not write_client.indices.exists(index='test-index')

0 comments on commit 3642187

Please sign in to comment.