Skip to content

Commit

Permalink
[MODEL] Ensure that specified ActiveRecord order is not overwritten b…
Browse files Browse the repository at this point in the history
…y Elasticsearch query order
  • Loading branch information
estolfo committed Sep 13, 2018
1 parent 6c00612 commit c267c3b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
Expand Up @@ -35,7 +35,11 @@ def records
else
self.__send__(:exec_queries)
end
@records.sort_by { |record| hits.index { |hit| hit['_id'].to_s == record.id.to_s } }
if !self.order_values.present?
@records.sort_by { |record| hits.index { |hit| hit['_id'].to_s == record.id.to_s } }
else
@records
end
end if self
end

Expand Down
Expand Up @@ -231,6 +231,13 @@ def as_indexed_json(options = {})
end
end

should "allow ordering following any method chain in SQL" do
if defined?(::ActiveRecord) && ::ActiveRecord::VERSION::MAJOR >= 4
response = Article.search query: { match: { title: { query: 'test' } } }
assert_equal 'Testing Coding', response.records.distinct.order(id: :desc).first.title
end
end

should "allow dot access to response" do
response = Article.search query: { match: { title: { query: 'test' } } },
aggregations: {
Expand Down

0 comments on commit c267c3b

Please sign in to comment.