Highlighting in Java: getSourceAsString with highlighted fields


(Emanuel Buzek) #1

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 :slight_smile:

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.
For more options, visit https://groups.google.com/groups/opt_out.


(Nik Everett) #2

On Tue, Nov 5, 2013 at 10:22 AM, Emanuel Buzek emanuel.buzek@roke.czwrote:

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 :slight_smile:

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.
For more options, visit https://groups.google.com/groups/opt_out.


(system) #3