Skip to content

Commit

Permalink
Merge pull request #60 from theablefew/esmarkowski/Highlighting
Browse files Browse the repository at this point in the history
Fix highlighting and add support for storing and retrieving highlights
  • Loading branch information
esmarkowski authored Mar 11, 2024
2 parents 0e4f143 + 239ad99 commit abe9acb
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/stretchy/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ def [](attribute)
self.send(attribute)
end

def highlights_for(attribute)
highlights[attribute.to_s]
end

class_methods do

# Set the default sort key to be used in sort operations
Expand Down
1 change: 1 addition & 0 deletions lib/stretchy/model/serialization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def serialize(document)

def deserialize(document)
attribs = ActiveSupport::HashWithIndifferentAccess.new(document['_source']).deep_symbolize_keys
attribs[:_highlights] = document["highlight"] if document["highlight"]
_id = __get_id_from_document(document)
attribs[:id] = _id if _id
klass.new attribs
Expand Down
7 changes: 7 additions & 0 deletions lib/stretchy/record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ def self.inherited(base)
# overriden by #size
default_size 10000

attr_accessor :highlights

def initialize(attributes = {})
@highlights = attributes.delete(:_highlights)
super(attributes)
end

end

def initialize(attributes = {})
Expand Down
8 changes: 7 additions & 1 deletion lib/stretchy/relations/query_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,13 @@ def build_highlights
structure.highlight do
structure.fields do
highlights.each do |highlight|
structure.set! highlight, extract_highlighter(highlight)
if highlight.is_a?(String) || highlight.is_a?(Symbol)
structure.set! highlight, {}
elsif highlight.is_a?(Hash)
highlight.each_pair do |k,v|
structure.set! k, v
end
end
end
end
end
Expand Down
18 changes: 17 additions & 1 deletion spec/stretchy/querying_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,26 @@

context 'highlight' do
it 'returns highlighted fields' do
result = described_class.highlight(:body)
result = described_class.highlight(body: {})
expected = {:highlight=>{:fields=>{body: {}}}}
expect(result.to_elastic).to eq(expected.with_indifferent_access)
end

it 'stores highlights' do
result = described_class.query_string("name: Soph*").highlight(name: {pre_tags: "__", post_tags: "__"}).first
expect(result.highlights).to eq({"name"=>["__Sophia__ Anderson"]})
end

it 'allows single symbol argument' do
result = described_class.highlight(:body)
expected = {:highlight=>{:fields=>{body: {}}}}
expect(result.to_elastic).to eq(expected.with_indifferent_access)
end

it 'highlights_for ' do
result = described_class.query_string("name: Soph*").highlight(name: {pre_tags: "__", post_tags: "__"}).first
expect(result.highlights_for(:name)).to eq(["__Sophia__ Anderson"])
end
end

it 'adds exists with has_field' do
Expand Down

0 comments on commit abe9acb

Please sign in to comment.