Skip to content

Instantly share code, notes, and snippets.

@ijokarumawak
Created December 15, 2021 03:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ijokarumawak/7fd8755fb7e3d53e8f197bb71f2881a4 to your computer and use it in GitHub Desktop.
Save ijokarumawak/7fd8755fb7e3d53e8f197bb71f2881a4 to your computer and use it in GitHub Desktop.
A runtime field example at query time.
GET blogs/_search
{
  "runtime_mappings": {
    "publish_hour": {
      "type": "long",
      "script": {
        "source": "emit(doc['publish_date'].value.hour)"
      }
    }
  },
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "publish_date": {
              "gte": "2018-01-01",
              "lte": "2018-12-31"
            }
          }
        },
        {
          "bool": {
            "should": [
              {
                "range": {
                  "publish_hour": {
                    "gte": 0,
                    "lt": 6
                  }
                }
              },
              {
                "range": {
                  "publish_hour": {
                    "gte": 23,
                    "lt": 24
                  }
                }
              }
            ]
          }
        }
      ]
    }
  },
  "_source": false,
  "fields": [
    "publish_date",
    "publish_hour"
  ]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment