使用的是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)
该文展示了如何在Elasticsearch7.10.x版本中,利用Python的elasticsearch库执行一个基于bool查询的搜索,其中对年龄为30的记录进行筛选,并使用脚本进行随机排序。查询结果从索引indexname中获取,可调整查询的from和size参数。
1551





