Range request in Elasticsearch_dsl by integer field

I used elasticsearch-dsl==5.2.0, elasticsearch==5.3.0and Django==1.8.15.

Django Model :

class Item(models.Model):
    price = models.DecimalField(default=0)

    def to_search(self):
        return DocItem(
            meta={'id': self.id},
            price=self.price
        )

DocType Class :

class DocItem(DocType):
    price = Integer()

FacetedSearch Class :

class ItemSearch(FacetedSearch):
    index = 'item'
    doc_types = [DocItem, ]
    fields = ['price']

When I need to search all the elements with price == 5.0, I do the following:

search = ItemSearch().search()
result = search.filter('match', price=5.0).execute()

Question

How can I search for all products with a price in the range: 1.0 <price <= 5.0?

+4
source share
1 answer

You can do it as follows:

search = ItemSearch().search()
result = search.filter('range', price={'gt': 1, 'lte': 5.0}).execute()
+3
source

Source: https://habr.com/ru/post/1674656/


All Articles