Skip to content

Instantly share code, notes, and snippets.

@punit-naik
Last active January 15, 2021 09:58
Show Gist options
  • Select an option

  • Save punit-naik/c04c8429e5b492c5b467b42cf41fbfd5 to your computer and use it in GitHub Desktop.

Select an option

Save punit-naik/c04c8429e5b492c5b467b42cf41fbfd5 to your computer and use it in GitHub Desktop.
Elasticsearch - search for keys inside nested field value, which is a map/json
#!/bin/bash
curl -X GET https://<username>:<password>@<host-url>/<index(es)>/_search?pretty=true -H 'Content-Type: application/json' -d "{\"query\": {\"match_all\":{}}, \"script_fields\": {\"field_keys_prefied_with_a\": {\"script\": {\"lang\": \"painless\", \"source\": \""" Map m = params._source.<nested/map field name>; return (m == null) ? new HashMap() : m.entrySet().stream().filter(e->e.getKey().startsWith(params.prefix)).collect(Collectors.toMap(e->e.getKey(),e->e.getValue())); \""", \"params\": {\"prefix\": \"a\"}}}}, \"size\": 100}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment