# Retrieve a specific field using Java API

**URL:** <https://discuss.elastic.co/t/retrieve-a-specific-field-using-java-api/6387>\
**Category:** Elasticsearch\
**Created:** [January 15, 2012, 4:13pm UTC](https://discuss.elastic.co/t/retrieve-a-specific-field-using-java-api/6387 "2012-01-15T16:13:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mp2893](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@mp2893](https://discuss.elastic.co/u/mp2893)\
**Post date:** [January 15, 2012, 4:13pm UTC](https://discuss.elastic.co/t/retrieve-a-specific-field-using-java-api/6387/1 "2012-01-15T16:13:31Z")

</div>

Hi,

## I'm trying to retrieve a specific field of document using Java API. I am using the following code:

## GetResponse response = client.prepareGet(index, type, id).execute().actionGet(); Map\<String, GetField\> fieldMap = response.fields(); GetField field = fieldMap.get("title"); return (String)field.getValue();

which fails, because "title" field has null value.  
But if try "response.exists()", it gives me "true".

Strangely, when I try the same thing with the following command:  
"curl -XGET '[http://localhost:9200/news/type1/12345?fields=title](http://localhost:9200/news/type1/12345?fields=title)'"  
I get the result I want.

So what is the difference here?  
I chose not to store json source when indexing the document. Is this  
the cause of the problem?

Ed

---

<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:** [January 15, 2012, 10:32pm UTC](https://discuss.elastic.co/t/retrieve-a-specific-field-using-java-api/6387/2 "2012-01-15T22:32:54Z")

</div>

By default, the source is returned. Use any of the source methods on the  
GetResponse. If you want to get just a specific field, you can ask for it  
in the get request (and then, \_source will not be returned unless you ask  
for it explicitly as well).

On Sun, Jan 15, 2012 at 6:13 PM, mp2893 [mp2893@gmail.com](mailto:mp2893@gmail.com) wrote:

> Hi,
> 
> I'm trying to retrieve a specific field of document using Java API.  
> I am using the following code:
> 
> * * *
> 
> GetResponse response = client.prepareGet(index, type,  
> id).execute().actionGet();  
> Map\<String, GetField\> fieldMap = response.fields();  
> GetField field = fieldMap.get("title");  
> return (String)field.getValue();
> 
> * * *
> 
> which fails, because "title" field has null value.  
> But if try "response.exists()", it gives me "true".
> 
> Strangely, when I try the same thing with the following command:  
> "curl -XGET '[http://localhost:9200/news/type1/12345?fields=title](http://localhost:9200/news/type1/12345?fields=title)'"  
> I get the result I want.
> 
> So what is the difference here?  
> I chose not to store json source when indexing the document. Is this  
> the cause of the problem?
> 
> Ed

---

<div class="post-metadata">

**Author:** ![mp2893](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@mp2893](https://discuss.elastic.co/u/mp2893)\
**Post date:** [January 16, 2012, 2:48am UTC](https://discuss.elastic.co/t/retrieve-a-specific-field-using-java-api/6387/3 "2012-01-16T02:48:42Z")

</div>

Thanks for the info.  
I tried the following code:  
"GetResponse response = client.prepareGet(index, type,  
id).setFields("title").execute().actionGet();"  
and it returned with the "title" object.  
The "setFields" option is what did the trick.  
Thanks again.

Ed

2012/1/16 Shay Banon [kimchy@gmail.com](mailto:kimchy@gmail.com)

> By default, the source is returned. Use any of the source methods on the  
> GetResponse. If you want to get just a specific field, you can ask for it  
> in the get request (and then, \_source will not be returned unless you ask  
> for it explicitly as well).
> 
> On Sun, Jan 15, 2012 at 6:13 PM, mp2893 [mp2893@gmail.com](mailto:mp2893@gmail.com) wrote:
> 
> > Hi,
> > 
> > I'm trying to retrieve a specific field of document using Java API.  
> > I am using the following code:
> > 
> > * * *
> > 
> > GetResponse response = client.prepareGet(index, type,  
> > id).execute().actionGet();  
> > Map\<String, GetField\> fieldMap = response.fields();  
> > GetField field = fieldMap.get("title");  
> > return (String)field.getValue();
> > 
> > * * *
> > 
> > which fails, because "title" field has null value.  
> > But if try "response.exists()", it gives me "true".
> > 
> > Strangely, when I try the same thing with the following command:  
> > "curl -XGET '[http://localhost:9200/news/type1/12345?fields=title](http://localhost:9200/news/type1/12345?fields=title)'"  
> > I get the result I want.
> > 
> > So what is the difference here?  
> > I chose not to store json source when indexing the document. Is this  
> > the cause of the problem?
> > 
> > Ed

---

<div class="post-metadata">

**Author:** ![Wes\_Plunk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wes_plunk/32/2446_2.png) [@Wes\_Plunk](https://discuss.elastic.co/u/Wes_Plunk)\
**Post date:** [February 22, 2012, 6:02pm UTC](https://discuss.elastic.co/t/retrieve-a-specific-field-using-java-api/6387/4 "2012-02-22T18:02:29Z")

</div>

what about when you want multiple fields. I tried a comma delimited list  
and it assumed the entire string was the field name. I tried calling  
setFields for each field and it only takes the last field. I tried setting  
setField with a String[] but that too failed. Is there something I'm  
missing here?

---

<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:** [February 26, 2012, 9:50am UTC](https://discuss.elastic.co/t/retrieve-a-specific-field-using-java-api/6387/5 "2012-02-26T09:50:32Z")

</div>

How are you indexing it? Is it a field with multiple values (an array in json)? If so, you will get an array back.

On Wednesday, February 22, 2012 at 8:02 PM, Wes Plunk wrote:

> what about when you want multiple fields. I tried a comma delimited list and it assumed the entire string was the field name. I tried calling setFields for each field and it only takes the last field. I tried setting setField with a String but that too failed. Is there something I'm missing here?

---

<div class="post-metadata">

**Author:** ![Ruben\_Moraleda](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ruben_moraleda/32/2549_2.png) [@Ruben\_Moraleda](https://discuss.elastic.co/u/Ruben_Moraleda)\
**Post date:** [January 9, 2013, 11:26am UTC](https://discuss.elastic.co/t/retrieve-a-specific-field-using-java-api/6387/6 "2013-01-09T11:26:10Z")

</div>

The setFields method accepts multiple strings as parameters:

GetResponse response = client.prepareGet(index, type, id).setFields("title",  
"name","postDate").execute().actionGet();  
String title = (String)response.field("title").getValue();  
String name = (String)response.field("name").getValue();  
String postDate = (String)response.field("postDate").getValue();

El miércoles, 22 de febrero de 2012 19:02:29 UTC+1, Wes Plunk escribió:

> what about when you want multiple fields. I tried a comma delimited list  
> and it assumed the entire string was the field name. I tried calling  
> setFields for each field and it only takes the last field. I tried setting  
> setField with a String but that too failed. Is there something I'm  
> missing here?

--

---

<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, 2:57am UTC](https://discuss.elastic.co/t/retrieve-a-specific-field-using-java-api/6387/7 "2017-07-06T02:57:15Z")

</div>


