Module: HasHelpers::Index
- Defined in:
- app/lib/has_helpers/index.rb,
lib/has_helpers/index/config.rb
Defined Under Namespace
Modules: ActiveRecord, ClassMethods Classes: Config, Error
Constant Summary collapse
- MAX_NGRAM_SIZE =
TODO Try to reduce this
20- MIN_NGRAM_SIZE =
1- MAX_NGRAM_DIFF =
MAX_NGRAM_SIZE - MIN_NGRAM_SIZE
Class Method Summary collapse
-
.active_indices ⇒ Object
Returns all indices that are in use.
- .after_index(key) ⇒ Object
- .before_index(klass) ⇒ Object
- .config ⇒ Object
-
.default_attributes(&block) ⇒ Object
Sets default attributes to add to every index records.
- .filter_hidden_constants(results, organization_id) ⇒ Object
- .included(mod) ⇒ Object
-
.indices ⇒ Object
HasHelpers::Index.indices.
- .is_keyword_query?(query_string, indexes = indices) ⇒ Boolean
- .parse_keyword_query(query_string, indexes = indices) ⇒ Object
-
.scheduled_reindexing ⇒ Object
indexes only one big resource at a time.
- .search(query, indexes:, where: {}, routing: nil, show_inactive: nil, **options) ⇒ Object
Class Method Details
.active_indices ⇒ Object
Returns all indices that are in use. It does this by checking if they have an alias.
204 205 206 |
# File 'app/lib/has_helpers/index.rb', line 204 def self.active_indices Searchkick.client.indices.get_alias.map { |_name, | ["aliases"].keys }.flatten.compact end |
.after_index(key) ⇒ Object
212 213 214 |
# File 'app/lib/has_helpers/index.rb', line 212 def self.after_index(key) config.after_index.call(key) end |
.before_index(klass) ⇒ Object
216 217 218 |
# File 'app/lib/has_helpers/index.rb', line 216 def self.before_index(klass) config.before_index.call(klass) end |
.config ⇒ Object
208 209 210 |
# File 'app/lib/has_helpers/index.rb', line 208 def self.config ::HasHelpers.config.index end |
.default_attributes(&block) ⇒ Object
Sets default attributes to add to every index records. ==Example HasHelpers::Index.default_attributes do { is_inactive: false, indexed_at: Time.now } end
232 233 234 235 236 237 238 |
# File 'app/lib/has_helpers/index.rb', line 232 def self.default_attributes(&block) if block.present? @default_attributes = block.call else @default_attributes || { is_inactive: nil } end end |
.filter_hidden_constants(results, organization_id) ⇒ Object
220 221 222 |
# File 'app/lib/has_helpers/index.rb', line 220 def self.filter_hidden_constants(results, organization_id) config.filter_hidden_constants.call(results, organization_id) end |
.included(mod) ⇒ Object
182 183 184 185 186 187 |
# File 'app/lib/has_helpers/index.rb', line 182 def self.included(mod) @indices ||= Set.new @indices.add(mod) mod.extend(ClassMethods) mod.send(:configure) end |
.indices ⇒ Object
HasHelpers::Index.indices
198 199 200 |
# File 'app/lib/has_helpers/index.rb', line 198 def self.indices @indices end |
.is_keyword_query?(query_string, indexes = indices) ⇒ Boolean
253 254 255 256 257 258 259 |
# File 'app/lib/has_helpers/index.rb', line 253 def self.is_keyword_query?(query_string, indexes = indices) query_string.include?(":") && indexes.each do |idx| return true if /#{idx.keyword_search_field_regex}/.match?(query_string) end false end |
.parse_keyword_query(query_string, indexes = indices) ⇒ Object
240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'app/lib/has_helpers/index.rb', line 240 def self.parse_keyword_query(query_string, indexes = indices) queries = {} columns_to_search = indexes.map(&:columns_to_search).flatten.uniq separated_query_string = query_string.downcase.split(/(?:\s+(?=\S+:))/).reject(&:empty?).map(&:rstrip) separated_query_string.each do |single_query| if /^(?<query_field>\w+):\s*(?<query_term>.+)\s*/ =~ single_query field_name = query_term.length > MAX_NGRAM_SIZE ? "keyword" : "wildcard_index" queries["#{query_field}.#{field_name}"] = query_term if columns_to_search.include?(query_field.to_sym) end end queries end |
.scheduled_reindexing ⇒ Object
indexes only one big resource at a time
190 191 192 193 194 195 |
# File 'app/lib/has_helpers/index.rb', line 190 def self.scheduled_reindexing all_index_names = ::HasHelpers::Index.indices.to_a.map(&:name) all_index_names.each do |index_name| ElasticsearchReindexScheduler.perform_async(index_name) end end |
.search(query, indexes:, where: {}, routing: nil, show_inactive: nil, **options) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'app/lib/has_helpers/index.rb', line 10 def self.search(query, indexes:, where: {}, routing: nil, show_inactive: nil, **) unless where[:organization_id].present? || !routing.nil? raise HasHelpers::Index::Error, <<-MSG.squish `organization_id` is required inside the `where` option unless `routing` is specified. Add `routing: false` to search across all organizations. MSG end organization_id = where[:organization_id] indexes = Array(indexes) weighted_attrs = indexes.map(&:weighted_attr).uniq weighted_attr = weighted_attrs.length == 1 ? weighted_attrs.first : "updated_at" where = where.except(:organization_id) where[:_and] ||= [] where[:_and] << { _or: [ { organization_id: organization_id }, { organization_id: nil } ] } unless show_inactive || indexes.any?(&:show_end_dated_records?) [ { is_inactive: nil }, { _or: [ { start_date: nil }, { start_date: { lte: ::Date.today } } ] }, { _or: [ { end_date: nil }, { end_date: { gte: ::Date.today } } ] } ].each { |default_filter| where[:_and] << default_filter } end routing_key = if routing == false nil else default_routing_key = if indexes.any?(&:is_global?) [0, organization_id] else organization_id end routing || default_routing_key end organization = ::HasHelpers::Organization.find_by(id: organization_id) use_keyword_query = organization && ::HasHelpers::Feature.active?(:keyword_search, organization) && is_keyword_query?(query, indexes) index_name = use_keyword_query ? indexes.map(&:keyword_search_index_name) : indexes.map(&:index_name) Searchkick.search( query, index_name: index_name.size > 1 ? index_name.select { |index| active_indices.include? index } : index_name, # only use available indexes for gloabl search fields: use_keyword_query ? nil : [:all_fields], load: false, misspellings: false, routing: routing_key, where: where, ** ) do |body| q = body[:query] q[:bool][:must][:bool][:should][0][:dis_max][:queries].tap do |queries| if use_keyword_query queries.clear keyword_query_array = [] keyword_queries = parse_keyword_query(query, indexes) keyword_queries.each do |field_name, search_text| keyword_query_array << { match: { field_name => { query: search_text, boost: 20, operator: "and", analyzer: "classic_search" } } } end queries << { bool: { must: keyword_query_array } } else # Weight name matches higher if organization && ::HasHelpers::Feature.active?(:classic_name_search, organization) queries << { match: { "name.analyzed" => { query: query, boost: 25, operator: "and", analyzer: "classic_search" } } } else queries << { match: { "name.analyzed" => { query: query, boost: 25, operator: "and", analyzer: "searchkick_search" } } } end # Tokens matched using the classic search should be weighted between our searchkick queries (10) # and the name query (25). Classic search is useful for searching emails or acronyms. queries << { match: { "all_fields.analyzed" => { query: query, boost: 20, operator: "and", analyzer: "classic_search" } } } # Also search keywords with the classic search. # The analyzed field uses tokens filtered by nGrams which can make # it so that results aren't found if the query is tokenized into # tokens greater than the max ngram length if query.length > MAX_NGRAM_SIZE queries << { match: { "all_fields.keyword" => { query: query, boost: 20, operator: "and", analyzer: "classic_search" } } } end end # Use exact search if query contains double quotes if query.count('"') > 1 q[:bool][:must] = [q[:bool][:must]] q[:bool][:must] << { match: { "all_fields.keyword" => { query: query, operator: "and", analyzer: "keyword_search" } } } end end # Weight recently viewed/updated records higher body[:query] = { function_score: { score_mode: "sum", boost_mode: "multiply", functions: [{ weight: 1 }, { weight: 5, gauss: { "#{weighted_attr}": { scale: "365d", decay: 0.5 } } }], query: q } } end end |