Skip to content

Instantly share code, notes, and snippets.

@wubin1989
Forked from bkersbergen/get_random_documents.py
Created January 5, 2017 07:18
Show Gist options
  • Save wubin1989/14d9387f63ecf9e27c8f2396ea49e74c to your computer and use it in GitHub Desktop.
Save wubin1989/14d9387f63ecf9e27c8f2396ea49e74c to your computer and use it in GitHub Desktop.
get some random documents from elasticsearch
from elasticsearch import Elasticsearch
es = Elasticsearch(["node001.bol.com", "node002.bol.com"],
sniff_on_start=True,
# refresh nodes after a node fails to respond
sniff_on_connection_fail=True,
# and also every xxx seconds
sniffer_timeout=300
)
# retrieve 2 random documents from index
res = es.search(index='barrie_cat_reco', doc_type='recommendation', size=2, body={"query": {"match_all": {}}, "sort": {
"_script" : {
"script" : "Math.random()",
"lang" : "groovy",
"type" : "number",
"params" : {}
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment