# Proper syntax of script fields

**URL:** <https://discuss.elastic.co/t/proper-syntax-of-script-fields/71697>\
**Category:** Elasticsearch\
**Created:** [January 16, 2017, 8:40am UTC](https://discuss.elastic.co/t/proper-syntax-of-script-fields/71697 "2017-01-16T08:40:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![esamudio](https://avatars.discourse-cdn.com/v4/letter/e/df788c/32.png) [@esamudio](https://discuss.elastic.co/u/esamudio)\
**Post date:** [January 16, 2017, 8:40am UTC](https://discuss.elastic.co/t/proper-syntax-of-script-fields/71697/1 "2017-01-16T08:40:07Z")

</div>

Hello,

I was trying to use [scripted fields as described in the docs](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-script-fields.html) for the ultimate goal of using them in Kibana but I was unable to get them to work via curl. I tried adapting the query shown in the docs for my own setup/example but I was unable to get it to work as my response did not contain '\_source' or 'doc'. I have source enabled and I did not disable doc values in [my mapping](https://gist.github.com/esamudio/f16a154b73c95e30062bc4ce9cff3123).

I was wondering if someone could shed some light into getting script fields working.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![tanguy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tanguy/32/6030_2.png) [@tanguy](https://discuss.elastic.co/u/tanguy)\
**Post date:** [January 16, 2017, 11:26am UTC](https://discuss.elastic.co/t/proper-syntax-of-script-fields/71697/2 "2017-01-16T11:26:51Z")

</div>

Hi,

Could you please share a sample of your doc and query with scripted fields? That would help to spot a syntax issue or whatever.

Thanks

---

<div class="post-metadata">

**Author:** ![esamudio](https://avatars.discourse-cdn.com/v4/letter/e/df788c/32.png) [@esamudio](https://discuss.elastic.co/u/esamudio)\
**Post date:** [January 16, 2017, 1:35pm UTC](https://discuss.elastic.co/t/proper-syntax-of-script-fields/71697/3 "2017-01-16T13:35:43Z")

</div>

thanks for the quick response!

If I do something such as:  
`curl -XGET 'localhost:9200/dns2/_search?pretty' -d' { "query" : { "match_all": {} }}'`

I get a [response without \_source or doc values](https://gist.github.com/esamudio/50b88eebb5bb54ba1cd577c57df4c131).

I changed the query from the one on the documentation (to perform the search on an index) because if I did not do that, I would only get a response that includes the .kibana indices.  
Query: `curl -XGET 'localhost:9200/dns2/_search?pretty' -d' { "query" : { "match_all": {} }}'`

[Response](https://gist.github.com/esamudio/93031ce002afd9cc24ef307c1b6fa92b)

I have an integer field called 'domain\_name\_length' so I suppose I can use the full example from the documentation but instead of 'my\_field\_name', I'll use 'domain\_name\_length'. Therefore, my query would be as follows:

```
curl -XGET 'localhost:9200/dns2/_search?pretty' -d '{
	"query": {
		"match_all": {}
	},
	"script_fields": {
		"test1": {
			"script": {
				"lang": "painless",
				"inline": "doc['domain_name_length'].value * 2"
			}
		},
		"test2": {
			"script": {
				"lang": "painless",
				"inline": "doc['domain_name_length'].value * factor",
				"params": {
					"factor": 2.0
				}
			}
		}
	}
}'

```

My [response](https://gist.github.com/esamudio/d1a26002b1d1e5eea36e2f5c5bf9e169) gives me an error as it does not contain doc values.

---

<div class="post-metadata">

**Author:** ![tanguy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tanguy/32/6030_2.png) [@tanguy](https://discuss.elastic.co/u/tanguy)\
**Post date:** [January 16, 2017, 1:59pm UTC](https://discuss.elastic.co/t/proper-syntax-of-script-fields/71697/4 "2017-01-16T13:59:00Z")

</div>

It looks like the `'` in the scripted field `doc['domain_name_length']` must be correctly escaped when used with the curl tool, because curl also uses the `'` as a delimiter for the request body.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [February 13, 2017, 1:59pm UTC](https://discuss.elastic.co/t/proper-syntax-of-script-fields/71697/5 "2017-02-13T13:59:09Z")

</div>

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