# Highlighting in Java: getSourceAsString with highlighted fields

**URL:** <https://discuss.elastic.co/t/highlighting-in-java-getsourceasstring-with-highlighted-fields/14266>\
**Category:** Elasticsearch\
**Created:** [November 5, 2013, 3:22pm UTC](https://discuss.elastic.co/t/highlighting-in-java-getsourceasstring-with-highlighted-fields/14266 "2013-11-05T15:22:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Emanuel\_Buzek](https://avatars.discourse-cdn.com/v4/letter/e/df705f/32.png) [@Emanuel\_Buzek](https://discuss.elastic.co/u/Emanuel_Buzek)\
**Post date:** [November 5, 2013, 3:22pm UTC](https://discuss.elastic.co/t/highlighting-in-java-getsourceasstring-with-highlighted-fields/14266/1 "2013-11-05T15:22:49Z")

</div>

Hi,  
I just spent a few hours trying to get highlighting to work. I am using the  
Java API and I am trying to highlight the 'artist' field in my documents.  
My code looks roughly like this:

searchRequestBuilder.addHighlightedField("artist");  
...  
SearchResponse response = searchRequestBuilder.execute().actionGet();  
...  
for (SearchHit hit : response.getHits()) {  
T entity = mapper.readValue(hit.getSourceAsString().getBytes(),  
entityClass);  
}

When I execute the code, the SearchHit.getHighlightFields() contains the  
'artist' field as expected (and it's correctly highlighted with the   
_tags...) However, 'artist' in the JSON returned from getSourceAsString() is  
not highlighted. That means that the field is not highlighted in my java  
bean and in the gui... I couldn't figure out an easy way to have the fields  
in the JSON highlighted as well. I'd appreciate any pointers, I've been  
searching the docs for hours 🙂_

Thanks!  
~Emanuel

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [November 5, 2013, 3:30pm UTC](https://discuss.elastic.co/t/highlighting-in-java-getsourceasstring-with-highlighted-fields/14266/2 "2013-11-05T15:30:16Z")

</div>

On Tue, Nov 5, 2013 at 10:22 AM, Emanuel Buzek [emanuel.buzek@roke.cz](mailto:emanuel.buzek@roke.cz)wrote:

Hi,

> I just spent a few hours trying to get highlighting to work. I am using  
> the Java API and I am trying to highlight the 'artist' field in my  
> documents.  
> My code looks roughly like this:
> 
> searchRequestBuilder.addHighlightedField("artist");  
> ...  
> SearchResponse response = searchRequestBuilder.execute().actionGet();  
> ...  
> for (SearchHit hit : response.getHits()) {  
> T entity = mapper.readValue(hit.getSourceAsString().getBytes(),  
> entityClass);  
> }
> 
> When I execute the code, the SearchHit.getHighlightFields() contains the  
> 'artist' field as expected (and it's correctly highlighted with the   
> _tags...) However, 'artist' in the JSON returned from getSourceAsString() is  
> not highlighted. That means that the field is not highlighted in my java  
> bean and in the gui... I couldn't figure out an easy way to have the fields  
> in the JSON highlighted as well. I'd appreciate any pointers, I've been  
> searching the docs for hours 🙂_

 

_The highlighted fields come back in  
.highlightFields()/.getHighlightedFields(). If you want the highlighted  
values you'll have to read from those rather than the source. When I  
search I typically don't ask for the source - maybe just the id and the  
highlights. That gives me enough to build the search results without  
needing the source._

 

_--  
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).  
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, 2:08am UTC](https://discuss.elastic.co/t/highlighting-in-java-getsourceasstring-with-highlighted-fields/14266/3 "2017-07-06T02:08:47Z")

</div>


