# Specifying Fields to Return in Source

**URL:** <https://discuss.elastic.co/t/specifying-fields-to-return-in-source/4614>\
**Category:** Elasticsearch\
**Created:** [June 14, 2011, 9:57am UTC](https://discuss.elastic.co/t/specifying-fields-to-return-in-source/4614 "2011-06-14T09:57:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![davrob](https://avatars.discourse-cdn.com/v4/letter/d/d78d45/32.png) [@davrob](https://discuss.elastic.co/u/davrob)\
**Post date:** [June 14, 2011, 9:57am UTC](https://discuss.elastic.co/t/specifying-fields-to-return-in-source/4614/1 "2011-06-14T09:57:49Z")

</div>

Hi,

I thought to limit the fields I return in the source, I would do the  
following:

SearchRequestBuilder searchReq = client.prepareSearch()  
.addFields(DEFAULT\_FIELDS)  
.setIndices(indexes)  
.setTypes(types)  
.setQuery(queryBuilder)  
.setFrom(this.start)  
.setSize(pageSize);

Where default fields is something like this:

String[] DEFAULT\_FIELDS = new String[]{"id","fullName","person\_name"};

But, when I execute this query I find this:

```
     for (SearchHit hit : queryResponse.getHits()){
		Map<String, Object> source = hit.getSource();
     }

```

In this case the source object is set to null. Should I be using the  
hit.fields() method instead?

Best Regards,

David.

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [June 14, 2011, 10:05am UTC](https://discuss.elastic.co/t/specifying-fields-to-return-in-source/4614/2 "2011-06-14T10:05:12Z")

</div>

Yes.

On Tuesday, June 14, 2011 at 12:57 PM, davrob2 wrote:

> Hi,
> 
> I thought to limit the fields I return in the source, I would do the  
> following:
> 
> SearchRequestBuilder searchReq = client.prepareSearch()  
> .addFields(DEFAULT\_FIELDS)  
> .setIndices(indexes)  
> .setTypes(types)  
> .setQuery(queryBuilder)  
> .setFrom(this.start)  
> .setSize(pageSize);
> 
> Where default fields is something like this:
> 
> String DEFAULT\_FIELDS = new String{"id","fullName","person\_name"};
> 
> But, when I execute this query I find this:
> 
> for (SearchHit hit : queryResponse.getHits()){  
> Map\<String, Object\> source = hit.getSource();  
> }
> 
> In this case the source object is set to null. Should I be using the  
> hit.fields() method instead?
> 
> Best Regards,
> 
> David.

---

<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, 4:03am UTC](https://discuss.elastic.co/t/specifying-fields-to-return-in-source/4614/3 "2017-07-06T04:03:40Z")

</div>


