pythonelasticsearchpyes

How to get a random document in pyes from elasticsearch


How do I use pyes to get a random document? I have a new cluster I have access to and want to explore

from pyes import *
conn = ES('127.0.0.1:9200', timeout=3.5)
#Now what?

Solution

  • For instance, you could match any document using MatchAllQuery and return a random one using start=xxx and size=1. Note that I've arbitrarily chosen 1000 to create a random number, but you can change this number to better match the number of documents you have in your index.

    import random
    from pyes import *
    conn = ES('127.0.0.1:9200', timeout=3.5)
    
    q = MatchAllQuery()
    rnd = random.randint(1, 1000)     <-- 1000 depends on how many docs you have
    docs = conn.search(Search(query=q, start=rnd, size=1))
    
    random_doc = docs['hits']['hits'][0] <-- your random document