testdrivenio / django-drf-elasticsearch Goto Github PK
View Code? Open in Web Editor NEWDjango REST Framework and Elasticsearch
License: MIT License
Django REST Framework and Elasticsearch
License: MIT License
django-drf-elasticsearch/search/views.py
Line 27 in ac9eb0b
response.hits.total.value
for some random string didn't give me the right total of items found which was 10000, on another hand the search.count()
gave me the 23000 and I could check that the amount of 23000 was right because I made a loop using the scan() method it iterates 23000. What do you think about it?Am embedding elastic search in my application, though when I run it I get this error :
raise TypeError("Unable to serialize %r (type: %s)" % (data, type(data)))
TypeError: Unable to serialize <QuerySet [<AccountTypes: MENTOR>]> (type: <class 'django.db.models.query.QuerySet'>)
elasticsearch.exceptions.SerializationError: ({'owner': {'id': 2, 'email': '[email protected]', 'username': 'huxy', 'auth_provider': 'email', 'account_types': <QuerySet [<AccountTypes: MENTOR>]>, 'profile': <Profile: None Idris>, 'uuid': UUID('d4a5cae3-44ad-49c0-bf89-cc5f4d993667'), 'status': 'PENDING'}, 'tags': [], 'comments': [], 'grade_level': {}, 'title': 'Math is fun', 'body': 'Math is nice, now I like it so much, this is awesom', 'image': '', 'status': 'DRAFT'}, TypeError("Unable to serialize <QuerySet [<AccountTypes: MENTOR>]> (type: <class 'django.db.models.query.QuerySet'>)"))
What could be the one thing that am missing, below is my code snippets.
This is my model for user :
class User(MainProcess, django_models.AbstractBaseUser, TimeStampedModel,
django_models.PermissionsMixin):
"""
User model for the user creation
"""
uuid = models.UUIDField(unique=True, max_length=500,
default=uuid.uuid4,
editable=False,
db_index=True, blank=False, null=False)
email = models.EmailField(_('Email'), db_index=True, unique=True)
is_verified = models.BooleanField(_('Is verified'), default=False)
is_staff = models.BooleanField(_('Is staff'), default=True)
is_active = models.BooleanField(_('Is Active'), default=True)
username = models.CharField(_('Username'), max_length=255,
blank=True, null=True)
account_types = models.ManyToManyField(AccountTypes,
related_name='account_types')
auth_provider = models.CharField(
max_length=255, blank=False,
null=False, default=AUTH_PROVIDERS.get('email'))
status = models.CharField(
choices=UserProcess.states,
default=PENDING, max_length=100, blank=True)
objects = UserManager()
USERNAME_FIELD = 'email'
class Meta:
verbose_name = 'User'
verbose_name_plural = 'Users'
Then this is a Post model :
class Post(MainProcess, TimeStampedModel, models.Model):
"""Post model."""
title = models.CharField(_('Title'), max_length=100, blank=False,
null=False)
image = models.ImageField(_('Image'), upload_to='blog_images', null=True,
max_length=900)
body = models.TextField(_('Body'), blank=False)
description = models.CharField(_('Description'), max_length=400,
blank=True, null=True)
slug = models.SlugField(default=uuid.uuid4(), unique=True, max_length=100)
owner = models.ForeignKey(User, related_name='posts',
on_delete=models.CASCADE)
bookmarks = models.ManyToManyField(User, related_name='bookmarks',
default=None, blank=True)
address_views = models.ManyToManyField(CustomIPAddress,
related_name='address_views',
default=None, blank=True)
likes = models.ManyToManyField(User, related_name='likes', default=None,
blank=True,
)
Then my documents.py file :
@registry.register_document
class UserDocument(Document):
"""User Document."""
account_types = fields.ObjectField(properties={
'id': fields.IntegerField(),
'uuid': fields.TextField(),
'name': fields.TextField(),
})
class Index:
name = 'users'
settings = {
'number_of_shards': 1,
'number_of_replicas': 0,
}
class Django:
model = User
fields = [
'id',
'email',
'username',
'status',
'auth_provider',
]
@registry.register_document
class TagDocument(Document):
"""Tag Document"""
class Index:
name = 'tags'
settings = {
'number_of_shards': 1,
'number_of_replicas': 0,
}
class Django:
model = Tag
fields = [
'name',
'description',
# 'color_code',
# 'has_followed',
]
@registry.register_document
class CommentDocument(Document):
"""Comment Document"""
owner = fields.ObjectField(properties={
'id': fields.IntegerField(),
'email': fields.TextField(),
'username': fields.TextField(),
'auth_provider': fields.TextField(),
'account_types': fields.TextField(),
'profile': fields.TextField(),
'uuid': fields.TextField(),
'status': fields.TextField(),
})
post = fields.ObjectField(properties={
'id': fields.IntegerField(),
})
class Index:
name = 'comments'
settings = {
'number_of_shards': 1,
'number_of_replicas': 0,
}
class Django:
model = Comment
fields = [
'id',
'body',
# 'created',
# 'modified',
]
@registry.register_document
class GradeLevel(Document):
"""Grade Level Document."""
class Index:
name = 'grades'
settings = {
'number_of_shards': 1,
'number_of_replicas': 0,
}
class Django:
model = ClassGrade
fields = [
'id',
'grade',
# 'country',
# 'color_code',
]
@registry.register_document
class BlogDocument(Document):
"""BlogDocument"""
owner = fields.ObjectField(properties={
'id': fields.IntegerField(),
'email': fields.TextField(),
'username': fields.TextField(),
'auth_provider': fields.TextField(),
'account_types': fields.TextField(),
'profile': fields.TextField(),
'uuid': fields.TextField(),
'status': fields.TextField(),
})
tags = fields.ObjectField(
properties={
'id': fields.IntegerField(),
'email': fields.TextField(),
'username': fields.TextField(),
'auth_provider': fields.TextField(),
'account_types': fields.TextField(),
'profile': fields.TextField(),
'uuid': fields.TextField(),
'status': fields.TextField(),
})
comments = fields.ObjectField(
properties={
'id': fields.IntegerField(),
'body': fields.TextField(),
'owner': fields.TextField(),
'post': fields.TextField(),
'created': fields.TextField(),
'modified': fields.TextField(),
})
grade_level = fields.ObjectField(
properties={
'id': fields.IntegerField(),
'grade': fields.TextField(),
'country': fields.TextField(),
'color_code': fields.TextField(),
}
)
class Index:
name = 'blogs'
settings = {
'number_of_shards': 1,
'number_of_replicas': 0,
}
# TODO: Add pending fields in each class :(
class Django:
model = Post
fields = [
'title',
'body',
'image',
'status',
# 'modified',
]
I am not able to start the search I am getting below error
CommandError: 'search' conflicts with the name of an existing Python module and cannot be used as an app name. Please try another name.
Also, how to populate new data into the database?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.