Skip to content

Deepsort/deterministic toJson Mustache function #35989

@ypid-geberit

Description

@ypid-geberit

Describe the feature:

The Mustache function {{#toJosn}} is not deterministic meaning that the order of keys in a dictionary can change when templating the same data multiple times. This is an issue for unit/integration testing where exact matches are required. On ES 6.2.4 I never had those issues. Now on ES 6.5.1 dictionaries are serialized not deterministical.

I am surprised that this has not caused issues for the unit tests of Elasticsearch itself. Example: https://github.com/elastic/elasticsearch/pull/18856/files#diff-ba0eba6caacd2ed288fbac13b74086eaR239

Maybe it only happens for more nested data structures. I found the issue while writing an Elasticsearch watch like this:

---

# yamllint disable rule:line-length rule:comments-indentation

metadata:

  ## Use an offset to allow events to travel thought their pipeline.
  ## See: https://discuss.elastic.co/t/ensure-that-watcher-does-not-miss-documents-logs/127780/1
  time_offset: '5m'
  time_window: '1m'

trigger:
  schedule:
    ## Based on ctx.metadata.time_window.
    ## Everyone triggers jobs at second 0 so this one is triggered at second 50
    ## every minute.
    cron:
      - '50 * *    * * ?'

input:
  search:
    ## Based on ctx.metadata.time_window.
    timeout: '40s'
    request:

      indices:
        - 'log_network-switch__*'

      body:

        size: 100

        query:
          bool:
            filter:
              - match_phrase:
                  host:
                    query: 'gnu'

condition:
  compare:
    ctx.payload.hits.total:
      gt: 0

actions:
  log:
    logging:
      level: info
      text: 'Watch query hits: {{#toJson}}ctx.payload.hits.hits{{/toJson}}'

(Yes, YAML is awesome, also for watch definitions. Ref: elastic/examples#239)

Maybe also other parameters like pretty, indent could be passed to {{#toJosn}} similar to {{#join}}. Ref: #18856

I at first used:

      text: 'Watch query hits: {{ctx.payload.hits.hits}}'

but this is also not deterministic.

Metadata

Metadata

Assignees

No one assigned

    Labels

    :Core/Infra/ScriptingScripting abstractions, Painless, and Mustache

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions