首页 > 解决方案 > 在 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
}

标签: elasticsearchelasticsearch-query

解决方案


{
    "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
        }
    }
}


推荐阅读