Skip to content

Commit

Permalink
Add unlimited method to fetch all records from ES
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Kintsel committed May 27, 2016
1 parent 7420757 commit aa6af74
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
16 changes: 13 additions & 3 deletions lib/chewy/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -894,8 +894,8 @@ def types! *params
# scope = UsersIndex.aggs(max_age: { max: { field: 'age' } }).search_type(:count)
# max_age = scope.aggs['max_age']['value']
#
def search_type val
chain { options.merge!(search_type: val) }
def search_type value
chain { criteria.update_search_options search_type: value }
end

# Merges two queries.
Expand Down Expand Up @@ -961,6 +961,16 @@ def exists?
search_type(:count).total > 0
end

# Sets limit to be equal to total documents count
#
# PlacesIndex.query(...).filter(...).unlimited
#

def unlimited
size = search_type(:count).total
chain { criteria.update_request_options size: Integer(size), from: 0 }
end

# Returns request total time elapsed as reported by elasticsearch
#
# UsersIndex.query(...).filter(...).took
Expand Down Expand Up @@ -1004,7 +1014,7 @@ def _request
@_request ||= begin
request = criteria.request_body
request.merge!(index: _indexes.map(&:index_name), type: _types.map(&:type_name))
request.merge!(search_type: options[:search_type]) if options[:search_type]
request.merge!(search_type: criteria.search_options[:search_type]) if criteria.search_options[:search_type]
request
end
end
Expand Down
6 changes: 5 additions & 1 deletion lib/chewy/query/criteria.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Query
class Criteria
include Compose
ARRAY_STORAGES = [:queries, :filters, :post_filters, :sort, :fields, :types, :scores]
HASH_STORAGES = [:options, :request_options, :facets, :aggregations, :suggest, :script_fields]
HASH_STORAGES = [:options, :request_options, :facets, :aggregations, :suggest, :script_fields, :search_options]
STORAGES = ARRAY_STORAGES + HASH_STORAGES

def initialize options = {}
Expand Down Expand Up @@ -48,6 +48,10 @@ def update_request_options(modifier)
request_options.merge!(modifier)
end

def update_search_options(modifier)
search_options.merge!(modifier)
end

def update_facets(modifier)
facets.merge!(modifier)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/chewy/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module Search
:boost_factor, :weight, :random_score, :field_value_factor, :decay, :aggregations,
:suggest, :none, :strategy, :query, :filter, :post_filter, :boost_mode,
:score_mode, :order, :reorder, :only, :types, :delete_all, :find, :total,
:total_count, :total_entries, to: :all
:total_count, :total_entries, :unlimited, to: :all
end

module ClassMethods
Expand Down
16 changes: 14 additions & 2 deletions spec/chewy/query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@
end

describe '#exists?' do
let(:data) { 10.times.map { |i| {id: i.next.to_s, name: "Name#{i.next}", age: 10 * i.next}.stringify_keys! } }
let(:data) { Array.new(10) { |i| {id: i.next.to_s, name: "Name#{i.next}", age: 10 * i.next}.stringify_keys! } }

before { ProductsIndex::Product.import!(data.map { |h| double(h) }) }

Expand All @@ -464,6 +464,18 @@
specify { expect(subject.filter(range: {age: {lt: 0}}).exists?).to eq false }
end

describe '#unlimited' do
let(:data_length) { 10 }
let(:data) { Array.new(data_length) { |i| {id: i.next.to_s, name: "Name#{i.next}", age: 10 * i.next}.stringify_keys! } }

before { ProductsIndex::Product.import!(data.map { |h| double(h) }) }

specify { expect(subject.unlimited.count).to eq data_length }
specify { expect(subject.offset(5).unlimited.count).to eq data_length }
specify { expect(subject.limit(1).unlimited.count).to eq data_length }
specify { expect(subject.unlimited.limit(1).count).to eq 1 }
end

describe '#none' do
specify { expect(subject.none).to be_a described_class }
specify { expect(subject.none).not_to eq(subject) }
Expand Down Expand Up @@ -577,7 +589,7 @@
end

describe '#search_type' do
specify { expect(subject.search_type(:count).options).to include(search_type: :count) }
specify { expect(subject.search_type(:count).criteria.search_options).to include(search_type: :count) }
end

describe '#aggregations' do
Expand Down

0 comments on commit aa6af74

Please sign in to comment.