27

I am running a simple query like so:

{
  "query": {
    "term": {
      "statuses": "active"
    }
  },
  "script_fields": {
    "test": {
      "script": "_source.name"
    }
  }
}

The problem is that once I introduce the script_fields, I no longer get _source in my results.

I have tried:

{
  "fields": [
    "_all"
  ],
  "query": {
    "term": {
      "statuses": "active"
    }
  },
  "script_fields": {
    "email": {
      "script": "_source.name"
    }
  }
}

and

{
  "fields": [
    "*"
  ],
  "query": {
    "term": {
      "statuses": "active"
    }
  },
  "script_fields": {
    "email": {
      "script": "_source.name"
    }
  }
}

But they did not make any difference. Is there a way to get _source returned in addition to the script_fields?

F21
  • 32,163
  • 26
  • 99
  • 170

2 Answers2

33

In the fields array, make it load _source:

{
  "stored_fields": [
    "_source"
  ],
  "query": {
    "term": {
      "statuses": "active"
    }
  },
  "script_fields": {
    "email": {
      "script": "_source.name"
    }
  }
}
Arun Kumar Nagarajan
  • 2,347
  • 3
  • 17
  • 28
F21
  • 32,163
  • 26
  • 99
  • 170
  • Does anyone know exactly _why_ this happens? Is it related to https://github.com/elastic/elasticsearch/issues/20068 ? – Henry C Aug 10 '17 at 14:01
1

This works for me:

curl -X DELETE localhost:9200/a

curl -X POST localhost:9200/a/b/c -d '{"title" : "foo"}'

curl -X POST localhost:9200/a/_refresh

echo;

curl localhost:9200/a/_search?pretty -d '{
  "fields": [
    "_source"
  ],
  "query": {
    "match_all": {}
  },
  "script_fields": {
    "title_script": {
      "script": "_source.title"
    }
  }
}'

Output:

"hits" : {
  # ...
  "hits" : [ {
    # ...
    "_source" : {"title" : "foo"},
    "fields" : {
      "title_script" : "foo"
    }
  } ]
}
karmi
  • 14,059
  • 3
  • 33
  • 41