12

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 ?

tarasinf
  • 796
  • 5
  • 17

1 Answers1

18

You can do it like this:

search = ItemSearch().search()
result = search.filter('range', price={'gt': 1, 'lte': 5.0}).execute()
Val
  • 207,596
  • 13
  • 358
  • 360
  • 1
    Where I can read about existing filter types, like `'range'` and other params like `'gt'`, `'lte'`? What is it the syntax? – tarasinf Apr 12 '17 at 11:50
  • Also you can see the full query DSL in the official documentation at https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html – Val Apr 12 '17 at 12:08
  • 1
    How to query all items that price are in the array, like: `price__in=[30, 10, 20, ...]`? – tarasinf Apr 12 '17 at 14:01
  • 2
    That should be a different question in my opinion – Val Apr 12 '17 at 14:04
  • For a question in the commens about how to query items in an array you can use 'terms', see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html – acaruci Jul 22 '20 at 17:44
  • how can we specify size here – lazarus Dec 04 '20 at 11:50
  • 1
    @lazarus you can find the pagination doc [here](https://elasticsearch-dsl.readthedocs.io/en/latest/search_dsl.html#pagination) – Val Dec 04 '20 at 12:02