Sha256: d629c528b7d04f51d48f2524bf2c9cf2d811eb687efe670c7f0ef98fcd3ad4c0
Contents?: true
Size: 1.07 KB
Versions: 3
Compression:
Stored size: 1.07 KB
Contents
module Workarea decorate Metrics::SearchByWeek, with: 'search_autocomplete' do decorated do index( { query_string: 1, total_results: 1, searches: 1 }, { partial_filter_expression: { total_results: { '$gt' => 0 } } } ) scope :by_query_string, ->(string) { where(query_string: /^#{string}/) } end class_methods do def autocomplete(query_string, max: 3) pipeline = [ { '$match' => { total_results: { '$gt' => 0 }, query_string: { '$regex' => /^#{query_string}/ } } }, { '$project' => { 'query_id' => 1, 'searches' => 1, 'query_string' => 1 } }, { '$group' => { '_id' => '$query_id', 'searches' => { '$sum' => '$searches' }, 'query_string' => { '$first' => '$query_string' } } }, { '$sort' => { 'searches' => -1 } }, { '$limit' => max } ] collection.aggregate(pipeline).map { |r| r['query_string'] } end end end end
Version data entries
3 entries across 3 versions & 1 rubygems