Class: SuperSearch::Scroll
- Inherits:
-
Object
- Object
- SuperSearch::Scroll
- Defined in:
- lib/super_search/scroll.rb
Instance Attribute Summary collapse
-
#expire_time ⇒ Object
Returns the value of attribute expire_time.
-
#query ⇒ Object
Returns the value of attribute query.
-
#size ⇒ Object
Returns the value of attribute size.
Instance Method Summary collapse
-
#initialize(model, term: '*', options: {}) ⇒ Scroll
constructor
ScrollSearch.new(Member, ‘*’, where: {}, order: {}, scroll: ‘5m’, size: 10000).
-
#query_inject_hack ⇒ Object
(hack) 改裝 Searchkick::Query.
-
#search(&block) ⇒ Object
開始遍歷搜尋.
Constructor Details
#initialize(model, term: '*', options: {}) ⇒ Scroll
ScrollSearch.new(Member, ‘*’, where: {}, order: {}, scroll: ‘5m’, size: 10000)
7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/super_search/scroll.rb', line 7 def initialize(model, term: '*', options: {}) self.expire_time = .delete(:scroll) || '5m' # 單次筆數不得超過 10000 self.size = .delete(:size).to_i # TODO: 這裏多搜尋一次,解決效能問題 searchkick = model.search(term, { execute: false }.merge()) self.query = Searchkick::Query.new(searchkick.klass, searchkick.term, searchkick.) query_inject_hack query.setup_scroll(expire_time) query.setup_size(size) end |
Instance Attribute Details
#expire_time ⇒ Object
Returns the value of attribute expire_time.
4 5 6 |
# File 'lib/super_search/scroll.rb', line 4 def expire_time @expire_time end |
#query ⇒ Object
Returns the value of attribute query.
4 5 6 |
# File 'lib/super_search/scroll.rb', line 4 def query @query end |
#size ⇒ Object
Returns the value of attribute size.
4 5 6 |
# File 'lib/super_search/scroll.rb', line 4 def size @size end |
Instance Method Details
#query_inject_hack ⇒ Object
(hack) 改裝 Searchkick::Query
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/super_search/scroll.rb', line 22 def query_inject_hack query.instance_eval do # 設定遍歷參數 def setup_scroll(scroll) @scroll_id = nil @scroll = scroll end # 設定遍歷 筆數/次 def setup_size(size) @size = size end # searchkick/query line: 58 def params = { scroll: @scroll } .merge!(body: { size: @size }) if @size <= 10000 && @size > 0 super.deep_merge() end # searchkick/query line: 78 def execute @execute = nil super end # searchkick/query line: 104 def handle_response(response) # 從 response 設定 scroll ID @scroll_id = response['_scroll_id'] super end # searchkick/query line: 205 def execute_search # 第一次用原本的 search 來取 scroll id, 第二次以後 scroll id 跑 scroll api if @scroll_id.present? Searchkick.client.scroll(body: { scroll_id: @scroll_id }, scroll: @scroll) else Searchkick.client.search(params) end end end end |
#search(&block) ⇒ Object
開始遍歷搜尋
68 69 70 71 72 |
# File 'lib/super_search/scroll.rb', line 68 def search(&block) while r = query.execute and not r.response['hits']['hits'].empty? do block.call(r.results, r.total_count) if block end end |