Retrieve a specific field using Java API

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'"
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

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 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'"
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

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

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 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'"
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

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?

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?

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?

--