Searching with runtime, without mapping with runtime

hello , I am trying to learn runtime .
it uses in the script the painless language , It wold be nice to I understand this language , that I could test it before mapping .
the way to test it before mapping it is search with runtime .
here it has one example -


PUT my-index-000002-test/
{
  "mappings": {
    "runtime": {
      "day_of_week": {
        "type": "keyword",
        "script": {
          "source": "emit(doc['@timestamp'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ROOT))"
        }
      }
    },
    "properties": {
      "@timestamp": {"type": "date"}
    }
  }
}

GET my-index-000002-test/_mapping


POST my-index-000002-test/_bulk?refresh=true
{"index":{}}
{"@timestamp":1516729294000,"model_number":"QVKC92Q","measures":{"voltage":"5.2","start": "300","end":"8675309"}}
{"index":{}}
{"@timestamp":1516642894000,"model_number":"QVKC92Q","measures":{"voltage":"5.8","start": "300","end":"8675309"}}
{"index":{}}
{"@timestamp":1516556494000,"model_number":"QVKC92Q","measures":{"voltage":"5.1","start": "300","end":"8675309"}}
{"index":{}}
{"@timestamp":1516470094000,"model_number":"QVKC92Q","measures":{"voltage":"5.6","start": "300","end":"8675309"}}
{"index":{}}
{"@timestamp":1516383694000,"model_number":"HG537PU","measures":{"voltage":"4.2","start": "400","end":"8625309"}}
{"index":{}}
{"@timestamp":1516297294000,"model_number":"HG537PU","measures":{"voltage":"4.0","start": "400","end":"8625309"}}



GET my-index-000002-test/_search
{
  "query": {
    "match_all": {}
  },
   "fields" : ["day_of_week"]
}

this worked for me .
it returns -

"hits": [
      {
        "_index": "my-index-000002-test",
        "_id": "pEH4q4kB6LNnmdzdEkwe",
        "_score": 1,
        "_source": {
          "@timestamp": 1516729294000,
          "model_number": "QVKC92Q",
          "measures": {
            "voltage": "5.2",
            "start": "300",
            "end": "8675309"
          }
        },
        "fields": {
          "day_of_week": [
            "Tuesday"
          ]
        }
      },
      {
        "_index": "my-index-000002-test",
        "_id": "pUH4q4kB6LNnmdzdEkwe",
        "_score": 1,
        "_source": {
          "@timestamp": 1516642894000,
          "model_number": "QVKC92Q",
          "measures": {
            "voltage": "5.8",
            "start": "300",
            "end": "8675309"
          }
        },
        "fields": {
          "day_of_week": [
            "Monday"
          ]
        }
      },

NOW, I am trying to search with runtime , without mapping with runtime .
this is important to I test the Painless Script .
So , I did like that -

GET my-index-000002-test/_search
{
  "runtime_mappings": {
    "day_of_week": {
      "type": "keyword",
      "source": "emit(doc['@timestamp'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ROOT))"
    }
  },
  "query": {
     "match_all": {}
  },
  "fields" : ["day_of_week"]
}


It is the same script that I used to mapping , and after search .
but it gave me this error -

{
  "error": {
    "root_cause": [
      {
        "type": "mapper_parsing_exception",
        "reason": "unknown parameter [source] on runtime field [day_of_week] of type [keyword]"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "my-index-000002-test",
        "node": "d8DCCAzDQGCLFPA7rX5QSQ",
        "reason": {
          "type": "mapper_parsing_exception",
          "reason": "unknown parameter [source] on runtime field [day_of_week] of type [keyword]"
        }
      }
    ]
  },
  "status": 400
}

It is not, in the mapping you used:

      "day_of_week": {
        "type": "keyword",
        "script": {
          "source": "emit(doc['@timestamp'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ROOT))"
        }
      }

But in your search you used:

    "day_of_week": {
      "type": "keyword",
      "source": "emit(doc['@timestamp'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ROOT))"
    }
  }

Which is the issue, the source needs to be inside the script object as you can see in the documentation.

thanks, it works

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.