python - 如何从Elasticsearch获取pyes中的随机文档

标签 python elasticsearch pyes

如何使用pyes获取随机文档?我有一个新的集群可以访问并想探索

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

最佳答案

例如,您可以使用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

关于python - 如何从Elasticsearch获取pyes中的随机文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31508379/

相关文章:

Python - 从具有多个类别的 csv 文件制作字典

稀疏随机填充数组的 Pythonic 方法?

elasticsearch - 如何在Java的Elasticsearch中获取文档的父ID?

php - 存储库删除查询后,Symfony ElasticSearch不同步

django - 从 elasticsearch 返回的嵌套对象中检索特定字段

Python dateutils 根据 iCalendar 格式打印重复规则(参见 RFC 5545)

python - 在 Pandas Dataframe 中显示多列,但分组并仅计算一列

json - 从Elasticsearch索引创建Atom或RSS格式的搜索页面,以JSON返回结果

python - 在 Pyramid 中实现 ElasticSearch

python - ElasticSearch地理距离查询