# How to retrieving all fields with Java client?

**URL:** https://discuss.elastic.co/t/how-to-retrieving-all-fields-with-java-client/8572
**Category:** Elasticsearch
**Created:** [July 31, 2012, 12:10am UTC](https://discuss.elastic.co/t/how-to-retrieving-all-fields-with-java-client/8572 "2012-07-31T00:10:24Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Yuhan](https://avatars.discourse-cdn.com/v4/letter/y/919ad9/32.png) [@Yuhan](https://discuss.elastic.co/u/Yuhan)
#### Post date: [July 31, 2012, 12:10am UTC](https://discuss.elastic.co/t/how-to-retrieving-all-fields-with-java-client/8572/1 "2012-07-31T00:10:24Z")

</div>

Hi all,

In case receiving a certain fields from a doc, I could construct query in  
this way:

```
        SearchRequestBuilder searchBuilder = client.prepareSearch(

```

indexName, indexType )  
.setSearchType(SearchType.DFS\_QUERY\_THEN\_FETCH)  
.setQuery( builder )  
.addField(field)  
.setFrom(0).setSize( urls.length );

I'm trying to perform a search query on an index with all the fields  
returned without explicitly specifying them.  
however, if I left without "addField", it returns docs without any fields.

is there a way I could retrieve this with ES java client?

Thank you.

Yuhan

---

<div class="post-metadata">

### Author: ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)
#### Post date: [July 31, 2012, 12:13am UTC](https://discuss.elastic.co/t/how-to-retrieving-all-fields-with-java-client/8572/2 "2012-07-31T00:13:50Z")

</div>

You can use wildcards in the field name: .addField("\*")

Alternatively, if you have source enabled (the default), you can  
extract the fields from the source: SearchHit#getSource().

Cheers,

Ivan

On Mon, Jul 30, 2012 at 5:10 PM, Yuhan Zhang [yzhang@onescreen.com](mailto:yzhang@onescreen.com) wrote:

> Hi all,
> 
> In case receiving a certain fields from a doc, I could construct query in  
> this way:
> 
> ```
> SearchRequestBuilder searchBuilder = client.prepareSearch(
> 
> ```
> 
> indexName, indexType )  
> .setSearchType(SearchType.DFS\_QUERY\_THEN\_FETCH)  
> .setQuery( builder )  
> .addField(field)  
> .setFrom(0).setSize( urls.length );
> 
> I'm trying to perform a search query on an index with all the fields  
> returned without explicitly specifying them.  
> however, if I left without "addField", it returns docs without any fields.
> 
> is there a way I could retrieve this with ES java client?
> 
> Thank you.
> 
> Yuhan

---

<div class="post-metadata">

### Author: ![Yuhan](https://avatars.discourse-cdn.com/v4/letter/y/919ad9/32.png) [@Yuhan](https://discuss.elastic.co/u/Yuhan)
#### Post date: [July 31, 2012, 12:50am UTC](https://discuss.elastic.co/t/how-to-retrieving-all-fields-with-java-client/8572/3 "2012-07-31T00:50:29Z")

</div>

Hi Ivan,

.addField("\*") didn't work in my case, It returns me 0 with  
searchResponse.getHits().getHits()[0].getFields().values().size() ...

I tried to get the source but received null from  
response.getHits().getHits()[0].getSource()

I'm wondering what I did wrong... I'm using the 0.19.4 client.

Thank you.

Yuhan

On Mon, Jul 30, 2012 at 5:13 PM, Ivan Brusic [ivan@brusic.com](mailto:ivan@brusic.com) wrote:

> You can use wildcards in the field name: .addField("\*")
> 
> Alternatively, if you have source enabled (the default), you can  
> extract the fields from the source: SearchHit#getSource().
> 
> Cheers,
> 
> Ivan
> 
> On Mon, Jul 30, 2012 at 5:10 PM, Yuhan Zhang [yzhang@onescreen.com](mailto:yzhang@onescreen.com) wrote:
> 
> > Hi all,
> > 
> > In case receiving a certain fields from a doc, I could construct query in  
> > this way:
> > 
> > ```
> > SearchRequestBuilder searchBuilder = client.prepareSearch(
> > 
> > ```
> > 
> > indexName, indexType )  
> > .setSearchType(SearchType.DFS\_QUERY\_THEN\_FETCH)  
> > .setQuery( builder )  
> > .addField(field)  
> > .setFrom(0).setSize( urls.length );
> > 
> > I'm trying to perform a search query on an index with all the fields  
> > returned without explicitly specifying them.  
> > however, if I left without "addField", it returns docs without any  
> > fields.
> > 
> > is there a way I could retrieve this with ES java client?
> > 
> > Thank you.
> > 
> > Yuhan

--  
Yuhan Zhang  
Senior Software Engineer  
OneScreen Inc.  
[yzhang@onescreen.com](mailto:yzhang@onescreen.com) [ehorne@onescreen.com](mailto:ehorne@onescreen.com)  
[www.onescreen.com](http://www.onescreen.com)  
(949) 525-4825 Ext: 177

The information contained in this e-mail is for the exclusive use of the  
intended recipient(s) and may be confidential, proprietary, and/or legally  
privileged. Inadvertent disclosure of this message does not constitute a  
waiver of any privilege. If you receive this message in error, please do  
not directly or indirectly print, copy, retransmit, disseminate, or  
otherwise use the information. In addition, please delete this e-mail and  
all copies and notify the sender.

---

<div class="post-metadata">

### Author: ![vkaggal](https://avatars.discourse-cdn.com/v4/letter/v/8e7dd6/32.png) [@vkaggal](https://discuss.elastic.co/u/vkaggal)
#### Post date: [September 20, 2012, 9:12pm UTC](https://discuss.elastic.co/t/how-to-retrieving-all-fields-with-java-client/8572/4 "2012-09-20T21:12:18Z")

</div>

Hello, I am wondering if you ever solved this issue?

I am using elasticsearch-0.19.9 and the following still does not work?

SearchRequestBuilder builder = client.prepareSearch("idx1002")  
.setSearchType(SearchType.DEFAULT)  
.setQuery(qb1).addField("\*");

Example:  
{  
a:{  
b: { attr: "value" }  
}  
}

IF I need to retrieve the field "attr" in the resulting documents, should I be adding a.b.attr via .addField("a.b.attr")?

Any help to retrieve specific field that is nested (example above) would be appreciated.

Thanks.

---

<div class="post-metadata">

### Author: ![Vinod\_2](https://avatars.discourse-cdn.com/v4/letter/v/d6d6ee/32.png) [@Vinod\_2](https://discuss.elastic.co/u/Vinod_2)
#### Post date: [October 12, 2012, 4:26pm UTC](https://discuss.elastic.co/t/how-to-retrieving-all-fields-with-java-client/8572/5 "2012-10-12T16:26:28Z")

</div>

Have you had any luck in retrieving the fields?

On Monday, July 30, 2012 7:10:24 PM UTC-5, Yuhan wrote:

> Hi all,
> 
> In case receiving a certain fields from a doc, I could construct query in  
> this way:
> 
> ```
> SearchRequestBuilder searchBuilder = client.prepareSearch( 
> 
> ```
> 
> indexName, indexType )  
> .setSearchType(SearchType.DFS\_QUERY\_THEN\_FETCH)  
> .setQuery( builder )  
> .addField(field)  
> .setFrom(0).setSize( urls.length );
> 
> I'm trying to perform a search query on an index with all the fields  
> returned without explicitly specifying them.  
> however, if I left without "addField", it returns docs without any fields.
> 
> is there a way I could retrieve this with ES java client?
> 
> Thank you.
> 
> Yuhan

--

---

<div class="post-metadata">

### Author: ![Yuhan](https://avatars.discourse-cdn.com/v4/letter/y/919ad9/32.png) [@Yuhan](https://discuss.elastic.co/u/Yuhan)
#### Post date: [October 16, 2012, 6:07pm UTC](https://discuss.elastic.co/t/how-to-retrieving-all-fields-with-java-client/8572/6 "2012-10-16T18:07:18Z")

</div>

Hi Vinod,

the field names of an index can be read from its mapping 🙂

[http://localhost:9200/my\_index/\_mapping?pretty=1](http://localhost:9200/my_index/_mapping?pretty=1)  
[http://localhost:9200/my\_index/my\_type/\_mapping?pretty=1](http://localhost:9200/my_index/my_type/_mapping?pretty=1)

Yuhan

On Fri, Oct 12, 2012 at 9:26 AM, Vinod [vkaggal@gmail.com](mailto:vkaggal@gmail.com) wrote:

> Have you had any luck in retrieving the fields?
> 
> On Monday, July 30, 2012 7:10:24 PM UTC-5, Yuhan wrote:
> 
> > Hi all,
> > 
> > In case receiving a certain fields from a doc, I could construct query in  
> > this way:
> > 
> > ```
> > SearchRequestBuilder searchBuilder = client.prepareSearch(
> > 
> > ```
> > 
> > indexName, indexType )  
> > .setSearchType(SearchType.DFS\_\*\*QUERY\_THEN\_FETCH)  
> > .setQuery( builder )  
> > .addField(field)  
> > .setFrom(0).setSize( urls.length );
> > 
> > I'm trying to perform a search query on an index with all the fields  
> > returned without explicitly specifying them.  
> > however, if I left without "addField", it returns docs without any fields.
> > 
> > is there a way I could retrieve this with ES java client?
> > 
> > Thank you.
> > 
> > Yuhan
> 
> --

--  
Yuhan Zhang  
Senior Software Engineer  
OneScreen Inc.  
[yzhang@onescreen.com](mailto:yzhang@onescreen.com) [ehorne@onescreen.com](mailto:ehorne@onescreen.com)  
[www.onescreen.com](http://www.onescreen.com)  
(949) 525-4825 Ext: 177

The information contained in this e-mail is for the exclusive use of the  
intended recipient(s) and may be confidential, proprietary, and/or legally  
privileged. Inadvertent disclosure of this message does not constitute a  
waiver of any privilege. If you receive this message in error, please do  
not directly or indirectly print, copy, retransmit, disseminate, or  
otherwise use the information. In addition, please delete this e-mail and  
all copies and notify the sender.

--

---

<div class="post-metadata">

### Author: ![vkaggal](https://avatars.discourse-cdn.com/v4/letter/v/8e7dd6/32.png) [@vkaggal](https://discuss.elastic.co/u/vkaggal)
#### Post date: [October 16, 2012, 9:50pm UTC](https://discuss.elastic.co/t/how-to-retrieving-all-fields-with-java-client/8572/7 "2012-10-16T21:50:31Z")

</div>

Thank you Yuhan. I will used the \_mapping before, however, I was wondering  
if there was some way to do this using Java API.

Thanks.

On Tue, Oct 16, 2012 at 1:07 PM, Yuhan [via Elasticsearch Users] \<  
[ml-node+s115913n4024090h58@n3.nabble.com](mailto:ml-node+s115913n4024090h58@n3.nabble.com)\> wrote:

> Hi Vinod,
> 
> the field names of an index can be read from its mapping 🙂
> 
> [http://localhost:9200/my\_index/\_mapping?pretty=1](http://localhost:9200/my_index/_mapping?pretty=1)  
> [http://localhost:9200/my\_index/my\_type/\_mapping?pretty=1](http://localhost:9200/my_index/my_type/_mapping?pretty=1)
> 
> Yuhan
> 
> On Fri, Oct 12, 2012 at 9:26 AM, Vinod \<[hidden email][http://user/SendEmail.jtp?type=node&node=4024090&i=0](http://user/SendEmail.jtp?type=node&node=4024090&i=0)
> 
> > wrote:
> 
> > Have you had any luck in retrieving the fields?
> > 
> > On Monday, July 30, 2012 7:10:24 PM UTC-5, Yuhan wrote:
> > 
> > > Hi all,
> > > 
> > > In case receiving a certain fields from a doc, I could construct query  
> > > in this way:
> > > 
> > > ```
> > > SearchRequestBuilder searchBuilder = client.prepareSearch(
> > > 
> > > ```
> > > 
> > > indexName, indexType )  
> > > .setSearchType(SearchType.DFS\_\*\*QUERY\_THEN\_FETCH)  
> > > .setQuery( builder )  
> > > .addField(field)  
> > > .setFrom(0).setSize( urls.length );
> > > 
> > > I'm trying to perform a search query on an index with all the fields  
> > > returned without explicitly specifying them.  
> > > however, if I left without "addField", it returns docs without any  
> > > fields.
> > > 
> > > is there a way I could retrieve this with ES java client?
> > > 
> > > Thank you.
> > > 
> > > Yuhan
> > 
> > --
> 
> --  
> Yuhan Zhang  
> Senior Software Engineer  
> OneScreen Inc.  
> [hidden email] [http://user/SendEmail.jtp?type=node&node=4024090&i=1](http://user/SendEmail.jtp?type=node&node=4024090&i=1)
> 
> [www.onescreen.com](http://www.onescreen.com)  
> (949) 525-4825 Ext: 177
> 
> The information contained in this e-mail is for the exclusive use of the  
> intended recipient(s) and may be confidential, proprietary, and/or legally  
> privileged. Inadvertent disclosure of this message does not constitute a  
> waiver of any privilege. If you receive this message in error, please do  
> not directly or indirectly print, copy, retransmit, disseminate, or  
> otherwise use the information. In addition, please delete this e-mail and  
> all copies and notify the sender.
> 
> --
> 
> * * *
> 
> If you reply to this email, your message will be added to the discussion  
> below:
> 
> [http://elasticsearch-users.115913.n3.nabble.com/how-to-retrieving-all-fields-with-Java-client-tp4021061p4024090.html](http://elasticsearch-users.115913.n3.nabble.com/how-to-retrieving-all-fields-with-Java-client-tp4021061p4024090.html)  
> To unsubscribe from how to retrieving all fields with Java client?, click  
> here[http://elasticsearch-users.115913.n3.nabble.com/template/NamlServlet.jtp?macro=unsubscribe\_by\_code&node=4021061&code=dmthZ2dhbEBob3RtYWlsLmNvbXw0MDIxMDYxfDIwMzI4OTI0OTQ=](http://elasticsearch-users.115913.n3.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=4021061&code=dmthZ2dhbEBob3RtYWlsLmNvbXw0MDIxMDYxfDIwMzI4OTI0OTQ=)  
> .  
> NAML[http://elasticsearch-users.115913.n3.nabble.com/template/NamlServlet.jtp?macro=macro\_viewer&id=instant\_html!nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify\_subscribers!nabble%3Aemail.naml-instant\_emails!nabble%3Aemail.naml-send\_instant\_email!nabble%3Aemail.naml](http://elasticsearch-users.115913.n3.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml)

--  
Vinod C K  
ವಿನೋದ್ ಸಿ ಕೆ

---

<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:08am UTC](https://discuss.elastic.co/t/how-to-retrieving-all-fields-with-java-client/8572/8 "2017-07-06T03:08:23Z")

</div>


