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?

2 Answers 2

33

In the fields array, make it load _source:

{
  "stored_fields": [
    "_source"
  ],
  "query": {
    "term": {
      "statuses": "active"
    }
  },
  "script_fields": {
    "email": {
      "script": "_source.name"
    }
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Does anyone know exactly why this happens? Is it related to github.com/elastic/elasticsearch/issues/20068 ?
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"
    }
  } ]
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.