Skip to content

Commit

Permalink
updated to python3 and elasticsearch 6.x, added method search_queryset
Browse files Browse the repository at this point in the history
  • Loading branch information
milashensky committed Jan 18, 2018
1 parent 302d8d0 commit 58bbca6
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 51 deletions.
20 changes: 11 additions & 9 deletions django_elasticsearch/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
u'FloatField': 'double',
u'IntegerField': 'long',
u'PositiveIntegerField': 'long',
u'PositiveSmallIntegerField': 'short',
u'PositiveSmallIntegerField': 'long',
u'SmallIntegerField': 'short',

u'ForeignKey': 'object',
Expand Down Expand Up @@ -68,15 +68,15 @@ def __init__(self, k):
self._mapping = None

def get_index(self):
return self.model.Elasticsearch.index
index = self.model.Elasticsearch.index + '-' + self.doc_type
return index.lower()

@property
def index(self):
return self.get_index()

def get_doc_type(self):
return (self.model.Elasticsearch.doc_type
or 'model-{0}'.format(self.model.__name__))
return (self.model.Elasticsearch.doc_type or '{meta.app_label}-{meta.object_name}'.format(meta=self.model._meta))

@property
def doc_type(self):
Expand All @@ -87,7 +87,7 @@ def check_cluster(self):

def get_serializer(self, **kwargs):
serializer = self.model.Elasticsearch.serializer_class
if isinstance(serializer, basestring):
if isinstance(serializer, str):
module, kls = self.model.Elasticsearch.serializer_class.rsplit(".", 1)
mod = importlib.import_module(module)
return getattr(mod, kls)(self.model, **kwargs)
Expand Down Expand Up @@ -191,7 +191,6 @@ def search(self, query,
:arg suggest_limit
:arg fuzziness
"""

q = self.queryset
q.fuzziness = fuzziness

Expand All @@ -209,6 +208,10 @@ def search(self, query,

return q.query(query)

def search_queryset(self, query, **kwargs):
results = self.search(query, **kwargs)
return self.model.objects.filter(pk__in=[i.get('id', None) for i in results])

# Convenience methods
def all(self):
"""
Expand Down Expand Up @@ -261,11 +264,11 @@ def make_mapping(self):
mapping = {}
else:
mapping = {'type': ELASTICSEARCH_FIELD_MAP.get(
field.get_internal_type(), 'string')}
field.get_internal_type(), 'text')}
try:
# if an analyzer is set as default, use it.
# TODO: could be also tokenizer, filter, char_filter
if mapping['type'] == 'string':
if mapping['type'] == 'text':
analyzer = settings.ELASTICSEARCH_SETTINGS['analysis']['default']
mapping['analyzer'] = analyzer
except (ValueError, AttributeError, KeyError, TypeError):
Expand Down Expand Up @@ -332,7 +335,6 @@ def create_index(self, ignore=True):
body = {}
if hasattr(settings, 'ELASTICSEARCH_SETTINGS'):
body['settings'] = settings.ELASTICSEARCH_SETTINGS

es_client.indices.create(self.index,
body=body,
ignore=ignore and 400)
Expand Down
11 changes: 4 additions & 7 deletions django_elasticsearch/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,8 @@ def make_search_body(self):

if self._query:
search['query'] = {
'match': {
'_all': {
'query': self._query,
'fuzziness': fuzziness
}
'query_string': {
'query': self._query
},
}

Expand Down Expand Up @@ -350,7 +347,7 @@ def exclude(self, **kwargs):
clone.filters.update(filters)
return clone

## getters
# getters
def all(self):
clone = self._clone()
return clone
Expand Down Expand Up @@ -388,7 +385,7 @@ def complete(self, field_name, query):
"completion": {
"field": field_name,
# stick to fuzziness settings
"fuzzy" : {}
"fuzzy": {}
}}})

return [r['text'] for r in resp[field_name][0]['options']]
Expand Down
8 changes: 6 additions & 2 deletions django_elasticsearch/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,11 @@ def serialize_field(self, instance, field_name):
return self.nested_serialize(rel)

try:
return getattr(instance, field_name)
val = getattr(instance, field_name)
if field.get_internal_type() in ['CharField', 'TextField']:
return str(val)
else:
return val
except AttributeError:
raise AttributeError("The serializer doesn't know how to serialize {0}, "
"please provide it a {1} method."
Expand All @@ -145,7 +149,7 @@ def nested_serialize(self, rel):
return obj

# Fallback on a dict with id + __unicode__ value of the related model instance.
return dict(id=rel.pk, value=unicode(rel))
return dict(id=rel.pk, value=str(rel))

def format(self, instance):
# from a model instance to a dict
Expand Down
4 changes: 2 additions & 2 deletions django_elasticsearch/tests/test_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ def tearDown(self):

def test_serialize(self):
obj = self.instance.es.serialize()
self.assertTrue(isinstance(obj, basestring))
self.assertTrue(isinstance(obj, str))

@withattrs(Test2Model.Elasticsearch, 'serializer_class',
'django_elasticsearch.serializers.EsJsonSerializer')
def test_dynamic_serializer_import(self):
obj = self.instance.es.serialize()
self.assertTrue(isinstance(obj, basestring))
self.assertTrue(isinstance(obj, str))

def test_deserialize(self):
instance = Test2Model.es.deserialize({'char': 'test'})
Expand Down
34 changes: 3 additions & 31 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,5 @@
django_elasticsearch is a wrapper around py-elasticsearch that automates the indexation and search of django models.
**Note**: if your elasticsearch documents/mappings are not close to django models, this package is probably not for you.
Updating of [liberation/django_elasticsearch.git](https://github.com/liberation/django_elasticsearch.git) for python3 and elasticsearch 6.x versions

INSTALL
=======
* [Install and launch elasticsearch](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/setup.html) if it's not done already.

* Install [py-elasticsearch](http://www.elasticsearch.org/guide/en/elasticsearch/client/python-api/current/)
```shell
pip install elasticsearch
```

* Install django_elasticsearch
```shell
pip install git+https://github.com/liberation/django_elasticsearch.git
```
**Note**: no pypy package yet


ELASTICSEARCH VERSION COMPATIBILITY
===================================

As stated in the python elasticsearch module documentation:


>There are two branches for development - master and 0.4. Master branch is used to track all the changes for Elasticsearch 1.0 and beyond whereas 0.4 tracks Elasticsearch 0.90.
>
>Releases with major version 1 (1.X.Y) are to be used with Elasticsearch 1.* and later, 0.4 releases are meant to work with Elasticsearch 0.90.*.

django_elasticsearch has only been tested with Elasticsearch 1.3.9 and it's corresponding python interface version 1.2.0, but since [the API hasn't change](https://elasticsearch-py.readthedocs.org/en/master/Changelog.html) i'm quite positive that newer and older versions should work fine too, as long as you use the right python module for your Elasticsearch version. [See the official docs on the matter](https://elasticsearch-py.readthedocs.org/en/master/#compatibility).

USAGE
=====
Expand Down Expand Up @@ -119,12 +91,12 @@ Each EsIndexable model receive an Elasticsearch class that contains its options
Defaults to None
You can override some or all of the fields mapping with this dictionary
Example:

```python

MyModel(EsIndexable, models.Model):
title = models.CharField(max_length=64)

class Elasticsearch(EsIndexable.Elasticsearch):
mappings = {'title': {'boost': 2.0}}
```
Expand Down

0 comments on commit 58bbca6

Please sign in to comment.