# SearchHit.fields() returns null even though fields are there

**URL:** <https://discuss.elastic.co/t/searchhit-fields-returns-null-even-though-fields-are-there/3429>\
**Category:** Elasticsearch\
**Created:** [October 9, 2010, 1:00am UTC](https://discuss.elastic.co/t/searchhit-fields-returns-null-even-though-fields-are-there/3429 "2010-10-09T01:00:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Chang](https://avatars.discourse-cdn.com/v4/letter/j/ecc23a/32.png) [@John\_Chang](https://discuss.elastic.co/u/John_Chang)\
**Post date:** [October 9, 2010, 1:00am UTC](https://discuss.elastic.co/t/searchhit-fields-returns-null-even-though-fields-are-there/3429/1 "2010-10-09T01:00:58Z")

</div>

Just upgraded from 0.9 to 0.11.0 and I find that org.elasticsearch.search.SearchHit.fields() is returning null even when I know the requested fields are there. When I do the query from the url through curl (including the requested fields), they show up fine in the returned json.

How I query:  
response = indexClient.search(Requests.searchRequest(getIndexName()).types(documentTypes).searchType(SearchType.QUERY\_THEN\_FETCH)  
.source(SearchSourceBuilder.searchSource().query(QueryBuilders.customScoreQuery(QueryBuilders.queryString(query)).script("doc['" + sortField + "'].value")).fields(fields).from(0).size(maxHitsPerIndexQuery).explain(true))

How I get the results:  
List\<Map\<String, List\>\> results = new ArrayList\<Map\<String, List\>\>();  
for(SearchHit hit : response.hits().hits()) {

```
        Map<String, List<Object>> fieldMap = new HashMap<String, List<Object>>();

        for (Map.Entry<String, SearchHitField> field : hit.fields().entrySet()) { //NPE here
            fieldMap.put(field.getKey(), field.getValue().getValues()); 
        }

        results.add(fieldMap);
    }

```

Note this is not a problem when I start local nodes in my unit tests; I only see this problem when I connect to a true stand-alone node started from the command line.

Thanks.

---

<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:** [October 9, 2010, 1:49am UTC](https://discuss.elastic.co/t/searchhit-fields-returns-null-even-though-fields-are-there/3429/2 "2010-10-09T01:49:18Z")

</div>

Sounds like a serialization problem... . Is there a chance that you can  
create a simple test case and send it to me?

On Sat, Oct 9, 2010 at 3:00 AM, John Chang [jchangkihtest2@gmail.com](mailto:jchangkihtest2@gmail.com) wrote:

> Just upgraded from 0.9 to 0.11.0 and I find that  
> org.elasticsearch.search.SearchHit.fields() is returning null even when I  
> know the requested fields are there. When I do the query from the url  
> through curl (including the requested fields), they show up fine in the  
> returned json.
> 
> How I query:  
> response =
> 
> indexClient.search(Requests.searchRequest(getIndexName()).types(documentTypes).searchType(SearchType.QUERY\_THEN\_FETCH)
> 
> .source(SearchSourceBuilder.searchSource().query(QueryBuilders.customScoreQuery(QueryBuilders.queryString(query)).script("doc['"
> 
> - sortField +
> 
> "'].value")).fields(fields).from(0).size(maxHitsPerIndexQuery).explain(true))
> 
> How I get the results:  
> List\<Map\<String, List\>\> results = new ArrayList\<Map\<String,  
> List\>\>();  
> for(SearchHit hit : response.hits().hits()) {
> 
> ```
> Map<String, List> fieldMap = new HashMap<String, List>();
> 
> for (Map.Entry<String, SearchHitField> field :
> 
> ```
> 
> hit.fields().entrySet()) { //NPE here  
> fieldMap.put(field.getKey(), field.getValue().getValues());  
> }
> 
> ```
> results.add(fieldMap);
> }
> 
> ```
> 
> Note this is not a problem when I start local nodes in my unit tests; I  
> only  
> see this problem when I connect to a true stand-alone node started from the  
> command line.
> 
> ## Thanks.
> 
> View this message in context:  
> [http://elasticsearch-users.115913.n3.nabble.com/SearchHit-fields-returns-null-even-though-fields-are-there-tp1668529p1668529.html](http://elasticsearch-users.115913.n3.nabble.com/SearchHit-fields-returns-null-even-though-fields-are-there-tp1668529p1668529.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![John\_Chang](https://avatars.discourse-cdn.com/v4/letter/j/ecc23a/32.png) [@John\_Chang](https://discuss.elastic.co/u/John_Chang)\
**Post date:** [October 11, 2010, 7:13am UTC](https://discuss.elastic.co/t/searchhit-fields-returns-null-even-though-fields-are-there/3429/3 "2010-10-11T07:13:39Z")

</div>

Thanks for the offer, but I think I may have figured it out.

I simplified my client setup to the below snippet. I took out the index conifg stuff (as you advised in a different thread) because I was setting client(true). After doing this, the null fields problem went away. Does that make sense; would that explain it?

BTW - the problem was erratic. It generally gave me back null fields, but sometimes they where non-null but empty, and sometimes they were non-null and properly populated.

At any rate, since dropping the index config and simplifying the client setup to what you see below, it seems to work, at least for now.

```
private void initClient() {
    //elasticSearchHostsList should only ever be null or empty in unit test invocation
    if (elasticSearchHostsList != null && !elasticSearchHostsList.trim().equals("")) {
        NodeBuilder nb = NodeBuilder.nodeBuilder().client(true);
        nb.settings().put("discovery.zen.ping.unicast.hosts", elasticSearchHostsList);

        //cluster name must change as per environment to avoid s3 path clashes -- only in junit test invocation should it be left to default
        if (elasticSearchClusterName != null && !elasticSearchClusterName.trim().equals("")) {
            nb.settings().put("cluster.name", elasticSearchClusterName);
        }

        this.indexClient = nb.node().client();

        logger.info("Successfully initialized client with Elastic Search hosts " + elasticSearchHostsList);
    }

}
```

---

<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:** [October 11, 2010, 4:07pm UTC](https://discuss.elastic.co/t/searchhit-fields-returns-null-even-though-fields-are-there/3429/4 "2010-10-11T16:07:57Z")

</div>

Strange, this should not have fixed the problem ... . Is there a chance, if  
you have time, for a recreation of this so I can delve into why this was  
happening?

On Mon, Oct 11, 2010 at 9:13 AM, John Chang [jchangkihtest2@gmail.com](mailto:jchangkihtest2@gmail.com)wrote:

> Thanks for the offer, but I think I may have figured it out.
> 
> I simplified my client setup to the below snippet. I took out the index  
> conifg stuff (as you advised in a different thread) because I was setting  
> client(true). After doing this, the null fields problem went away. Does  
> that make sense; would that explain it?
> 
> BTW - the problem was erratic. It generally gave me back null fields, but  
> sometimes they where non-null but empty, and sometimes they were non-null  
> and properly populated.
> 
> At any rate, since dropping the index config and simplifying the client  
> setup to what you see below, it seems to work, at least for now.
> 
> private void initClient() {  
> //elasticSearchHostsList should only ever be null or empty in unit  
> test invocation  
> if (elasticSearchHostsList != null &&  
> !elasticSearchHostsList.trim().equals("")) {  
> NodeBuilder nb = NodeBuilder.nodeBuilder().client(true);  
> nb.settings().put("discovery.zen.ping.unicast.hosts",  
> elasticSearchHostsList);
> 
> ```
> //cluster name must change as per environment to avoid s3 path
> 
> ```
> 
> clashes -- only in junit test invocation should it be left to default  
> if (elasticSearchClusterName != null &&  
> !elasticSearchClusterName.trim().equals("")) {  
> nb.settings().put("cluster.name",  
> elasticSearchClusterName);  
> }
> 
> ```
> this.indexClient = nb.node().client();
> 
> logger.info("Successfully initialized client with Elastic
> 
> ```
> 
> Search  
> hosts " + elasticSearchHostsList);  
> }
> 
> ## }
> 
> View this message in context:  
> [http://elasticsearch-users.115913.n3.nabble.com/SearchHit-fields-returns-null-even-though-fields-are-there-tp1668529p1678640.html](http://elasticsearch-users.115913.n3.nabble.com/SearchHit-fields-returns-null-even-though-fields-are-there-tp1668529p1678640.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![John\_Chang](https://avatars.discourse-cdn.com/v4/letter/j/ecc23a/32.png) [@John\_Chang](https://discuss.elastic.co/u/John_Chang)\
**Post date:** [October 11, 2010, 11:24pm UTC](https://discuss.elastic.co/t/searchhit-fields-returns-null-even-though-fields-are-there/3429/5 "2010-10-11T23:24:01Z")

</div>

I'll try, but as I said, it is inconsistent. When I tried to write a little stand-alone test case to repro the problem, it worked fine. Of course, I can't ship you my whole project for numerous reasons. I'll try again to get a stand-alone repro test case to send you, but it may be elusive. THanks.

---

<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:** [October 12, 2010, 12:37am UTC](https://discuss.elastic.co/t/searchhit-fields-returns-null-even-though-fields-are-there/3429/6 "2010-10-12T00:37:15Z")

</div>

Hey,

If you do manage to recreate it, it would be great. I will try and run  
some tests on my end to see if I can reproduce this as well. If its a bug, I  
would love to nail it.

-shay.banon

On Tue, Oct 12, 2010 at 1:24 AM, John Chang [jchangkihtest2@gmail.com](mailto:jchangkihtest2@gmail.com)wrote:

> ## I'll try, but as I said, it is inconsistent. When I tried to write a little stand-alone test case to repro the problem, it worked fine. Of course, I can't ship you my whole project for numerous reasons. I'll try again to get a stand-alone repro test case to send you, but it may be elusive. THanks.
> 
> View this message in context:  
> [http://elasticsearch-users.115913.n3.nabble.com/SearchHit-fields-returns-null-even-though-fields-are-there-tp1668529p1684188.html](http://elasticsearch-users.115913.n3.nabble.com/SearchHit-fields-returns-null-even-though-fields-are-there-tp1668529p1684188.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<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:18am UTC](https://discuss.elastic.co/t/searchhit-fields-returns-null-even-though-fields-are-there/3429/7 "2017-07-06T04:18:06Z")

</div>


