Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ES inner_hits support #955

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### New Features

* [#950](https://github.com/toptal/chewy/issues/950): Adds supports for inner_hits in the search request. ([@JonathanFrias][])

### Changes

### Bugs Fixed
Expand Down
16 changes: 16 additions & 0 deletions lib/chewy/search/parameters/inner_hits.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'chewy/search/parameters/storage'

module Chewy
module Search
class Parameters
# Just a standard hash storage. Nothing to see here.
#
# @see Chewy::Search::Parameters::HashStorage
# @see Chewy::Search::Request#inner_hits
# @see https://www.elastic.co/guide/en/elasticsearch/reference/current/inner-hits.html
class InnerHits < Storage
include HashStorage
end
end
end
end
18 changes: 16 additions & 2 deletions lib/chewy/search/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Request
DELEGATED_METHODS = %i[
query filter post_filter knn order reorder docvalue_fields
track_scores track_total_hits request_cache explain version profile
search_type preference limit offset terminate_after
search_type preference limit offset terminate_after inner_hits
timeout min_score source stored_fields search_after
load script_fields suggest aggs aggregations collapse none
indices_boost rescore highlight total total_count
Expand All @@ -41,7 +41,7 @@ class Request
EXTRA_STORAGES = %i[aggs suggest].freeze
# An array of storage names that are changing the returned hist collection in any way.
WHERE_STORAGES = %i[
query filter post_filter knn none min_score rescore indices_boost collapse
query filter post_filter inner_hits knn none min_score rescore indices_boost collapse
].freeze

delegate :hits, :wrappers, :objects, :records, :documents,
Expand Down Expand Up @@ -537,6 +537,20 @@ def reorder(value, *values)
end
end

# @!method inner_hits(value)
# Updates `inner_hits` request part
#
# @example
# PlacesIndex.inner_hits(attendees: {size: 5})
# # => <PlacesIndex::Query {..., :body=>{:inner_hits=>{:attendees=>{:size=>5}}}}>
# @see Chewy::Search::Parameters::InnerHits
# @see https://www.elastic.co/guide/en/elasticsearch/reference/current/inner-hits.html
# @param value [Hash]
# @return [Chewy::Search::Request]
def inner_hits(value)
modify(:inner_hits) { update!(value) }
end

# @!method source(*values)
# Updates `_source` request part. Accepts either an array
# of field names/templates or a hash with `includes` and `excludes`
Expand Down
5 changes: 5 additions & 0 deletions spec/chewy/search/parameters/inner_hits_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'chewy/search/parameters/hash_storage_examples'

describe Chewy::Search::Parameters::InnerHits do
it_behaves_like :hash_storage, :inner_hits
end
2 changes: 1 addition & 1 deletion spec/chewy/search/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@
end
end

%i[collapse knn].each do |name|
%i[collapse knn inner_hits].each do |name|
describe "##{name}" do
specify { expect(subject.send(name, foo: {bar: 42}).render[:body]).to include(name => {'foo' => {bar: 42}}) }
specify do
Expand Down