elasticsearch - 在 Elasticsearch 中组合范围和匹配
问题描述
我在具有以下结构的 Elasticsearch 索引中有文档:
{
"title": 'Nutrtional facts',
"begin_timestamp" : 1582686052,
"end_timestamp" : 1582686093
}
{
"title": 'Guitar facts',
"begin_timestamp" : 1447991100,
"end_timestamp" : 1447994100
}
{
"title": 'Hair style facts',
"begin_timestamp" : 1447991100,
"end_timestamp" : 1447994100
}
{
"title": 'Piano facts',
"begin_timestamp" : 1554416211,
"end_timestamp" : 1591308724
}
我的目标是检索标题匹配的文档facts
以及开始或结束时间戳是否大于当前日期和时间。
title matches `facts` && begin_timestamp > CURRENT_DATE_TIME OR end_timestamp > CURRENT_DATE_TIME
我正在运行的当前查询如下:
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "facts"
}
}
],
"should": [
{
"range": {
"begin_timestamp_for_search": {
"gte": 1580853917
}
}
},
{
"range": {
"begin_timestamp_for_search": {
"gte": 1580853917
}
}
}
]
}
}
}
然而,这匹配任何匹配的内容facts
,并返回所有文档,无论时间戳是在当前日期和时间之前还是之后。我对 ES 很陌生,想知道如何编写查询,所以唯一会返回的文档是:
{
"title": 'Nutrtional facts',
"begin_timestamp" : 1582686052,
"end_timestamp" : 1582686093
}
{
"title": 'Piano facts',
"begin_timestamp" : 1570227141,
"end_timestamp" : 1591308724
}
解决方案
{
"from": 0,
"size": 200,
"query": {
"bool": {
"filter": [
{
"bool": {
"must": [
{
"bool": {
"must": [
{
"wildcard": {
"title": {
"wildcard": "*facts*",
"boost": 1
}
}
},
{
"bool": {
"should": [
{
"range": {
"begin_timestamp": {
"from": 1580853917,
"to": null,
"include_lower": false,
"include_upper": true,
"boost": 1
}
}
},
{
"range": {
"end_timestamp": {
"from": 1580853917,
"to": null,
"include_lower": false,
"include_upper": true,
"boost": 1
}
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
}
}
推荐阅读
- mysql - 为什么mysql不执行查询?
- python - 在 jupyter notebook 中添加命令行参数
- android - 使用 textswitcher 切换文本时删除边距
- spring-boot - 端点调用使用@Transational 注释的多个方法不起作用
- java - 几个小时不活动后打开应用程序时黑屏
- c++ - 可以调整这两种方法,让我只解析一次命令行吗?
- rvest - RVest 试图抓取日期时间字段
- javascript - 尽管发送了数据,但对 Express 应用程序的请求带有空正文,但仅在一条路线中
- c# - Outlook 渲染问题
- javascript - 如何使 JS for 循环异步