我正在將 elasticsearch 與 django rest 框架一起使用。我正在使用這個庫: https ://github.com/barseghyanartur/django-elasticsearch-dsl-drf/
我正在嘗試根據這些檔案按價格范圍進行過濾:https ://github.com/barseghyartur/django-elasticsearch-dsl-drf/
這是我的看法:
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,
],
},
}
這是我的document.py檔案
@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',
]
當我在瀏覽器上點擊這個網址時:http://127.0.0.1:8000/search/api/v1/test-model/?price=12它作業得很好,即使我嘗試使用這個網址:http://127.0.0.1:8000/search/api/v1/test-model/?price=55它也有效,
我在按范圍過濾時遇到問題,例如我想將價格過濾12為價格90,在這種情況下,如何傳遞范圍的查詢引數?在這種情況下誰能幫助我?
uj5u.com熱心網友回復:
源代碼 [GitHub]為此提供了一個示例:
# Example: {"query": {"range": {"age": {"gte": "16", "lte": "67"}}}} # Example: http://localhost:8000/api/users/?age__range=16__67
因此,您可以過濾:
http://127.0.0.1:8000/search/api/v1/test-model/? 價格__范圍=12__90
檢索價格在 12 到 90 之間的專案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/485984.html
標籤:django 弹性搜索 django-rest-framework
