Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Range query in Elasticsearch_dsl by integer field

I used elasticsearch-dsl==5.2.0, elasticsearch==5.3.0 and 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 items with price == 5.0, I do the next:

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

Question:

How can I search all items with price in range: 1.0 < price <= 5.0 ?

like image 374
tarasinf Avatar asked Apr 12 '17 11:04

tarasinf


1 Answers

You can do it like this:

search = ItemSearch().search()
result = search.filter('range', price={'gt': 1, 'lte': 5.0}).execute()
like image 76
Val Avatar answered Nov 13 '22 00:11

Val