Elastic Search 与多范围、术语过滤器与 And 和 Or 运算符的组合

Elastic Search combination of with Multiple Range, Term filters with And and Or operators

我有一个包含多个数据范围过滤器的过滤器,其中包含 And 和 OR 运算符。我必须获得满足两个日期范围过滤器或任何一个日期范围过滤器的过滤器结果。

 "query":{
    "bool" : {
    "must" : [
      {
        "match_phrase_prefix" : {
          "searchField" : {
            "query" : "Adam",
            "slop" : 0,
            "max_expansions" : 50,
            "boost" : 1.0
          }
        }
      }
    ],
    "filter" : [
      {
        "term" : {
          "srvcType" : {
            "value" : "FullTime",
            "boost" : 1.0
          }
        }
      },
      {"range" : { "or": {"startDt": {"from" : "2010-05-16","to" : "2022-02-18","include_lower": true,"include_upper" : true,"boost" : 1.0}} }},
      {"range" : { "or": {"endDt": {"from" : "2015-05-16","to" : "2022-02-18","include_lower" : true,"include_upper" : true,"boost" : 1.0}}}}
    ],
    "adjust_pure_negative" : true,
    "boost" : 1.0
  }
}
}

我尝试 运行 上面的查询,我得到 parsing_exception - 查询不支持 StartDt。

{
 "query":{
    "bool" : {
    "must" : [
      {
        "match_phrase_prefix" : {
          "searchField" : {
            "query" : "Adam",
            "slop" : 0,
            "max_expansions" : 50,
            "boost" : 1.0
          }
        }
      }
    ],
    "filter" : [
      {
        "term" : {
          "srvcType" : {
            "value" : "FullTime",
            "boost" : 1.0
          }
        }
      },
      {"range" : {"startDt": {"from" : "2010-05-16","to" : "2022-02-18","include_lower": true,"include_upper" : true,"boost" : 1.0}} },
      {"range" : {"endDt": {"from" : "2015-05-16","to" : "2022-02-18","include_lower" : true,"include_upper" : true,"boost" : 1.0}}}
    ],
    "adjust_pure_negative" : true,
    "boost" : 1.0
  }
}
}

如果您的日期范围过滤器需要 AND 语义,您可以在 bool/filter 数组中同时使用 range 查询。

但是,如果您需要 OR 语义,您可以使用 bool/should 查询,如下所示:

{
  "query": {
    "bool": {
      "must": [
        {
          "match_phrase_prefix": {
            "searchField": {
              "query": "Adam",
              "slop": 0,
              "max_expansions": 50,
              "boost": 1
            }
          }
        }
      ],
      "filter": [
        {
          "term": {
            "srvcType": {
              "value": "FullTime",
              "boost": 1
            }
          }
        }
      ],
      "minimum_should_match": 1,
      "should": [
        {
          "range": {
            "startDt": {
              "from": "2010-05-16",
              "to": "2022-02-18",
              "include_lower": true,
              "include_upper": true,
              "boost": 1
            }
          }
        },
        {
          "range": {
            "endDt": {
              "from": "2015-05-16",
              "to": "2022-02-18",
              "include_lower": true,
              "include_upper": true,
              "boost": 1
            }
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1
    }
  }
}