I am using elasticsearch with django rest framework. I am using this lib: https://github.com/barseghyanartur/django-elasticsearch-dsl-drf/
I am trying to filter by price range according to these docs: https://github.com/barseghyanartur/django-elasticsearch-dsl-drf/
This is my views:
class TestAPIView(DocumentViewSet):
document = TestDocument
serializer_class = TestSerializer
queryset = TestModel.objects.all()
filter_backends = [
FilteringFilterBackend
]
filter_fields = {
'price': {
'field': 'price',
'lookups': [
LOOKUP_FILTER_RANGE,
LOOKUP_QUERY_IN,
],
},
}
and this is my document.py
file
@registry.register_document
class TestDocument(Document):
price = fields.IntegerField(attr='price')
class Index:
name = 'TestModel'
settings = {
'number_of_shards': 1,
'number_of_replicas': 0,
}
class Django:
model = TestModel
fields = [
'id',
]
When I hit on browser this url: http://127.0.0.1:8000/search/api/v1/test-model/?price=12
It works very well, even when I try with this url : http://127.0.0.1:8000/search/api/v1/test-model/?price=55
it works,
I am facing a problem to filter by range, like I want to filter price 12
to price 90
, in this case how can I pass query parameter for the range? can anyone help me in this case?
CodePudding user response:
The source code [GitHub] provides an example for this:
# Example: {"query": {"range": {"age": {"gte": "16", "lte": "67"}}}} # Example: http://localhost:8000/api/users/?age__range=16__67
You thus can filter with:
http://127.0.0.1:8000/search/api/v1/test-model/?price__range=12__90
to retrieve items with a price between 12 and 90.