# Java API Highlighting

**URL:** <https://discuss.elastic.co/t/java-api-highlighting/20591>\
**Category:** Elasticsearch\
**Created:** [November 6, 2014, 12:36am UTC](https://discuss.elastic.co/t/java-api-highlighting/20591 "2014-11-06T00:36:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![William\_Bowen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/william_bowen/32/1057_2.png) [@William\_Bowen](https://discuss.elastic.co/u/William_Bowen)\
**Post date:** [November 6, 2014, 12:36am UTC](https://discuss.elastic.co/t/java-api-highlighting/20591/1 "2014-11-06T00:36:23Z")

</div>

I'm new to Elasticsearch and I have two questions. I've done quite a bit of  
Google searching and looked at the Elasticsearch tests for guidance, but  
I'm not getting the desired result.

I'm querying an Elasticsearch server I don't control, but the index seems  
straight forward enough. When I query it via the Java API however, I cannot  
get highlights to work. My code looks more or less like this:

SearchRequestBuilder builder = client.prepareSearch(SEARCH\_INDEX)  
.setTypes(SEARCH\_DOCUMENT\_TYPE)  
.setSearchType(SearchType.QUERY\_AND\_FETCH)  
.addHighlightedField("content")  
.addHighlightedField("title")  
.addHighlightedField("subject")  
.addAggregation(AggregationBuilders.terms("types-count").field("type"))  
.setQuery(QueryBuilders.simpleQueryString(query))  
.setQuery(QueryBuilders.queryString(query)).execute().actionGet();

I get back results, but I don't get back a "highlight" collection. I get  
back this instead:

"highlightFields": {  
"content": {  
"name": "content",  
"fragments": [  
{},  
{}  
]  
},  
"title": {  
"name": "title",  
"fragments": [  
{}  
]  
}  
}

In other words, it seems like it wanted to find fragments, but it gave me  
blank results. Thinking maybe it was something wrong with the index, I  
POSTed this to the server:

{ "highlight" : { "fields" : { "content" : {} }  
}  
, "fields": ["title", "\_id", "subject", "uri", "dateEffective"]  
}

And got the expected results:

"highlight": {  
"content": [  
" fuel transfer _pump_ operation,  
\r\ninstall Javelin _Placard_ 782-14 or an equivalent FAA-approved  
_placard_",  
"73-17-01 _CESSNA_: Amdt. 39-1701.  
Applies to all series of _Cessna_ Models 170, \r\n172, 175, 180 and"  
,  
", adjacent \r\nto the fuel transfer _pump_  
switch which reads as follows: "AUXILIARY FUEL \r\nTRANSFER _PUMP_",  
". PULL ON. PUSH OFF. PLACE _PUMP_ OFF  
BEFORE CHANGING FROM LEFT \r\nTANK." \r\n\r\n\tNOTE: Failure to turn",  
" off fuel boost _pump_ allows air to enter  
engine \r\nfuel system and may cause engine power failure when"  
]  
}

It's obvious I'm doing something wrong in the API, but I can't seem to get  
it to perform the highlighting I want. Any help?

This leads me to my second question. When trying to debug this, I wanted to  
view the "source" or JSON of the request being sent to Elasticsearch so I  
could compare but I couldn't find a method for this. I resorted to sniffing  
my traffic with Wireshark, but it was not very efficient for viewing what I  
was sending to the server.

Is there a log level or something I can set in the Java API?

Thanks!

--  
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/50112bd3-6d3e-4c23-998c-6a609cfdbf7d%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/50112bd3-6d3e-4c23-998c-6a609cfdbf7d%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![mbertani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mbertani/32/11398_2.png) [@mbertani](https://discuss.elastic.co/u/mbertani)\
**Post date:** [April 20, 2017, 11:53am UTC](https://discuss.elastic.co/t/java-api-highlighting/20591/2 "2017-04-20T11:53:03Z")

</div>

You have probably found an answer to your question. But it seems there is little here about examples for the java API and highlighting.

I recently found this page, maybe it helps you with another way of doing things?  
[http://www.programcreek.com/java-api-examples/index.php?api=org.elasticsearch.search.highlight.HighlightField](http://www.programcreek.com/java-api-examples/index.php?api=org.elasticsearch.search.highlight.HighlightField)

---

<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 5, 2017, 10:01pm UTC](https://discuss.elastic.co/t/java-api-highlighting/20591/3 "2017-07-05T22:01:00Z")

</div>


