I have a simple sounding problem according highlighting of a field with elasticsearch version 0.18.6. What I want to do is to highlight a complete field and get the content back (either highlighted or if there are no matches then without highlighting – but always the full field content!)
On elasticsearch.org I found this:
If the number_of_fragments value is set to 0 then no fragments are produced, instead the whole content of the field is returned, and of course it is highlighted.
So I tried this to achieve what I want:
final SearchResponse response = client.prepareSearch(indexName)
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setTypes(FEED_TYPE)
.setQuery(queryString(query).field("title").field("description").field("keywords"))
.addHighlightedField("title", 0, 0)
.addHighlightedField("description", 0, 0)
.addFields("url", "iconUrl", "keywords")
.setSize(size)
.setFrom(start)
.execute().actionGet();
The Signature of this method is:
public SearchRequestBuilder addHighlightedField(java.lang.String name, int fragmentSize, int numberOfFragments)
Do I miss something here? Or did I misunderstand that it only returns the full content if there is at least one term to hightlight?
Thanks for all answers.
I don’t think it’s possible to achieve exactly what you want at the moment. The highlightFields collection contains only fields that were highlighted. You can do something like this:
It will always return you all fields in
and fields that were highlighted in
While displaying results, you can check highlightFields() first and if a field is not present there, use fields() instead.