2015-07-20 35 views

回答

2

例如,你可以使用匹配任何MatchAllQuery文檔並返回使用start=xxxsize=1隨機一個。請注意,我已經任意選擇1000來創建一個隨機數字,但是您可以更改此數字以更好地匹配您在索引中擁有的文檔數量。

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 
相關問題