# AddFields Seems to not return result

**URL:** https://discuss.elastic.co/t/addfields-seems-to-not-return-result/9931
**Category:** Elasticsearch
**Created:** [December 4, 2012, 11:24am UTC](https://discuss.elastic.co/t/addfields-seems-to-not-return-result/9931 "2012-12-04T11:24:07Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![DMAC](https://avatars.discourse-cdn.com/v4/letter/d/7bcc69/32.png) [@DMAC](https://discuss.elastic.co/u/DMAC)
#### Post date: [December 4, 2012, 11:24am UTC](https://discuss.elastic.co/t/addfields-seems-to-not-return-result/9931/1 "2012-12-04T11:24:07Z")

</div>

Hi,

I am trying to get elastic search to return a subset of fields from my documents. I am using the following code:

```
	SearchResponse response = client.prepareSearch("myIndex")
			.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
			.setQuery(query)
			.addField("myField")
			.setFrom(0)
			.setSize(10000).setExplain(false).execute().actionGet();
	SearchHit[] docs = response.getHits().getHits();
	for (SearchHit doc : docs)
		System.err.println(doc.getSource().keySet());
	System.out.println("size is " + docs.length);
	client.close();

```

This returns 8000 results but getSource() is null, when I use the REST API with

```
	String url = "http://myServer:9200/myIndex/_search";
	String data = "{\"fields\":[\"myField\"], \"from\":0,\"size\" : 10000, \"query\": { \"bool\": { \"should\": [{ \"query_string\": { \"default_field\": \"body\", \"query\": \"*\" }}]}}}";
	HttpGetWithEntity httpGet = new HttpGetWithEntity(url);
	
	httpGet.setEntity(new StringEntity(data, "UTF-8"));
	HttpResponse res = httpclient.execute(httpGet);

```

This works well, and returns the fields I am looking for.

What am I doing incorrectly?

Regards

D.

--

---

<div class="post-metadata">

### Author: ![DMAC](https://avatars.discourse-cdn.com/v4/letter/d/7bcc69/32.png) [@DMAC](https://discuss.elastic.co/u/DMAC)
#### Post date: [December 4, 2012, 12:43pm UTC](https://discuss.elastic.co/t/addfields-seems-to-not-return-result/9931/2 "2012-12-04T12:43:11Z")

</div>

Hi All,

Fixed my problem,

I had to use

System.err.println(doc.getField("myField").getValue());

Regards

D.

> Hi,
> 
> I am trying to get Elasticsearch to return a subset of fields from my documents. I am using the following code:
> 
> ```
> SearchResponse response = client.prepareSearch("myIndex")
> .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
> .setQuery(query)
> .addField("myField")
> .setFrom(0)
> .setSize(10000).setExplain(false).execute().actionGet();
> SearchHit[] docs = response.getHits().getHits();
> for (SearchHit doc : docs)
> System.err.println(doc.getSource().keySet());
> System.out.println("size is " + docs.length);
> client.close();
> 
> ```
> 
> This returns 8000 results but getSource() is null, when I use the REST API with
> 
> ```
> String url = "http://myServer:9200/myIndex/_search";
> String data = "{\"fields\":[\"myField\"], \"from\":0,\"size\" : 10000, \"query\": { \"bool\": { \"should\": [{ \"query_string\": { \"default_field\": \"body\", \"query\": \"*\" }}]}}}";
> HttpGetWithEntity httpGet = new HttpGetWithEntity(url);
>   
> httpGet.setEntity(new StringEntity(data, "UTF-8"));
> HttpResponse res = httpclient.execute(httpGet);
> 
> ```
> 
> This works well, and returns the fields I am looking for.
> 
> What am I doing incorrectly?
> 
> Regards
> 
> D.

--

---

<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: [July 6, 2017, 3:01am UTC](https://discuss.elastic.co/t/addfields-seems-to-not-return-result/9931/3 "2017-07-06T03:01:40Z")

</div>


