Slowat_Kela
Slowat_Kela

Reputation: 1511

Limit the number of ElasticSearch outputs

I have this code; that can successfully do an ElasticSearch.

es = Elasticsearch()
index_name = 'db'
query_param = Q('bool', must = [Q('match_phrase', doc_text='electronic')])
search = Search(using=es, index="db").query(query_param)
result = search.execute()

The problem; when i print result, it only prints the top 10 hits (even though I know there are >500,000 entries in the db).

I've read the solution should be to use the 'size' parameter/or from and size. However, no matter where I insert 'size':50 into this code, or from and size parameters, I still can't change the number of outputs returned.

Could someone show me where in the code I can change the number of outputs returned? I want to limit the number based on the number of hits, not by kb or time or anything like that.

Thanks

Upvotes: 1

Views: 4533

Answers (2)

Slowat_Kela
Slowat_Kela

Reputation: 1511

So it turns out the answer is really simple, posting it here in case it helps anyone else (see the line where I've added in [0:50]).

    es = Elasticsearch()
    index_name = 'db'
    query_param = Q('bool', must = [Q('match_phrase', doc_text='electronic')])
    search = Search(using=es, index="db").query(query_param)[0:50]
    result = search.execute()

Upvotes: 3

fmdaboville
fmdaboville

Reputation: 1781

Look at this Elasticsearch python API, you have from_ and size parameters to perform your request.

Upvotes: 2

Related Questions