【问题标题】:need something like coalesce in elasticsearch在 elasticsearch 中需要类似 coalesce 的东西
【发布时间】:2022-12-18 12:48:55
【问题描述】:

我当前的弹性搜索查询是-

{

            "must": [
                {
                    "range": {
                        "firstClosedAt": {
                            "gte": 1667948400000,
                            "lte": 1668034800000
                        }
                    }
                },

                {
                    "term": {
                        "status": "CLOSED"

                }

}

我想修改它,如果“firstClosedAt”为空或不存在,则查找“closedAt”。 就像我们在 sql 中有 coalesce("firstClosedAt","closedAt")

帮助将不胜感激

【问题讨论】:

    标签: elasticsearch coalesce opensearch


    【解决方案1】:

    ES 中没有合并等价物,但您可以执行如下查询,其内容如下:“如果 firstClosedAt 不存在,则使用 firstClosedAt 或使用 closedAt”:

    {
      "query": {
        "bool": {
          "filter": [
            {
              "term": {
                "status": "CLOSED"
              }
            },
            {
              "bool": {
                "minimum_should_match": 1,
                "should": [
                  {
                    "range": {
                      "firstClosedAt": {
                        "gte": 1667948400000,
                        "lte": 1668034800000
                      }
                    }
                  },
                  {
                    "bool": {
                      "must_not": {
                        "exists": {
                          "field": "firstClosedAt"
                        }
                      },
                      "filter": {
                        "range": {
                          "closedAt": {
                            "gte": 1667948400000,
                            "lte": 1668034800000
                          }
                        }
                      }
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
    

    但是,如果您创建另一个日期字段,则可以创建一个更简单的查询在索引时如果firstClosedAt不存在,它将采用firstClosedAtclosedAt的值

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-16
      • 2019-12-10
      • 1970-01-01
      • 2011-06-16
      相关资源
      最近更新 更多