-
Notifications
You must be signed in to change notification settings - Fork 800
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic, naive, implementation of Index class
For now usable mostly during creation and to group doc_types for search
- Loading branch information
Showing
4 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
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) |
This file contains 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
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} | ||
|
This file contains 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
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') |