# Getting 0 from field-data which contains text

**URL:** https://discuss.elastic.co/t/getting-0-from-field-data-which-contains-text/139490
**Category:** Elasticsearch
**Created:** [July 11, 2018, 7:05am UTC](https://discuss.elastic.co/t/getting-0-from-field-data-which-contains-text/139490 "2018-07-11T07:05:08Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![PolterFox](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polterfox/32/29781_2.png) [@PolterFox](https://discuss.elastic.co/u/PolterFox)
#### Post date: [July 11, 2018, 7:05am UTC](https://discuss.elastic.co/t/getting-0-from-field-data-which-contains-text/139490/1 "2018-07-11T07:05:08Z")

</div>

Hey guys,

I want to return a field-data value which contains text, but I am getting 0 as a result, my GET request looks like this:

```
GET myIndex-*/_search
{
  "query": {
  "match_all": {}
},
"script_fields": {
"my_field": {
  "script": {
    "lang": "painless",
    "source": "return doc['my_field'].value"
   }
  }
 }
}

```

Do you have an idea why I am facing this?

Best regards,  
Robert

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [July 13, 2018, 9:21am UTC](https://discuss.elastic.co/t/getting-0-from-field-data-which-contains-text/139490/2 "2018-07-13T09:21:05Z")

</div>

> [@PolterFox](#):
>
> GET myIndex-\*/\_search { "query": { "match\_all": {} }, "script\_fields": { "my\_field": { "script": { "lang": "painless", "source": "return doc['my\_field'].value" } } } }

From [the docs](https://www.elastic.co/guide/en/elasticsearch/reference/6.3/search-request-script-fields.html):

> the doc[...] notation only allows for simple valued fields (you can’t return a json object from it) and makes sense only for non-analyzed or single term based fields

However, depending on what you want to achieve here are some approaches:

```
DELETE test
PUT test
{
  "settings": {
	"number_of_replicas": 0,
	"number_of_shards": 1
  },
  "mappings": {
	"doc": {
	  "properties": {
		"text": {
		  "type": "text",
		  "fielddata": true
		}
	  }
	}
  }
}
POST test/doc
{
  "text":"The quick brown fox"
}

//This returns only "brown"
GET test/_search
{
  "query": {
	"match_all": {}
  },
  "script_fields": {
	"text": {
	  "script": {
		"lang": "painless",
		"source": "return doc['text'].value"
	  }
	}
  }
}

//This returns values (in this order): "brown", "fox", "quick", "the"
GET test/_search
{
  "query": {
	"match_all": {}
  },
  "script_fields": {
	"text": {
	  "script": {
		"lang": "painless",
		"source": "return doc['text'].values"
	  }
	}
  }
}

//This returns value "The quick brown fox"	
GET test/_search
{
  "query": {
	"match_all": {}
  },
  "_source": [
	"text"
  ]
}

```

---

<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: [August 10, 2018, 9:28am UTC](https://discuss.elastic.co/t/getting-0-from-field-data-which-contains-text/139490/3 "2018-08-10T09:28:04Z")

</div>

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