Getting 0 from field-data which contains text

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

From the docs:

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"
  ]
}

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