可以使用Elasticsearch查询语句来实现上述需求。首先确定要查询的索引,然后使用以下代码片段查询:
from elasticsearch import Elasticsearch
es = Elasticsearch()
res = es.search(index="your_index_name", body={
"query": {
"match_all": {}
},
"sort": [
{
"timestamp": {"order": "asc"}
}
],
"from": 0,
"size": 10
})
print(res['hits']['hits'])
上述代码会输出文档在查询时的一些元数据。如果想要输出特定字段的值,可以在查询语句中指定要检索的字段。例如,如果仅需要查询“message”字段并返回结果,则应使用以下代码片段:
from elasticsearch import Elasticsearch
es = Elasticsearch()
res = es.search(index="your_index_name", body={
"query": {
"match_all": {}
},
"sort": [
{
"timestamp": {"order": "asc"}
}
],
"_source" : ["message"],
"from": 0,
"size": 10
})
for hit in res['hits']['hits']:
print(hit['_source']['message'])
此代码将仅检索文档的“message”字段并打印出与查询匹配的值。