How to properly Integrate Django rest framework with Haystack to make a GET REST API for searching query.

214 views
Skip to first unread message

bhupendra singh chauhan

unread,
Mar 20, 2017, 11:43:48 AM3/20/17
to django-haystack
   model.py

   
class Item(models.Model):
        name
=models.CharField(max_length=50)
        company
=models.CharField(max_length=100)



 search_indexes.py
   
   
   
class ItemIndex(indexes.SearchIndex, indexes.Indexable):
            text
= indexes.CharField(document=True, use_template=True)
            name
=indexes.CharField(model_attr='name')
            company
=indexes.CharField(model_attr='company')
           
           
def get_model(self):
           
return Item
           
           
def index_queryset(self, using=None):
           
return self.get_model().objects.all()


   serializer.py

   
 class ItemSearchSerializer(serializers.Serializer):
    text
= serializers.CharField()
    name
=serializers.CharField()
    company
=serializers.CharField()

     
views.py
    
   
  class ItemSearchViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):
    serializer_class
= ItemSearchSerializer  
    permission_classes
= (permissions.IsAuthenticatedOrReadOnly,)
   
   
def get_queryset(self):
    request
= self.request
    queryset
= EmptySearchQuerySet()
   
   
if request.GET.get('q', ''):
    query
= request.GET.get('q', '')
    queryset
=SearchQuerySet().filter(content=query);
           
return queryset

   
And in url.py I added :

   
router.register(r'searchquery', views.ItemSearchViewSet, base_name='searchquery')



 

 

Now on making GET request from postman like :
 http://127.0.0.1:8000/searchquery/?q=app, I am getting the response as desired as show below.
   
{
     
"count": 2,
     
"next": null,
     
"previous": null,
     
"results": [
       
{
         
"text": "apple\ndjflkj",
         
"id": 14,
         
"name": "apple",
         
"mrp": "45.000000",
         
"company": "djflkj",
         
"imageid": "jfhi",
         
"edible": false,
         
"discount": "0.000000",
         
"deliverable": true,
         
"seller_uid": "ljhkh",
         
"category": "ldjhgjfdk"
       
},
       
{
         
"text": "app\nhuhiu",
         
"id": 16,
         
"name": "app",
         
"mrp": "78.000000",
         
"company": "huhiu",
         
"imageid": "iyiugiy",
         
"edible": false,
         
"discount": "45.000000",
         
"deliverable": true,
         
"seller_uid": "hjh",
         
"category": "hhl"
       
}
     
]
   
}


But the reponse time is very slow it takes around 2700 ms everytime ,and I want to make it fast. As response of elastic search is much fast
but I don't know what I am doing wrong. Not sure but may be due to these reasons I am getting this delay :
1) Haystack is made for django, so on integrating it with django rest framework , it may be getting slow.
2) I am using free Bonsai Elastic search heroku add on and it has just 125 mb memory.

 
This is how I am connecting to Bonsai elastic search (setting.py)

   
 ES_URL = urlparse('https://******@pine-1455731.us-east1.bonsaisearch.net')
   
print ES_URL
    HAYSTACK_CONNECTIONS
= {
       
'default': {
           
'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
           
'URL': ES_URL.scheme + '://' + ES_URL.hostname + ':443',
           
'INDEX_NAME': 'haystack',
       
},
   
}
   
   
if ES_URL.username:
        HAYSTACK_CONNECTIONS
['default']['KWARGS'] = {"http_auth": ES_URL.username + ':' + ES_URL.password}




     
Any help will be appreciated. I am new to elastic search and haystack . I want to do elastic search to search products by name for my android application.
I even don't know weather this is the correct approach to do searching. I thought I would enter name of product I want to search and then i will send a GET request and get all the products which are related.    

If any one could suggest me any other way of achieving this I will appreciate your help.  


Steve Schwarz

unread,
Mar 23, 2017, 11:49:55 AM3/23/17
to django-...@googlegroups.com
Hi,
It seems weird that DRF or Django would be taking several seconds to process the request it should be 10's of ms or at most 100ms. Have you tried using wget or curl to test how long bonsaisearch takes? I'd do that first.


--
You received this message because you are subscribed to the Google Groups "django-haystack" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-haystack+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Reply all
Reply to author
Forward
0 new messages