Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Showing better highlighted search result fragments with Elasticsearch
def ellipses_for_highlights(params_highlight, params_original)
# have to do this because highlighted stuff from ES has a trailing space for whatever reason
stripped_highlighted_item = strip_tags(params_highlight).rstrip
# if the beginning of the highlighted text doesn't match the original it has been clipped
tmp = params_original =~ /#{stripped_highlighted_item}/
front_ellipsis = tmp != 0
# if the last 10 characters of the highlighted text don't match the original, same deal
back_ellipsis = last_string_chars(stripped_highlighted_item, 10) != last_string_chars(params_original, 10)
highlighted_item = front_ellipsis ? "... " + params_highlight : params_highlight
highlighted_item = back_ellipsis ? highlighted_item + " ..." : highlighted_item
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment