使用的是es 7.10.x的版本
python -m pip install elasticsearch==7.10.1 -i https://mirrors.aliyun.com/pypi/simple
from elasticsearch import Elasticsearch
"""
本利展示es的随机查询
"""
client = Elasticsearch("http://localhost:9200")
# 使用用户密码登录
# client = Elasticsearch(["username:password@ip1:port1", "username:password@ip2:port2"])
body = {
"query": {
"bool": {
"must": [
{
"term": {
"age": {
"value": 30
}
}
}
]
}
},
# "from": 0,
# "size": 2000,
"sort": [
{
"_script": {
"script": 'Math.random()',
"type": "number"
}
}
]
}
result: list = client.search(index="indexname", body=body, from_=0, size=2000)