-
Notifications
You must be signed in to change notification settings - Fork 119
/
algoliasearch-rails.rb
1037 lines (913 loc) · 38.1 KB
/
algoliasearch-rails.rb
1
2
3
4
5
6
7
8
9
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require 'algoliasearch'
require 'algoliasearch/version'
require 'algoliasearch/utilities'
if defined? Rails
begin
require 'algoliasearch/railtie'
rescue LoadError
end
end
begin
require 'active_job'
rescue LoadError
# no queue support, fine
end
require 'logger'
module AlgoliaSearch
class NotConfigured < StandardError; end
class BadConfiguration < StandardError; end
class NoBlockGiven < StandardError; end
class MixedSlavesAndReplicas < StandardError; end
autoload :Configuration, 'algoliasearch/configuration'
extend Configuration
autoload :Pagination, 'algoliasearch/pagination'
class << self
attr_reader :included_in
def included(klass)
@included_in ||= []
@included_in << klass
@included_in.uniq!
klass.class_eval do
extend ClassMethods
include InstanceMethods
end
end
end
class IndexSettings
DEFAULT_BATCH_SIZE = 1000
# AlgoliaSearch settings
OPTIONS = [
# Attributes
:searchableAttributes, :attributesForFaceting, :unretrievableAttributes, :attributesToRetrieve,
:attributesToIndex, #Legacy name of searchableAttributes
# Ranking
:ranking, :customRanking, # Replicas are handled via `add_replica`
# Faceting
:maxValuesPerFacet, :sortFacetValuesBy,
# Highlighting / Snippeting
:attributesToHighlight, :attributesToSnippet, :highlightPreTag, :highlightPostTag,
:snippetEllipsisText, :restrictHighlightAndSnippetArrays,
# Pagination
:hitsPerPage, :paginationLimitedTo,
# Typo
:minWordSizefor1Typo, :minWordSizefor2Typos, :typoTolerance, :allowTyposOnNumericTokens,
:disableTypoToleranceOnAttributes, :disableTypoToleranceOnWords, :separatorsToIndex,
# Language
:ignorePlurals, :removeStopWords, :camelCaseAttributes, :decompoundedAttributes,
:keepDiacriticsOnCharacters, :queryLanguages,
# Query Rules
:enableRules,
# Query Strategy
:queryType, :removeWordsIfNoResults, :advancedSyntax, :optionalWords,
:disablePrefixOnAttributes, :disableExactOnAttributes, :exactOnSingleWordQuery, :alternativesAsExact,
# Performance
:numericAttributesForFiltering, :allowCompressionOfIntegerArray,
:numericAttributesToIndex, # Legacy name of numericAttributesForFiltering
# Advanced
:attributeForDistinct, :distinct, :replaceSynonymsInHighlight, :minProximity, :responseFields,
:maxFacetHits,
# Rails-specific
:synonyms, :placeholders, :altCorrections,
]
OPTIONS.each do |k|
define_method k do |v|
instance_variable_set("@#{k}", v)
end
end
def initialize(options, block)
@options = options
instance_exec(&block) if block
end
def use_serializer(serializer)
@serializer = serializer
# instance_variable_set("@serializer", serializer)
end
def attribute(*names, &block)
raise ArgumentError.new('Cannot pass multiple attribute names if block given') if block_given? and names.length > 1
raise ArgumentError.new('Cannot specify additional attributes on a replica index') if @options[:slave] || @options[:replica]
@attributes ||= {}
names.flatten.each do |name|
@attributes[name.to_s] = block_given? ? Proc.new { |o| o.instance_eval(&block) } : Proc.new { |o| o.send(name) }
end
end
alias :attributes :attribute
def add_attribute(*names, &block)
raise ArgumentError.new('Cannot pass multiple attribute names if block given') if block_given? and names.length > 1
raise ArgumentError.new('Cannot specify additional attributes on a replica index') if @options[:slave] || @options[:replica]
@additional_attributes ||= {}
names.each do |name|
@additional_attributes[name.to_s] = block_given? ? Proc.new { |o| o.instance_eval(&block) } : Proc.new { |o| o.send(name) }
end
end
alias :add_attributes :add_attribute
def is_mongoid?(object)
defined?(::Mongoid::Document) && object.class.include?(::Mongoid::Document)
end
def is_sequel?(object)
defined?(::Sequel) && object.class < ::Sequel::Model
end
def is_active_record?(object)
!is_mongoid?(object) && !is_sequel?(object)
end
def get_default_attributes(object)
if is_mongoid?(object)
# work-around mongoid 2.4's unscoped method, not accepting a block
object.attributes
elsif is_sequel?(object)
object.to_hash
else
object.class.unscoped do
object.attributes
end
end
end
def get_attribute_names(object)
get_attributes(object).keys
end
def attributes_to_hash(attributes, object)
if attributes
Hash[attributes.map { |name, value| [name.to_s, value.call(object) ] }]
else
{}
end
end
def get_attributes(object)
# If a serializer is set, we ignore attributes
# everything should be done via the serializer
if not @serializer.nil?
attributes = @serializer.new(object).attributes
else
if @attributes.nil? || @attributes.length == 0
# no `attribute ...` have been configured, use the default attributes of the model
attributes = get_default_attributes(object)
else
# at least 1 `attribute ...` has been configured, therefore use ONLY the one configured
if is_active_record?(object)
object.class.unscoped do
attributes = attributes_to_hash(@attributes, object)
end
else
attributes = attributes_to_hash(@attributes, object)
end
end
end
attributes.merge!(attributes_to_hash(@additional_attributes, object)) if @additional_attributes
if @options[:sanitize]
sanitizer = begin
::HTML::FullSanitizer.new
rescue NameError
# from rails 4.2
::Rails::Html::FullSanitizer.new
end
attributes = sanitize_attributes(attributes, sanitizer)
end
if @options[:force_utf8_encoding] && Object.const_defined?(:RUBY_VERSION) && RUBY_VERSION.to_f > 1.8
attributes = encode_attributes(attributes)
end
attributes
end
def sanitize_attributes(v, sanitizer)
case v
when String
sanitizer.sanitize(v)
when Hash
v.each { |key, value| v[key] = sanitize_attributes(value, sanitizer) }
when Array
v.map { |x| sanitize_attributes(x, sanitizer) }
else
v
end
end
def encode_attributes(v)
case v
when String
v.force_encoding('utf-8')
when Hash
v.each { |key, value| v[key] = encode_attributes(value) }
when Array
v.map { |x| encode_attributes(x) }
else
v
end
end
def geoloc(lat_attr, lng_attr)
raise ArgumentError.new('Cannot specify additional attributes on a replica index') if @options[:slave] || @options[:replica]
add_attribute :_geoloc do |o|
{ :lat => o.send(lat_attr).to_f, :lng => o.send(lng_attr).to_f }
end
end
def tags(*args, &block)
raise ArgumentError.new('Cannot specify additional attributes on a replica index') if @options[:slave] || @options[:replica]
add_attribute :_tags do |o|
v = block_given? ? o.instance_eval(&block) : args
v.is_a?(Array) ? v : [v]
end
end
def get_setting(name)
instance_variable_get("@#{name}")
end
def to_settings
settings = {}
OPTIONS.each do |k|
v = get_setting(k)
settings[k] = v if !v.nil?
end
if !@options[:slave] && !@options[:replica]
settings[:slaves] = additional_indexes.select { |opts, s| opts[:slave] }.map do |opts, s|
name = opts[:index_name]
name = "#{name}_#{Rails.env.to_s}" if opts[:per_environment]
name
end
settings.delete(:slaves) if settings[:slaves].empty?
settings[:replicas] = additional_indexes.select { |opts, s| opts[:replica] }.map do |opts, s|
name = opts[:index_name]
name = "#{name}_#{Rails.env.to_s}" if opts[:per_environment]
name
end
settings.delete(:replicas) if settings[:replicas].empty?
end
settings
end
def add_index(index_name, options = {}, &block)
raise ArgumentError.new('Cannot specify additional index on a replica index') if @options[:slave] || @options[:replica]
raise ArgumentError.new('No block given') if !block_given?
raise ArgumentError.new('Options auto_index and auto_remove cannot be set on nested indexes') if options[:auto_index] || options[:auto_remove]
@additional_indexes ||= {}
raise MixedSlavesAndReplicas.new('Cannot mix slaves and replicas in the same configuration (add_slave is deprecated)') if (options[:slave] && @additional_indexes.any? { |opts, _| opts[:replica] }) || (options[:replica] && @additional_indexes.any? { |opts, _| opts[:slave] })
options[:index_name] = index_name
@additional_indexes[options] = IndexSettings.new(options, Proc.new)
end
def add_replica(index_name, options = {}, &block)
raise ArgumentError.new('Cannot specify additional replicas on a replica index') if @options[:slave] || @options[:replica]
raise ArgumentError.new('No block given') if !block_given?
add_index(index_name, options.merge({ :replica => true, :primary_settings => self }), &block)
end
def add_slave(index_name, options = {}, &block)
raise ArgumentError.new('Cannot specify additional slaves on a slave index') if @options[:slave] || @options[:replica]
raise ArgumentError.new('No block given') if !block_given?
add_index(index_name, options.merge({ :slave => true, :primary_settings => self }), &block)
end
def additional_indexes
@additional_indexes || {}
end
end
# Default queueing system
if defined?(::ActiveJob::Base)
# lazy load the ActiveJob class to ensure the
# queue is initialized before using it
# see https://github.com/algolia/algoliasearch-rails/issues/69
autoload :AlgoliaJob, 'algoliasearch/algolia_job'
end
# this class wraps an Algolia::Index object ensuring all raised exceptions
# are correctly logged or thrown depending on the `raise_on_failure` option
class SafeIndex
def initialize(name, raise_on_failure)
@index = ::Algolia::Index.new(name)
@raise_on_failure = raise_on_failure.nil? || raise_on_failure
end
::Algolia::Index.instance_methods(false).each do |m|
define_method(m) do |*args, &block|
SafeIndex.log_or_throw(m, @raise_on_failure) do
@index.send(m, *args, &block)
end
end
end
# special handling of wait_task to handle null task_id
def wait_task(task_id)
return if task_id.nil? && !@raise_on_failure # ok
SafeIndex.log_or_throw(:wait_task, @raise_on_failure) do
@index.wait_task(task_id)
end
end
# special handling of get_settings to avoid raising errors on 404
def get_settings(*args)
SafeIndex.log_or_throw(:get_settings, @raise_on_failure) do
begin
@index.get_settings(*args)
rescue Algolia::AlgoliaError => e
return {} if e.code == 404 # not fatal
raise e
end
end
end
# expose move as well
def self.move_index(old_name, new_name)
SafeIndex.log_or_throw(:move_index, true) do
::Algolia.move_index(old_name, new_name)
end
end
private
def self.log_or_throw(method, raise_on_failure, &block)
begin
yield
rescue Algolia::AlgoliaError => e
raise e if raise_on_failure
# log the error
(Rails.logger || Logger.new(STDOUT)).error("[algoliasearch-rails] #{e.message}")
# return something
case method.to_s
when 'search'
# some attributes are required
{ 'hits' => [], 'hitsPerPage' => 0, 'page' => 0, 'facets' => {}, 'error' => e }
else
# empty answer
{ 'error' => e }
end
end
end
end
# these are the class methods added when AlgoliaSearch is included
module ClassMethods
def self.extended(base)
class <<base
alias_method :without_auto_index, :algolia_without_auto_index unless method_defined? :without_auto_index
alias_method :reindex!, :algolia_reindex! unless method_defined? :reindex!
alias_method :reindex, :algolia_reindex unless method_defined? :reindex
alias_method :index_objects, :algolia_index_objects unless method_defined? :index_objects
alias_method :index!, :algolia_index! unless method_defined? :index!
alias_method :remove_from_index!, :algolia_remove_from_index! unless method_defined? :remove_from_index!
alias_method :clear_index!, :algolia_clear_index! unless method_defined? :clear_index!
alias_method :search, :algolia_search unless method_defined? :search
alias_method :raw_search, :algolia_raw_search unless method_defined? :raw_search
alias_method :search_facet, :algolia_search_facet unless method_defined? :search_facet
alias_method :search_for_facet_values, :algolia_search_for_facet_values unless method_defined? :search_for_facet_values
alias_method :index, :algolia_index unless method_defined? :index
alias_method :index_name, :algolia_index_name unless method_defined? :index_name
alias_method :must_reindex?, :algolia_must_reindex? unless method_defined? :must_reindex?
end
base.cattr_accessor :algoliasearch_options, :algoliasearch_settings
end
def algoliasearch(options = {}, &block)
self.algoliasearch_settings = IndexSettings.new(options, block_given? ? Proc.new : nil)
self.algoliasearch_options = { :type => algolia_full_const_get(model_name.to_s), :per_page => algoliasearch_settings.get_setting(:hitsPerPage) || 10, :page => 1 }.merge(options)
attr_accessor :highlight_result, :snippet_result
if options[:synchronous] == true
if defined?(::Sequel) && self < Sequel::Model
class_eval do
copy_after_validation = instance_method(:after_validation)
define_method(:after_validation) do |*args|
super(*args)
copy_after_validation.bind(self).call
algolia_mark_synchronous
end
end
else
after_validation :algolia_mark_synchronous if respond_to?(:after_validation)
end
end
if options[:enqueue]
raise ArgumentError.new("Cannot use a enqueue if the `synchronous` option if set") if options[:synchronous]
proc = if options[:enqueue] == true
Proc.new do |record, remove|
AlgoliaJob.perform_later(record, remove ? 'algolia_remove_from_index!' : 'algolia_index!')
end
elsif options[:enqueue].respond_to?(:call)
options[:enqueue]
elsif options[:enqueue].is_a?(Symbol)
Proc.new { |record, remove| self.send(options[:enqueue], record, remove) }
else
raise ArgumentError.new("Invalid `enqueue` option: #{options[:enqueue]}")
end
algoliasearch_options[:enqueue] = Proc.new do |record, remove|
proc.call(record, remove) unless algolia_without_auto_index_scope
end
end
unless options[:auto_index] == false
if defined?(::Sequel) && self < Sequel::Model
class_eval do
copy_after_validation = instance_method(:after_validation)
copy_before_save = instance_method(:before_save)
define_method(:after_validation) do |*args|
super(*args)
copy_after_validation.bind(self).call
algolia_mark_must_reindex
end
define_method(:before_save) do |*args|
copy_before_save.bind(self).call
algolia_mark_for_auto_indexing
super(*args)
end
sequel_version = Gem::Version.new(Sequel.version)
if sequel_version >= Gem::Version.new('4.0.0') && sequel_version < Gem::Version.new('5.0.0')
copy_after_commit = instance_method(:after_commit)
define_method(:after_commit) do |*args|
super(*args)
copy_after_commit.bind(self).call
algolia_perform_index_tasks
end
else
copy_after_save = instance_method(:after_save)
define_method(:after_save) do |*args|
super(*args)
copy_after_save.bind(self).call
self.db.after_commit do
algolia_perform_index_tasks
end
end
end
end
else
after_validation :algolia_mark_must_reindex if respond_to?(:after_validation)
before_save :algolia_mark_for_auto_indexing if respond_to?(:before_save)
if respond_to?(:after_commit)
after_commit :algolia_perform_index_tasks
elsif respond_to?(:after_save)
after_save :algolia_perform_index_tasks
end
end
end
unless options[:auto_remove] == false
if defined?(::Sequel) && self < Sequel::Model
class_eval do
copy_after_destroy = instance_method(:after_destroy)
define_method(:after_destroy) do |*args|
copy_after_destroy.bind(self).call
algolia_enqueue_remove_from_index!(algolia_synchronous?)
super(*args)
end
end
else
after_destroy { |searchable| searchable.algolia_enqueue_remove_from_index!(algolia_synchronous?) } if respond_to?(:after_destroy)
end
end
end
def algolia_without_auto_index(&block)
self.algolia_without_auto_index_scope = true
begin
yield
ensure
self.algolia_without_auto_index_scope = false
end
end
def algolia_without_auto_index_scope=(value)
Thread.current["algolia_without_auto_index_scope_for_#{self.model_name}"] = value
end
def algolia_without_auto_index_scope
Thread.current["algolia_without_auto_index_scope_for_#{self.model_name}"]
end
def algolia_reindex!(batch_size = AlgoliaSearch::IndexSettings::DEFAULT_BATCH_SIZE, synchronous = false)
return if algolia_without_auto_index_scope
algolia_configurations.each do |options, settings|
next if algolia_indexing_disabled?(options)
index = algolia_ensure_init(options, settings)
next if options[:slave] || options[:replica]
last_task = nil
algolia_find_in_batches(batch_size) do |group|
if algolia_conditional_index?(options)
# delete non-indexable objects
ids = group.select { |o| !algolia_indexable?(o, options) }.map { |o| algolia_object_id_of(o, options) }
index.delete_objects(ids.select { |id| !id.blank? })
# select only indexable objects
group = group.select { |o| algolia_indexable?(o, options) }
end
objects = group.map do |o|
attributes = settings.get_attributes(o)
unless attributes.class == Hash
attributes = attributes.to_hash
end
attributes.merge 'objectID' => algolia_object_id_of(o, options)
end
last_task = index.save_objects(objects)
end
index.wait_task(last_task["taskID"]) if last_task and (synchronous || options[:synchronous])
end
nil
end
# reindex whole database using a extra temporary index + move operation
def algolia_reindex(batch_size = AlgoliaSearch::IndexSettings::DEFAULT_BATCH_SIZE, synchronous = false)
return if algolia_without_auto_index_scope
algolia_configurations.each do |options, settings|
next if algolia_indexing_disabled?(options)
next if options[:slave] || options[:replica]
# fetch the master settings
master_index = algolia_ensure_init(options, settings)
master_settings = master_index.get_settings rescue {} # if master doesn't exist yet
master_settings.merge!(JSON.parse(settings.to_settings.to_json)) # convert symbols to strings
# remove the replicas of the temporary index
master_settings.delete :slaves
master_settings.delete 'slaves'
master_settings.delete :replicas
master_settings.delete 'replicas'
# init temporary index
index_name = algolia_index_name(options)
tmp_options = options.merge({ :index_name => "#{index_name}.tmp" })
tmp_options.delete(:per_environment) # already included in the temporary index_name
tmp_settings = settings.dup
tmp_index = algolia_ensure_init(tmp_options, tmp_settings, master_settings)
algolia_find_in_batches(batch_size) do |group|
if algolia_conditional_index?(tmp_options)
# select only indexable objects
group = group.select { |o| algolia_indexable?(o, tmp_options) }
end
objects = group.map { |o| tmp_settings.get_attributes(o).merge 'objectID' => algolia_object_id_of(o, tmp_options) }
tmp_index.save_objects(objects)
end
move_task = SafeIndex.move_index(tmp_index.name, index_name)
master_index.wait_task(move_task["taskID"]) if synchronous || options[:synchronous]
end
nil
end
def algolia_index_objects(objects, synchronous = false)
algolia_configurations.each do |options, settings|
next if algolia_indexing_disabled?(options)
index = algolia_ensure_init(options, settings)
next if options[:slave] || options[:replica]
task = index.save_objects(objects.map { |o| settings.get_attributes(o).merge 'objectID' => algolia_object_id_of(o, options) })
index.wait_task(task["taskID"]) if synchronous || options[:synchronous]
end
end
def algolia_index!(object, synchronous = false)
return if algolia_without_auto_index_scope
algolia_configurations.each do |options, settings|
next if algolia_indexing_disabled?(options)
object_id = algolia_object_id_of(object, options)
index = algolia_ensure_init(options, settings)
next if options[:slave] || options[:replica]
if algolia_indexable?(object, options)
raise ArgumentError.new("Cannot index a record with a blank objectID") if object_id.blank?
if synchronous || options[:synchronous]
index.add_object!(settings.get_attributes(object), object_id)
else
index.add_object(settings.get_attributes(object), object_id)
end
elsif algolia_conditional_index?(options) && !object_id.blank?
# remove non-indexable objects
if synchronous || options[:synchronous]
index.delete_object!(object_id)
else
index.delete_object(object_id)
end
end
end
nil
end
def algolia_remove_from_index!(object, synchronous = false)
return if algolia_without_auto_index_scope
object_id = algolia_object_id_of(object)
raise ArgumentError.new("Cannot index a record with a blank objectID") if object_id.blank?
algolia_configurations.each do |options, settings|
next if algolia_indexing_disabled?(options)
index = algolia_ensure_init(options, settings)
next if options[:slave] || options[:replica]
if synchronous || options[:synchronous]
index.delete_object!(object_id)
else
index.delete_object(object_id)
end
end
nil
end
def algolia_clear_index!(synchronous = false)
algolia_configurations.each do |options, settings|
next if algolia_indexing_disabled?(options)
index = algolia_ensure_init(options, settings)
next if options[:slave] || options[:replica]
synchronous || options[:synchronous] ? index.clear! : index.clear
@algolia_indexes[settings] = nil
end
nil
end
def algolia_raw_search(q, params = {})
index_name = params.delete(:index) ||
params.delete('index') ||
params.delete(:slave) ||
params.delete('slave') ||
params.delete(:replica) ||
params.delete('replica')
index = algolia_index(index_name)
index.search(q, Hash[params.map { |k,v| [k.to_s, v.to_s] }])
end
module AdditionalMethods
def self.extended(base)
class <<base
alias_method :raw_answer, :algolia_raw_answer unless method_defined? :raw_answer
alias_method :facets, :algolia_facets unless method_defined? :facets
end
end
def algolia_raw_answer
@algolia_json
end
def algolia_facets
@algolia_json['facets']
end
private
def algolia_init_raw_answer(json)
@algolia_json = json
end
end
def algolia_search(q, params = {})
if AlgoliaSearch.configuration[:pagination_backend]
# kaminari and will_paginate start pagination at 1, Algolia starts at 0
params[:page] = (params.delete('page') || params.delete(:page)).to_i
params[:page] -= 1 if params[:page].to_i > 0
end
json = algolia_raw_search(q, params)
hit_ids = json['hits'].map { |hit| hit['objectID'] }
if defined?(::Mongoid::Document) && self.include?(::Mongoid::Document)
condition_key = algolia_object_id_method.in
else
condition_key = algolia_object_id_method
end
results_by_id = algoliasearch_options[:type].where(condition_key => hit_ids).index_by do |hit|
algolia_object_id_of(hit)
end
results = json['hits'].map do |hit|
o = results_by_id[hit['objectID'].to_s]
if o
o.highlight_result = hit['_highlightResult']
o.snippet_result = hit['_snippetResult']
o
end
end.compact
# Algolia has a default limit of 1000 retrievable hits
total_hits = json['nbHits'].to_i < json['nbPages'].to_i * json['hitsPerPage'].to_i ?
json['nbHits'].to_i: json['nbPages'].to_i * json['hitsPerPage'].to_i
res = AlgoliaSearch::Pagination.create(results, total_hits, algoliasearch_options.merge({ :page => json['page'].to_i + 1, :per_page => json['hitsPerPage'] }))
res.extend(AdditionalMethods)
res.send(:algolia_init_raw_answer, json)
res
end
def algolia_search_for_facet_values(facet, text, params = {})
index_name = params.delete(:index) ||
params.delete('index') ||
params.delete(:slave) ||
params.delete('slave') ||
params.delete(:replica) ||
params.delete('replicas')
index = algolia_index(index_name)
query = Hash[params.map { |k, v| [k.to_s, v.to_s] }]
index.search_facet(facet, text, query)['facetHits']
end
# deprecated (renaming)
alias :algolia_search_facet :algolia_search_for_facet_values
def algolia_index(name = nil)
if name
algolia_configurations.each do |o, s|
return algolia_ensure_init(o, s) if o[:index_name].to_s == name.to_s
end
raise ArgumentError.new("Invalid index/replica name: #{name}")
end
algolia_ensure_init
end
def algolia_index_name(options = nil)
options ||= algoliasearch_options
name = options[:index_name] || model_name.to_s.gsub('::', '_')
name = "#{name}_#{Rails.env.to_s}" if options[:per_environment]
name
end
def algolia_must_reindex?(object)
# use +algolia_dirty?+ method if implemented
return object.send(:algolia_dirty?) if (object.respond_to?(:algolia_dirty?))
# Loop over each index to see if a attribute used in records has changed
algolia_configurations.each do |options, settings|
next if options[:slave] || options[:replica]
return true if algolia_object_id_changed?(object, options)
settings.get_attribute_names(object).each do |k|
return true if algolia_attribute_changed?(object, k)
# return true if !object.respond_to?(changed_method) || object.send(changed_method)
end
[options[:if], options[:unless]].each do |condition|
case condition
when nil
when String, Symbol
return true if algolia_attribute_changed?(object, condition)
else
# if the :if, :unless condition is a anything else,
# we have no idea whether we should reindex or not
# let's always reindex then
return true
end
end
end
# By default, we don't reindex
return false
end
protected
def algolia_ensure_init(options = nil, settings = nil, index_settings = nil)
raise ArgumentError.new('No `algoliasearch` block found in your model.') if algoliasearch_settings.nil?
@algolia_indexes ||= {}
options ||= algoliasearch_options
settings ||= algoliasearch_settings
return @algolia_indexes[settings] if @algolia_indexes[settings]
@algolia_indexes[settings] = SafeIndex.new(algolia_index_name(options), algoliasearch_options[:raise_on_failure])
current_settings = @algolia_indexes[settings].get_settings(:getVersion => 1) rescue nil # if the index doesn't exist
index_settings ||= settings.to_settings
index_settings = options[:primary_settings].to_settings.merge(index_settings) if options[:inherit]
if !algolia_indexing_disabled?(options) && algoliasearch_settings_changed?(current_settings, index_settings)
used_slaves = !current_settings.nil? && !current_settings['slaves'].nil?
replicas = index_settings.delete(:replicas) ||
index_settings.delete('replicas') ||
index_settings.delete(:slaves) ||
index_settings.delete('slaves')
index_settings[used_slaves ? :slaves : :replicas] = replicas unless replicas.nil? || options[:inherit]
@algolia_indexes[settings].set_settings(index_settings)
end
@algolia_indexes[settings]
end
private
def algolia_configurations
raise ArgumentError.new('No `algoliasearch` block found in your model.') if algoliasearch_settings.nil?
if @configurations.nil?
@configurations = {}
@configurations[algoliasearch_options] = algoliasearch_settings
algoliasearch_settings.additional_indexes.each do |k,v|
@configurations[k] = v
if v.additional_indexes.any?
v.additional_indexes.each do |options, index|
@configurations[options] = index
end
end
end
end
@configurations
end
def algolia_object_id_method(options = nil)
options ||= algoliasearch_options
options[:id] || options[:object_id] || :id
end
def algolia_object_id_of(o, options = nil)
o.send(algolia_object_id_method(options)).to_s
end
def algolia_object_id_changed?(o, options = nil)
changed = algolia_attribute_changed?(o, algolia_object_id_method(options))
changed.nil? ? false : changed
end
def algoliasearch_settings_changed?(prev, current)
return true if prev.nil?
current.each do |k, v|
prev_v = prev[k.to_s]
if v.is_a?(Array) and prev_v.is_a?(Array)
# compare array of strings, avoiding symbols VS strings comparison
return true if v.map { |x| x.to_s } != prev_v.map { |x| x.to_s }
else
return true if prev_v != v
end
end
false
end
def algolia_full_const_get(name)
list = name.split('::')
list.shift if list.first.blank?
obj = Object.const_defined?(:RUBY_VERSION) && RUBY_VERSION.to_f < 1.9 ? Object : self
list.each do |x|
# This is required because const_get tries to look for constants in the
# ancestor chain, but we only want constants that are HERE
obj = obj.const_defined?(x) ? obj.const_get(x) : obj.const_missing(x)
end
obj
end
def algolia_conditional_index?(options = nil)
options ||= algoliasearch_options
options[:if].present? || options[:unless].present?
end
def algolia_indexable?(object, options = nil)
options ||= algoliasearch_options
if_passes = options[:if].blank? || algolia_constraint_passes?(object, options[:if])
unless_passes = options[:unless].blank? || !algolia_constraint_passes?(object, options[:unless])
if_passes && unless_passes
end
def algolia_constraint_passes?(object, constraint)
case constraint
when Symbol
object.send(constraint)
when String
object.send(constraint.to_sym)
when Enumerable
# All constraints must pass
constraint.all? { |inner_constraint| algolia_constraint_passes?(object, inner_constraint) }
else
if constraint.respond_to?(:call) # Proc
constraint.call(object)
else
raise ArgumentError, "Unknown constraint type: #{constraint} (#{constraint.class})"
end
end
end
def algolia_indexing_disabled?(options = nil)
options ||= algoliasearch_options
constraint = options[:disable_indexing] || options['disable_indexing']
case constraint
when nil
return false
when true, false
return constraint
when String, Symbol
return send(constraint)
else
return constraint.call if constraint.respond_to?(:call) # Proc
end
raise ArgumentError, "Unknown constraint type: #{constraint} (#{constraint.class})"
end
def algolia_find_in_batches(batch_size, &block)
if (defined?(::ActiveRecord) && ancestors.include?(::ActiveRecord::Base)) || respond_to?(:find_in_batches)
find_in_batches(:batch_size => batch_size, &block)
elsif defined?(::Sequel) && self < Sequel::Model
dataset.extension(:pagination).each_page(batch_size, &block)
else
# don't worry, mongoid has its own underlying cursor/streaming mechanism
items = []
all.each do |item|
items << item
if items.length % batch_size == 0
yield items
items = []
end
end
yield items unless items.empty?
end
end
def algolia_attribute_changed?(object, attr_name)
# if one of two method is implemented, we return its result
# true/false means whether it has changed or not
# +#{attr_name}_changed?+ always defined for automatic attributes but deprecated after Rails 5.2
# +will_save_change_to_#{attr_name}?+ should be use instead for Rails 5.2+, also defined for automatic attributes.
# If none of the method are defined, it's a dynamic attribute
method_name = "#{attr_name}_changed?"
if object.respond_to?(method_name)
# If +#{attr_name}_changed?+ respond we want to see if the method is user defined or if it's automatically
# defined by Rails.
# If it's user-defined, we call it.
# If it's automatic we check ActiveRecord version to see if this method is deprecated
# and try to call +will_save_change_to_#{attr_name}?+ instead.
# See: https://github.com/algolia/algoliasearch-rails/pull/338
# This feature is not compatible with Ruby 1.8
# In this case, we always call #{attr_name}_changed?
if Object.const_defined?(:RUBY_VERSION) && RUBY_VERSION.to_f < 1.9
return object.send(method_name)
end
unless automatic_changed_method?(object, method_name) && automatic_changed_method_deprecated?
return object.send(method_name)
end
end
if object.respond_to?("will_save_change_to_#{attr_name}?")
return object.send("will_save_change_to_#{attr_name}?")
end
# Nil means we don't know if the attribute has changed
nil
end
def automatic_changed_method?(object, method_name)
raise ArgumentError.new("Method #{method_name} doesn't exist on #{object.class.name}") unless object.respond_to?(method_name)
file = object.method(method_name).source_location[0]
file.end_with?("active_model/attribute_methods.rb")
end
def automatic_changed_method_deprecated?
(defined?(::ActiveRecord) && ActiveRecord::VERSION::MAJOR >= 5 && ActiveRecord::VERSION::MINOR >= 1) ||
(defined?(::ActiveRecord) && ActiveRecord::VERSION::MAJOR > 5)
end
end
# these are the instance methods included
module InstanceMethods
def self.included(base)
base.instance_eval do
alias_method :index!, :algolia_index! unless method_defined? :index!
alias_method :remove_from_index!, :algolia_remove_from_index! unless method_defined? :remove_from_index!
end
end
def algolia_index!(synchronous = false)
self.class.algolia_index!(self, synchronous || algolia_synchronous?)
end
def algolia_remove_from_index!(synchronous = false)
self.class.algolia_remove_from_index!(self, synchronous || algolia_synchronous?)
end
def algolia_enqueue_remove_from_index!(synchronous)
if algoliasearch_options[:enqueue]
algoliasearch_options[:enqueue].call(self, true) unless self.class.send(:algolia_indexing_disabled?, algoliasearch_options)
else
algolia_remove_from_index!(synchronous || algolia_synchronous?)
end
end
def algolia_enqueue_index!(synchronous)
if algoliasearch_options[:enqueue]
algoliasearch_options[:enqueue].call(self, false) unless self.class.send(:algolia_indexing_disabled?, algoliasearch_options)
else