Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

elasticsearch-dsl using from and size

I'm using python 2.7 with Elasticsearch-DSL package to query my elastic cluster.

Trying to add "from and limit" capabilities to the query in order to have pagination in my FE which presents the documents elastic returns but 'from' doesn't work right (i.e. I'm not using it correctly I spouse).

The relevant code is:

s = Search(using=elastic_conn, index='my_index'). \
       filter("terms", organization_id=org_list)

    hits = s[my_from:my_size].execute() # if from = 10, size = 10 then I get 0 documents, altought 100 documents match the filters.

My index contains 100 documents. even when my filter match all results (i.e nothing is filtered out), if I use my_from = 10 and my_size = 10, for instance, then I get nothing in hits (no matching documents)

Why is that? Am I misusing the from?

Documentation states:

from and size parameters. The from parameter defines the offset from the first result you want to fetch. The size parameter allows you to configure the maximum amount of hits to be returned.

So it seems really straightforward, what am I missing?

like image 888
Noam Avatar asked Nov 25 '18 08:11

Noam


2 Answers

The answer to this question can be found in their documentation under the Pagination Section of the Search DSL:

Pagination

To specify the from/size parameters, use the Python slicing API:

s = s[10:20]

# {"from": 10, "size": 10}

The correct usage of these parameters with the Search DSL is just as you would with a Python list, slicing from the starting index to the end index. The size parameter would be implicitly defined as the end index minus the start index.

Hope this clears things up!

like image 85
Dalofeco Avatar answered Oct 06 '22 12:10

Dalofeco


Try to pass from and size params as below:

search = Search(using=elastic_conn, index='my_index'). \
        filter("terms", organization_id=org_list). \
        extra(from_=10, size=20)
result = search.execute()
like image 4
Aida.Mirabadi Avatar answered Oct 06 '22 13:10

Aida.Mirabadi