Python 仅当字段存在时按字段筛选,弹性搜索
我的文档中有一个日期字段,我只想返回日期小于现在-5m的文档,但是不是所有文档都有该字段,它们只在第一次单独查询时收到Python 仅当字段存在时按字段筛选,弹性搜索,python,
elasticsearch,Python,
elasticsearch,我的文档中有一个日期字段,我只想返回日期小于现在-5m的文档,但是不是所有文档都有该字段,它们只在第一次单独查询时收到 documents = es.search(index='index_name', size=10000, body={ "query": { "bool": { "must": [ { "range": { "time_lockout":
documents = es.search(index='index_name', size=10000, body={
"query": {
"bool": {
"must": [
{
"range": {
"time_lockout": {
"lt": "now-5m"
}
}
}
],
"filter": [
],
"should": [
],
"must_not": [
]
}
}})
所以我的伪代码是
if `time_lockout` exists
give documents that are now-5 including all documents thats dont have `time_lockout`
Exclude documents that dat range falls withon that 5 minute window
将查询更新到以下位置:
{
"query": {
"bool": {
"should": [
{
"range": {
"time_lockout": {
"lt": "now-5m"
}
}
},
{
"bool": {
"must_not": [
{
"exists": {
"field": "time_lockout"
}
}
]
}
}
]
}
}
}