# Return specific field and highlights via Java API

**URL:** <https://discuss.elastic.co/t/return-specific-field-and-highlights-via-java-api/15333>\
**Category:** Elasticsearch\
**Created:** [January 21, 2014, 2:30am UTC](https://discuss.elastic.co/t/return-specific-field-and-highlights-via-java-api/15333 "2014-01-21T02:30:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![IronMike](https://avatars.discourse-cdn.com/v4/letter/i/e9a140/32.png) [@IronMike](https://discuss.elastic.co/u/IronMike)\
**Post date:** [January 21, 2014, 2:30am UTC](https://discuss.elastic.co/t/return-specific-field-and-highlights-via-java-api/15333/1 "2014-01-21T02:30:16Z")

</div>

I am having two issues using the java api

1. I am not able to return specific field in my search query - It shows I  
have the right number of results, but displays Null
2. Not return highlights  
Note: Assume Indexing is fine, because I am able to get correct results if  
comment out the line .AddField("hid")  
I am using default everything, I understand for highlights \_source for  
field has to be enabled, but I thought if not, it grabs the original source.

json:  
{"uid":'123", "name":"hello},  
{"uid":'1234", "name":"hello1}

node = NodeBuilder.nodeBuilder() //

```
            .local(true)//

            .data(true) //

            .node();

            client = node.client();

```

//..createIndex

private void search(String index, String type,String field, String value)

```
  {

          SearchResponse response = client.prepareSearch(index)

                      

          .setTypes(type)

         .addHighlightedField("uid")

         .addField("uid")

         SearchHit[] results = response.getHits().getHits();

            System.out.println("Current results: " + results.length);

          for (SearchHit hit : results) {

            System.out.println("------------------------------");

              Map<String,Object> result = hit.getSource();  

             System.out.println(result);

        }

    }

```

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/4984505f-9946-4855-8bf0-5dd11b0a1b21%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/4984505f-9946-4855-8bf0-5dd11b0a1b21%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![brian\_yoder](https://avatars.discourse-cdn.com/v4/letter/b/f1d935/32.png) [@brian\_yoder](https://discuss.elastic.co/u/brian_yoder)\
**Post date:** [January 21, 2014, 7:00pm UTC](https://discuss.elastic.co/t/return-specific-field-and-highlights-via-java-api/15333/2 "2014-01-21T19:00:14Z")

</div>

A couple of things:

1. If you ask for any fields, you must also ask for source or else you  
won't get the source. You get the source by default only if you don't ask  
for any fields. At least, that's the behavior I've seen.

2. If you ask for the "hid" field, then I am guessing you need to set  
"store":true or something like that in the mapping.

Other than that, I am also interesting in some examples of highlighting via  
the Java API!

Brian

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/4c6e3d8b-e178-4976-ad4e-65bc6f7d6d00%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/4c6e3d8b-e178-4976-ad4e-65bc6f7d6d00%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 1:55am UTC](https://discuss.elastic.co/t/return-specific-field-and-highlights-via-java-api/15333/3 "2017-07-06T01:55:32Z")

</div>


