开发者

need something like coalesce in elasticsearch

My current elasticsearch query is-

{

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

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

                }

}

I want to modify it such that if "firstClosedAt" is null or not present then look for "closedAt". Just lik开发者_如何学Goe we have coalesce("firstClosedAt","closedAt") in sql

Help would be appreciated


There's no coalesce equivalent in ES, but you can do the query like below, which can read like: "either use firstClosedAt OR use closedAt if firstClosedAt does not exist":

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

You could, however, create a much simpler query if you create another date field at indexing time which would either take the value of firstClosedAt or closedAt if firstClosedAt does not exist

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜