使用python和ElasticSearch获取最近2天的结果



我有这个python代码从ElasticSearch获得结果

from elasticsearch import Elasticsearch
es = Elasticsearch([{'host':'127.0.0.1' , 'port' : 1234}])
query = 'name:Jon'
es.search(index='MyIndex' , q=query , size =50)

行得通!但是…

  1. 我如何只获得最近2天的结果?
  2. 如何先获得按新闻项排序的结果?

python elasticsearch模块中的查询可以像弹性查询一样。

query = 
'{

"sort": [
{
"date": {
"order": "desc"
}
}
],
"query": {
"bool": {
"must": [
{
"range": {
"date": {
"gte": "2021-02-09 00:00",
"format": "yyyy-MM-dd HH:mm"
}
}
},
{
"term": {
"name": {
"value": "jon"
}
}
}
]
}
}
}'

最新更新