first base of project-changed apps: Herd-livestock-tag-log-elasticsearch-
This commit is contained in:
0
apps/search/api/__init__.py
Normal file
0
apps/search/api/__init__.py
Normal file
0
apps/search/api/v1/__init__.py
Normal file
0
apps/search/api/v1/__init__.py
Normal file
61
apps/search/api/v1/api.py
Normal file
61
apps/search/api/v1/api.py
Normal file
@@ -0,0 +1,61 @@
|
||||
from .serializers import UserRelationDocumentSerializer
|
||||
from django_elasticsearch_dsl_drf.viewsets import DocumentViewSet
|
||||
from rest_framework.pagination import LimitOffsetPagination
|
||||
from apps.search.document.user_document import UserRelationDocument
|
||||
from django.http.response import HttpResponse
|
||||
from rest_framework.response import Response
|
||||
from apps.authentication.models import User
|
||||
from rest_framework.views import APIView
|
||||
from elasticsearch_dsl.query import Q
|
||||
import abc
|
||||
|
||||
|
||||
class PaginatedElasticSearchApiView(APIView, LimitOffsetPagination):
|
||||
"""Base ApiView Class for elasticsearch views with pagination,
|
||||
Other ApiView classes should inherit from this class"""
|
||||
serializer_class = None
|
||||
document_class = None
|
||||
|
||||
@abc.abstractmethod
|
||||
def generate_q_expression(self, query):
|
||||
"""This method should be overridden
|
||||
and return a Q() expression."""
|
||||
|
||||
def get(self, request, query):
|
||||
try:
|
||||
q = self.generate_q_expression(query)
|
||||
search = self.document_class.search().query(q)
|
||||
response = search.execute()
|
||||
|
||||
print(f"Found {response.hits.total.value} hit(s) for query: '{query}'")
|
||||
|
||||
results = self.paginate_queryset(response, request, view=self) # noqa
|
||||
serializer = self.serializer_class(results, many=True)
|
||||
return self.get_paginated_response(serializer.data)
|
||||
except Exception as e:
|
||||
return HttpResponse(e, status=500)
|
||||
|
||||
|
||||
class SearchUserDocumentApiView(PaginatedElasticSearchApiView):
|
||||
"""Base ApiView Class for elasticsearch views with pagination,
|
||||
Other ApiView classes should inherit from this class"""
|
||||
serializer_class = UserRelationDocumentSerializer
|
||||
document_class = UserRelationDocument
|
||||
|
||||
def generate_q_expression(self, query):
|
||||
return Q(
|
||||
'bool',
|
||||
should=[
|
||||
Q("match", user__username=query),
|
||||
Q("match", user__mobile=query),
|
||||
Q("match", user__national_code=query),
|
||||
Q("match", organization__type__key=query),
|
||||
Q("match", organization__name=query),
|
||||
Q("match", organization__city__name=query),
|
||||
Q("match", organization__province__name=query),
|
||||
Q("match", organization__national_unique_id=query),
|
||||
Q("match", organization__company_code=query),
|
||||
Q("match", role__role_name=query),
|
||||
],
|
||||
minimum_should_match=1,
|
||||
)
|
||||
15
apps/search/api/v1/serializers.py
Normal file
15
apps/search/api/v1/serializers.py
Normal file
@@ -0,0 +1,15 @@
|
||||
from django_elasticsearch_dsl_drf.serializers import DocumentSerializer
|
||||
from apps.search.documents import UserRelationDocument
|
||||
|
||||
|
||||
class UserRelationDocumentSerializer(DocumentSerializer):
|
||||
"""Serializer for user relation document."""
|
||||
|
||||
class Meta:
|
||||
document = UserRelationDocument
|
||||
fields = (
|
||||
'id',
|
||||
'user',
|
||||
'organization',
|
||||
'role'
|
||||
)
|
||||
10
apps/search/api/v1/urls.py
Normal file
10
apps/search/api/v1/urls.py
Normal file
@@ -0,0 +1,10 @@
|
||||
from django.urls import path, include
|
||||
from .api import SearchUserDocumentApiView
|
||||
from rest_framework import routers
|
||||
|
||||
router = routers.DefaultRouter()
|
||||
|
||||
urlpatterns = [
|
||||
path('', include(router.urls), name='search_user'),
|
||||
path('user_elastic/<str:query>/', SearchUserDocumentApiView.as_view()),
|
||||
]
|
||||
0
apps/search/api/v1/views.py
Normal file
0
apps/search/api/v1/views.py
Normal file
0
apps/search/api/v2/__init__.py
Normal file
0
apps/search/api/v2/__init__.py
Normal file
0
apps/search/api/v2/serializers.py
Normal file
0
apps/search/api/v2/serializers.py
Normal file
0
apps/search/api/v2/urls.py
Normal file
0
apps/search/api/v2/urls.py
Normal file
0
apps/search/api/v2/views.py
Normal file
0
apps/search/api/v2/views.py
Normal file
Reference in New Issue
Block a user