我需要在elasticsearch中使用像coalesce这样的东西

wgmfuz8q  于 2022-12-11  发布在  ElasticSearch
关注(0)|答案(1)|浏览(173)

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 like we have coalesce("firstClosedAt","closedAt") in sql
Help would be appreciated

tvokkenx

tvokkenx1#

ES中没有coalesce等效项,但您可以执行如下查询,如下所示:“使用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
                      }
                    }
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

但是,如果您在编制索引时**创建另一个日期字段,则可以创建一个简单得多的查询,该字段的值为firstClosedAtclosedAt(如果firstClosedAt不存在

相关问题