python - Elasticsearch query: syntax for range restriction gives 400
问题描述
I'm using python to communicate with an ElasticSearch server. I'm using the elasticsearch package, and I'm formatting queries and feeding them to the search function in the body parameter.
Sending queries worked just fine. For example, this query works:
{'query': {'constant_score': {'filter': {'bool': {'must': {'terms': {'id.keyword': ['d42bdc8a-a38b-43fa-9283-13b5e5c08c6e']}}}}}}}
I now want to restrict the range, so I add a little segment (indentation for clarity):
{'query':
{'constant_score':
{'filter':
{'bool':
{'must':
{'range':
{'startTime':
{'format': "yyyy-MM-dd'T'HH:mm:ss.SSS",
'gte': '2018-01-20T17:19:43.393',
'lte': '2018-04-01T17:19:43.393'}
},
'terms':
{'id.keyword':
['d42bdc8a-a38b-43fa-9283-13b5e5c08c6e']
}
}
}
}
}
}
}
A query that looks identical (to me) worked in R. I'm getting a status 400 though (bad request). Does anyone see what the problem is?
解决方案
你快到了,这是正确的查询:
{'query':
{'constant_score':
{'filter':
{'bool':
{'must': [
{'range':
{'startTime':
{'format': "yyyy-MM-dd'T'HH:mm:ss.SSS",
'gte': '2018-01-20T17:19:43.393',
'lte': '2018-04-01T17:19:43.393'}
}
},
{
'terms':
{'id.keyword':
['d42bdc8a-a38b-43fa-9283-13b5e5c08c6e']
}
}
]
}
}
}
}
}
推荐阅读
- c# - 在异步调用中强制重新渲染更新面板中的 svg 部分?
- r - 在 R 中创建一个带有序数约束的分类随机向量
- php - UpdateOrCreate 无法更新一列
- c# - 按钮上的绑定命令不起作用 wpf mvvm
- python - 为什么 subprocess 会启动“额外”应用程序?
- c++ - Eclipse C++:致命错误:cheddar.h:没有这样的文件或目录
- css - CSS 模块和悬停 CSS 无法正常工作
- c# - 如何在不访问每个控制器中的 HttpContext 的情况下使所有控制器都可以使用会话变量?
- linux - 无法通过同一网络中的 IP 地址访问 nexus 服务器
- sql - SQL Server COUNT 多个表返回错误结果