algoliasearch-rails 1.17.0 → 1.17.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/ChangeLog +4 -0
- data/README.md +218 -240
- data/VERSION +1 -1
- data/lib/algoliasearch-rails.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0bb0ed3c6e61faa93d87f73b3486759e93f6d77b
|
4
|
+
data.tar.gz: 80bacfec7eb024e61a72196dde014d0cfdecb45d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 803713e7a76f93c14309150aafef6ad66703d1e200ce887234914ea1301841e4225e0ecd001658efe8a3061d9c0857aa208310bfd04ce73267cabf2a35019c88
|
7
|
+
data.tar.gz: ff9f70b0815494a7847a36a02d1987d18bab009522dc3c86250134412d106f6cee44d7c416a20fed40594fd71b674e009c7001b1d3b7871a82943d045ead4230
|
data/ChangeLog
CHANGED
data/README.md
CHANGED
@@ -9,6 +9,9 @@ You might be interested in the sample Ruby on Rails application providing a `aut
|
|
9
9
|
[](https://travis-ci.org/algolia/algoliasearch-rails) [](http://badge.fury.io/rb/algoliasearch-rails) [](https://codeclimate.com/github/algolia/algoliasearch-rails)   
|
10
10
|
|
11
11
|
|
12
|
+
**Note:** An easier-to-read version of this documentation is available on
|
13
|
+
[Algolia's website](https://www.algolia.com/doc/api-client/rails/).
|
14
|
+
|
12
15
|
# Table of Contents
|
13
16
|
|
14
17
|
|
@@ -16,56 +19,51 @@ You might be interested in the sample Ruby on Rails application providing a `aut
|
|
16
19
|
|
17
20
|
1. [Install](#install)
|
18
21
|
1. [Configuration](#configuration)
|
22
|
+
1. [Timeouts](#timeouts)
|
23
|
+
1. [Notes](#notes)
|
19
24
|
|
20
|
-
**
|
25
|
+
**Usage**
|
21
26
|
|
22
|
-
1. [Schema](#schema)
|
27
|
+
1. [Index Schema](#index-schema)
|
23
28
|
1. [Relevancy](#relevancy)
|
24
29
|
1. [Indexing](#indexing)
|
25
30
|
1. [Frontend Search (realtime experience)](#frontend-search-realtime-experience)
|
26
31
|
1. [Backend Search](#backend-search)
|
27
32
|
1. [Backend Pagination](#backend-pagination)
|
28
|
-
1. [
|
33
|
+
1. [Tags](#tags)
|
34
|
+
1. [Faceting](#faceting)
|
35
|
+
1. [Faceted search](#faceted-search)
|
36
|
+
1. [Group by](#group-by)
|
37
|
+
1. [Geo-Search](#geo-search)
|
29
38
|
|
30
39
|
**Options**
|
31
40
|
|
32
|
-
1. [Auto-indexing &
|
33
|
-
1. [Exceptions](#exceptions)
|
41
|
+
1. [Auto-indexing & asynchronism](#auto-indexing--asynchronism)
|
34
42
|
1. [Custom index name](#custom-index-name)
|
35
|
-
1. [Per-environment
|
43
|
+
1. [Per-environment indices](#per-environment-indices)
|
36
44
|
1. [Custom attribute definition](#custom-attribute-definition)
|
37
45
|
1. [Nested objects/relations](#nested-objectsrelations)
|
38
46
|
1. [Custom `objectID`](#custom-objectid)
|
39
47
|
1. [Restrict indexing to a subset of your data](#restrict-indexing-to-a-subset-of-your-data)
|
40
48
|
1. [Sanitizer](#sanitizer)
|
41
49
|
1. [UTF-8 Encoding](#utf-8-encoding)
|
50
|
+
1. [Exceptions](#exceptions)
|
42
51
|
1. [Configuration example](#configuration-example)
|
43
52
|
|
44
|
-
**
|
53
|
+
**Indices**
|
45
54
|
|
46
55
|
1. [Manual indexing](#manual-indexing)
|
47
56
|
1. [Manual removal](#manual-removal)
|
48
57
|
1. [Reindexing](#reindexing)
|
49
58
|
1. [Clearing an index](#clearing-an-index)
|
50
59
|
1. [Using the underlying index](#using-the-underlying-index)
|
51
|
-
|
52
|
-
**Indexes**
|
53
|
-
|
54
60
|
1. [Primary/replica](#primaryreplica)
|
55
61
|
1. [Share a single index](#share-a-single-index)
|
56
|
-
1. [Target multiple
|
62
|
+
1. [Target multiple indices](#target-multiple-indices)
|
57
63
|
|
58
|
-
**
|
64
|
+
**Testing**
|
59
65
|
|
60
|
-
1. [
|
61
|
-
1. [Search](#search)
|
62
|
-
1. [Faceting](#faceting)
|
63
|
-
1. [Facet search](#facet-search)
|
64
|
-
1. [Group by](#group-by)
|
65
|
-
1. [Geo-Search](#geo-search)
|
66
|
-
1. [Caveats](#caveats)
|
67
|
-
1. [Timeouts](#timeouts)
|
68
|
-
1. [Note on testing](#note-on-testing)
|
66
|
+
1. [Notes](#notes)
|
69
67
|
|
70
68
|
|
71
69
|
# Guides & Tutorials
|
@@ -118,12 +116,40 @@ AlgoliaSearch.configuration = { application_id: 'YourApplicationID', api_key: 'Y
|
|
118
116
|
|
119
117
|
The gem is compatible with [ActiveRecord](https://github.com/rails/rails/tree/master/activerecord), [Mongoid](https://github.com/mongoid/mongoid) and [Sequel](https://github.com/jeremyevans/sequel).
|
120
118
|
|
119
|
+
## Timeouts
|
121
120
|
|
122
|
-
|
121
|
+
You can configure a various timeout thresholds by setting the following options at initialization time:
|
123
122
|
|
123
|
+
```ruby
|
124
|
+
AlgoliaSearch.configuration = {
|
125
|
+
application_id: 'YourApplicationID',
|
126
|
+
api_key: 'YourAPIKey'
|
127
|
+
connect_timeout: 2,
|
128
|
+
receive_timeout: 30,
|
129
|
+
send_timeout: 30,
|
130
|
+
batch_timeout: 120,
|
131
|
+
search_timeout: 5
|
132
|
+
}
|
133
|
+
```
|
124
134
|
|
135
|
+
## Notes
|
136
|
+
|
137
|
+
This gem makes intensive use of Rails' callbacks to trigger the indexing tasks. If you're using methods bypassing `after_validation`, `before_save` or `after_commit` callbacks, it will not index your changes. For example: `update_attribute` doesn't perform validations checks, to perform validations when updating use `update_attributes`.
|
125
138
|
|
126
|
-
|
139
|
+
All methods injected by the `AlgoliaSearch` module are prefixed by `algolia_` and aliased to the associated short names if they aren't already defined.
|
140
|
+
|
141
|
+
```ruby
|
142
|
+
Contact.algolia_reindex! # <=> Contact.reindex!
|
143
|
+
|
144
|
+
Contact.algolia_search("jon doe") # <=> Contact.search("jon doe")
|
145
|
+
```
|
146
|
+
|
147
|
+
|
148
|
+
# Usage
|
149
|
+
|
150
|
+
|
151
|
+
|
152
|
+
## Index Schema
|
127
153
|
|
128
154
|
The following code will create a <code>Contact</code> index and add search capabilities to your <code>Contact</code> model:
|
129
155
|
|
@@ -240,16 +266,51 @@ index.search('something', { hitsPerPage: 10, page: 0 })
|
|
240
266
|
|
241
267
|
## Backend Search
|
242
268
|
|
243
|
-
|
269
|
+
***Notes:*** We recommend the usage of our [JavaScript API Client](https://github.com/algolia/algoliasearch-client-javascript) to perform queries directly from the end-user browser without going through your server.
|
270
|
+
|
271
|
+
A search returns ORM-compliant objects reloading them from your database. We recommend the usage of our [JavaScript API Client](https://github.com/algolia/algoliasearch-client-javascript) to perform queries to decrease the overall latency and offload your servers.
|
272
|
+
|
273
|
+
```ruby
|
274
|
+
hits = Contact.search("jon doe")
|
275
|
+
p hits
|
276
|
+
p hits.raw_answer # to get the original JSON raw answer
|
277
|
+
```
|
278
|
+
|
279
|
+
A `highlight_result` attribute is added to each ORM object:
|
244
280
|
|
245
281
|
```ruby
|
246
|
-
|
282
|
+
hits[0].highlight_result['first_name']['value']
|
247
283
|
```
|
248
284
|
|
249
|
-
|
285
|
+
If you want to retrieve the raw JSON answer from the API, without re-loading the objects from the database, you can use:
|
250
286
|
|
251
287
|
```ruby
|
252
|
-
|
288
|
+
json_answer = Contact.raw_search("jon doe")
|
289
|
+
p json_answer
|
290
|
+
p json_answer['hits']
|
291
|
+
p json_answer['facets']
|
292
|
+
```
|
293
|
+
|
294
|
+
Search parameters can be specified either through the index's [settings](https://github.com/algolia/algoliasearch-client-ruby#index-settings-parameters) statically in your model or dynamically at search time specifying [search parameters](https://github.com/algolia/algoliasearch-client-ruby#search) as second argument of the `search` method:
|
295
|
+
|
296
|
+
```ruby
|
297
|
+
class Contact < ActiveRecord::Base
|
298
|
+
include AlgoliaSearch
|
299
|
+
|
300
|
+
algoliasearch do
|
301
|
+
attribute :first_name, :last_name, :email
|
302
|
+
|
303
|
+
# default search parameters stored in the index settings
|
304
|
+
minWordSizeForApprox1 4
|
305
|
+
minWordSizeForApprox2 8
|
306
|
+
hitsPerPage 42
|
307
|
+
end
|
308
|
+
end
|
309
|
+
```
|
310
|
+
|
311
|
+
```ruby
|
312
|
+
# dynamical search parameters
|
313
|
+
p Contact.raw_search("jon doe", { :hitsPerPage => 5, :page => 2 })
|
253
314
|
```
|
254
315
|
|
255
316
|
## Backend Pagination
|
@@ -276,16 +337,120 @@ Then, as soon as you use the `search` method, the returning results will be a pa
|
|
276
337
|
<%= paginate @results %>
|
277
338
|
```
|
278
339
|
|
279
|
-
##
|
340
|
+
## Tags
|
280
341
|
|
281
|
-
|
342
|
+
Use the <code>tags</code> method to add tags to your record:
|
282
343
|
|
283
344
|
```ruby
|
284
|
-
Contact
|
345
|
+
class Contact < ActiveRecord::Base
|
346
|
+
include AlgoliaSearch
|
285
347
|
|
286
|
-
|
348
|
+
algoliasearch do
|
349
|
+
tags ['trusted']
|
350
|
+
end
|
351
|
+
end
|
287
352
|
```
|
288
353
|
|
354
|
+
or using dynamical values:
|
355
|
+
|
356
|
+
```ruby
|
357
|
+
class Contact < ActiveRecord::Base
|
358
|
+
include AlgoliaSearch
|
359
|
+
|
360
|
+
algoliasearch do
|
361
|
+
tags do
|
362
|
+
[first_name.blank? || last_name.blank? ? 'partial' : 'full', has_valid_email? ? 'valid_email' : 'invalid_email']
|
363
|
+
end
|
364
|
+
end
|
365
|
+
end
|
366
|
+
```
|
367
|
+
|
368
|
+
At query time, specify <code>{ tagFilters: 'tagvalue' }</code> or <code>{ tagFilters: ['tagvalue1', 'tagvalue2'] }</code> as search parameters to restrict the result set to specific tags.
|
369
|
+
|
370
|
+
## Faceting
|
371
|
+
|
372
|
+
Facets can be retrieved calling the extra `facets` method of the search answer.
|
373
|
+
|
374
|
+
```ruby
|
375
|
+
class Contact < ActiveRecord::Base
|
376
|
+
include AlgoliaSearch
|
377
|
+
|
378
|
+
algoliasearch do
|
379
|
+
# [...]
|
380
|
+
|
381
|
+
# specify the list of attributes available for faceting
|
382
|
+
attributesForFaceting [:company, :zip_code]
|
383
|
+
end
|
384
|
+
end
|
385
|
+
```
|
386
|
+
|
387
|
+
```ruby
|
388
|
+
hits = Contact.search("jon doe", { :facets => '*' })
|
389
|
+
p hits # ORM-compliant array of objects
|
390
|
+
p hits.facets # extra method added to retrieve facets
|
391
|
+
p hits.facets['company'] # facet values+count of facet 'company'
|
392
|
+
p hits.facets['zip_code'] # facet values+count of facet 'zip_code'
|
393
|
+
```
|
394
|
+
|
395
|
+
```ruby
|
396
|
+
raw_json = Contact.raw_search("jon doe", { :facets => '*' })
|
397
|
+
p raw_json['facets']
|
398
|
+
```
|
399
|
+
|
400
|
+
## Faceted search
|
401
|
+
|
402
|
+
You can also search for facet values.
|
403
|
+
|
404
|
+
```ruby
|
405
|
+
Product.search_for_facet_values('category', 'Headphones') # Array of {value, highlighted, count}
|
406
|
+
```
|
407
|
+
|
408
|
+
This method can also take any parameter a query can take.
|
409
|
+
This will adjust the search to only hits which would have matched the query.
|
410
|
+
|
411
|
+
```ruby
|
412
|
+
# Only sends back the categories containing red Apple products (and only counts those)
|
413
|
+
Product.search_for_facet_values('category', 'phone', {
|
414
|
+
query: 'red',
|
415
|
+
filters: 'brand:Apple'
|
416
|
+
}) # Array of phone categories linked to red Apple products
|
417
|
+
```
|
418
|
+
|
419
|
+
## Group by
|
420
|
+
|
421
|
+
More info on distinct for grouping can be found
|
422
|
+
[here](https://www.algolia.com/doc/guides/search/distinct#distinct-for-grouping).
|
423
|
+
|
424
|
+
```ruby
|
425
|
+
class Contact < ActiveRecord::Base
|
426
|
+
include AlgoliaSearch
|
427
|
+
|
428
|
+
algoliasearch do
|
429
|
+
# [...]
|
430
|
+
|
431
|
+
# specify the attribute to be used for distinguishing the records
|
432
|
+
# in this case the records will be grouped by company
|
433
|
+
attributeForDistinct "company"
|
434
|
+
end
|
435
|
+
end
|
436
|
+
```
|
437
|
+
|
438
|
+
## Geo-Search
|
439
|
+
|
440
|
+
Use the <code>geoloc</code> method to localize your record:
|
441
|
+
|
442
|
+
```ruby
|
443
|
+
class Contact < ActiveRecord::Base
|
444
|
+
include AlgoliaSearch
|
445
|
+
|
446
|
+
algoliasearch do
|
447
|
+
geoloc :lat_attr, :lng_attr
|
448
|
+
end
|
449
|
+
end
|
450
|
+
```
|
451
|
+
|
452
|
+
At query time, specify <code>{ aroundLatLng: "37.33, -121.89", aroundRadius: 50000 }</code> as search parameters to restrict the result set to 50KM around San Jose.
|
453
|
+
|
289
454
|
|
290
455
|
# Options
|
291
456
|
|
@@ -419,21 +584,6 @@ class Contact < ActiveRecord::Base
|
|
419
584
|
end
|
420
585
|
```
|
421
586
|
|
422
|
-
## Exceptions
|
423
|
-
|
424
|
-
You can disable exceptions that could be raised while trying to reach Algolia's API by using the `raise_on_failure` option:
|
425
|
-
|
426
|
-
```ruby
|
427
|
-
class Contact < ActiveRecord::Base
|
428
|
-
include AlgoliaSearch
|
429
|
-
|
430
|
-
# only raise exceptions in development env
|
431
|
-
algoliasearch raise_on_failure: Rails.env.development? do
|
432
|
-
attribute :first_name, :last_name, :email
|
433
|
-
end
|
434
|
-
end
|
435
|
-
```
|
436
|
-
|
437
587
|
## Custom index name
|
438
588
|
|
439
589
|
By default, the index name will be the class name, e.g. "Contact". You can customize the index name by using the `index_name` option:
|
@@ -448,7 +598,7 @@ class Contact < ActiveRecord::Base
|
|
448
598
|
end
|
449
599
|
```
|
450
600
|
|
451
|
-
## Per-environment
|
601
|
+
## Per-environment indices
|
452
602
|
|
453
603
|
You can suffix the index name with the current Rails environment using the following option:
|
454
604
|
|
@@ -636,6 +786,21 @@ end
|
|
636
786
|
|
637
787
|
***Notes:*** This option is not compatible with Ruby 1.8
|
638
788
|
|
789
|
+
## Exceptions
|
790
|
+
|
791
|
+
You can disable exceptions that could be raised while trying to reach Algolia's API by using the `raise_on_failure` option:
|
792
|
+
|
793
|
+
```ruby
|
794
|
+
class Contact < ActiveRecord::Base
|
795
|
+
include AlgoliaSearch
|
796
|
+
|
797
|
+
# only raise exceptions in development env
|
798
|
+
algoliasearch raise_on_failure: Rails.env.development? do
|
799
|
+
attribute :first_name, :last_name, :email
|
800
|
+
end
|
801
|
+
end
|
802
|
+
```
|
803
|
+
|
639
804
|
## Configuration example
|
640
805
|
|
641
806
|
Here is a real-word configuration example (from [HN Search](https://github.com/algolia/hn-search)):
|
@@ -694,7 +859,7 @@ end
|
|
694
859
|
```
|
695
860
|
|
696
861
|
|
697
|
-
#
|
862
|
+
# Indices
|
698
863
|
|
699
864
|
|
700
865
|
|
@@ -722,7 +887,7 @@ The gem provides 2 ways to reindex all your objects:
|
|
722
887
|
|
723
888
|
### Atomical reindexing
|
724
889
|
|
725
|
-
To reindex all your records (taking into account the deleted objects), the `reindex` class method
|
890
|
+
To reindex all your records (taking into account the deleted objects), the `reindex` class method indices all your objects to a temporary index called `<INDEX_NAME>.tmp` and moves the temporary index to the final one once everything is indexed (atomically). This is the safest way to reindex all your content.
|
726
891
|
|
727
892
|
```ruby
|
728
893
|
Contact.reindex
|
@@ -755,10 +920,6 @@ index = Contact.index
|
|
755
920
|
# index.get_settings, index.partial_update_object, ...
|
756
921
|
```
|
757
922
|
|
758
|
-
|
759
|
-
# Indexes
|
760
|
-
|
761
|
-
|
762
923
|
## Primary/replica
|
763
924
|
|
764
925
|
You can define replica indices using the <code>add_replica</code> method:
|
@@ -832,9 +993,9 @@ end
|
|
832
993
|
|
833
994
|
***Notes:*** If you target a single index from several models, you must never use `MyModel.reindex` and only use `MyModel.reindex!`. The `reindex` method uses a temporary index to perform an atomic reindexing: if you use it, the resulting index will only contain records for the current model because it will not reindex the others.
|
834
995
|
|
835
|
-
## Target multiple
|
996
|
+
## Target multiple indices
|
836
997
|
|
837
|
-
You can index a record in several
|
998
|
+
You can index a record in several indices using the <code>add_index</code> method:
|
838
999
|
|
839
1000
|
```ruby
|
840
1001
|
class Book < ActiveRecord::Base
|
@@ -876,194 +1037,11 @@ Book.search 'foo bar', index: 'Book_by_editor'
|
|
876
1037
|
```
|
877
1038
|
|
878
1039
|
|
879
|
-
#
|
880
|
-
|
881
|
-
|
882
|
-
|
883
|
-
## Tags
|
884
|
-
|
885
|
-
Use the <code>tags</code> method to add tags to your record:
|
886
|
-
|
887
|
-
```ruby
|
888
|
-
class Contact < ActiveRecord::Base
|
889
|
-
include AlgoliaSearch
|
890
|
-
|
891
|
-
algoliasearch do
|
892
|
-
tags ['trusted']
|
893
|
-
end
|
894
|
-
end
|
895
|
-
```
|
896
|
-
|
897
|
-
or using dynamical values:
|
898
|
-
|
899
|
-
```ruby
|
900
|
-
class Contact < ActiveRecord::Base
|
901
|
-
include AlgoliaSearch
|
902
|
-
|
903
|
-
algoliasearch do
|
904
|
-
tags do
|
905
|
-
[first_name.blank? || last_name.blank? ? 'partial' : 'full', has_valid_email? ? 'valid_email' : 'invalid_email']
|
906
|
-
end
|
907
|
-
end
|
908
|
-
end
|
909
|
-
```
|
910
|
-
|
911
|
-
At query time, specify <code>{ tagFilters: 'tagvalue' }</code> or <code>{ tagFilters: ['tagvalue1', 'tagvalue2'] }</code> as search parameters to restrict the result set to specific tags.
|
912
|
-
|
913
|
-
## Search
|
914
|
-
|
915
|
-
***Notes:*** We recommend the usage of our [JavaScript API Client](https://github.com/algolia/algoliasearch-client-javascript) to perform queries directly from the end-user browser without going through your server.
|
916
|
-
|
917
|
-
A search returns ORM-compliant objects reloading them from your database. We recommend the usage of our [JavaScript API Client](https://github.com/algolia/algoliasearch-client-javascript) to perform queries to decrease the overall latency and offload your servers.
|
918
|
-
|
919
|
-
```ruby
|
920
|
-
hits = Contact.search("jon doe")
|
921
|
-
p hits
|
922
|
-
p hits.raw_answer # to get the original JSON raw answer
|
923
|
-
```
|
924
|
-
|
925
|
-
A `highlight_result` attribute is added to each ORM object:
|
1040
|
+
# Testing
|
926
1041
|
|
927
|
-
```ruby
|
928
|
-
hits[0].highlight_result['first_name']['value']
|
929
|
-
```
|
930
1042
|
|
931
|
-
If you want to retrieve the raw JSON answer from the API, without re-loading the objects from the database, you can use:
|
932
1043
|
|
933
|
-
|
934
|
-
json_answer = Contact.raw_search("jon doe")
|
935
|
-
p json_answer
|
936
|
-
p json_answer['hits']
|
937
|
-
p json_answer['facets']
|
938
|
-
```
|
939
|
-
|
940
|
-
Search parameters can be specified either through the index's [settings](https://github.com/algolia/algoliasearch-client-ruby#index-settings-parameters) statically in your model or dynamically at search time specifying [search parameters](https://github.com/algolia/algoliasearch-client-ruby#search) as second argument of the `search` method:
|
941
|
-
|
942
|
-
```ruby
|
943
|
-
class Contact < ActiveRecord::Base
|
944
|
-
include AlgoliaSearch
|
945
|
-
|
946
|
-
algoliasearch do
|
947
|
-
attribute :first_name, :last_name, :email
|
948
|
-
|
949
|
-
# default search parameters stored in the index settings
|
950
|
-
minWordSizeForApprox1 4
|
951
|
-
minWordSizeForApprox2 8
|
952
|
-
hitsPerPage 42
|
953
|
-
end
|
954
|
-
end
|
955
|
-
```
|
956
|
-
|
957
|
-
```ruby
|
958
|
-
# dynamical search parameters
|
959
|
-
p Contact.raw_search("jon doe", { :hitsPerPage => 5, :page => 2 })
|
960
|
-
```
|
961
|
-
|
962
|
-
## Faceting
|
963
|
-
|
964
|
-
Facets can be retrieved calling the extra `facets` method of the search answer.
|
965
|
-
|
966
|
-
```ruby
|
967
|
-
class Contact < ActiveRecord::Base
|
968
|
-
include AlgoliaSearch
|
969
|
-
|
970
|
-
algoliasearch do
|
971
|
-
# [...]
|
972
|
-
|
973
|
-
# specify the list of attributes available for faceting
|
974
|
-
attributesForFaceting [:company, :zip_code]
|
975
|
-
end
|
976
|
-
end
|
977
|
-
```
|
978
|
-
|
979
|
-
```ruby
|
980
|
-
hits = Contact.search("jon doe", { :facets => '*' })
|
981
|
-
p hits # ORM-compliant array of objects
|
982
|
-
p hits.facets # extra method added to retrieve facets
|
983
|
-
p hits.facets['company'] # facet values+count of facet 'company'
|
984
|
-
p hits.facets['zip_code'] # facet values+count of facet 'zip_code'
|
985
|
-
```
|
986
|
-
|
987
|
-
```ruby
|
988
|
-
raw_json = Contact.raw_search("jon doe", { :facets => '*' })
|
989
|
-
p raw_json['facets']
|
990
|
-
```
|
991
|
-
|
992
|
-
## Facet search
|
993
|
-
|
994
|
-
You can also search for facet values.
|
995
|
-
|
996
|
-
```ruby
|
997
|
-
Product.search_for_facet_values('category', 'Headphones') # Array of {value, highlighted, count}
|
998
|
-
```
|
999
|
-
|
1000
|
-
This method can also take any parameter a query can take.
|
1001
|
-
This will adjust the search to only hits which would have matched the query.
|
1002
|
-
|
1003
|
-
```ruby
|
1004
|
-
# Only sends back the categories containing red Apple products (and only counts those)
|
1005
|
-
Product.search_for_facet_values('category', 'phone', {
|
1006
|
-
query: 'red',
|
1007
|
-
filters: 'brand:Apple'
|
1008
|
-
}) # Array of phone categories linked to red Apple products
|
1009
|
-
```
|
1010
|
-
|
1011
|
-
## Group by
|
1012
|
-
|
1013
|
-
More info on distinct for grouping can be found
|
1014
|
-
[here](https://www.algolia.com/doc/guides/search/distinct#distinct-for-grouping).
|
1015
|
-
|
1016
|
-
```ruby
|
1017
|
-
class Contact < ActiveRecord::Base
|
1018
|
-
include AlgoliaSearch
|
1019
|
-
|
1020
|
-
algoliasearch do
|
1021
|
-
# [...]
|
1022
|
-
|
1023
|
-
# specify the attribute to be used for distinguishing the records
|
1024
|
-
# in this case the records will be grouped by company
|
1025
|
-
attributeForDistinct "company"
|
1026
|
-
end
|
1027
|
-
end
|
1028
|
-
```
|
1029
|
-
|
1030
|
-
## Geo-Search
|
1031
|
-
|
1032
|
-
Use the <code>geoloc</code> method to localize your record:
|
1033
|
-
|
1034
|
-
```ruby
|
1035
|
-
class Contact < ActiveRecord::Base
|
1036
|
-
include AlgoliaSearch
|
1037
|
-
|
1038
|
-
algoliasearch do
|
1039
|
-
geoloc :lat_attr, :lng_attr
|
1040
|
-
end
|
1041
|
-
end
|
1042
|
-
```
|
1043
|
-
|
1044
|
-
At query time, specify <code>{ aroundLatLng: "37.33, -121.89", aroundRadius: 50000 }</code> as search parameters to restrict the result set to 50KM around San Jose.
|
1045
|
-
|
1046
|
-
## Caveats
|
1047
|
-
|
1048
|
-
This gem makes intensive use of Rails' callbacks to trigger the indexing tasks. If you're using methods bypassing `after_validation`, `before_save` or `after_commit` callbacks, it will not index your changes. For example: `update_attribute` doesn't perform validations checks, to perform validations when updating use `update_attributes`.
|
1049
|
-
|
1050
|
-
## Timeouts
|
1051
|
-
|
1052
|
-
You can configure a bunch of timeout threshold by setting the following options at initialization time:
|
1053
|
-
|
1054
|
-
```ruby
|
1055
|
-
AlgoliaSearch.configuration = {
|
1056
|
-
application_id: 'YourApplicationID',
|
1057
|
-
api_key: 'YourAPIKey'
|
1058
|
-
connect_timeout: 2,
|
1059
|
-
receive_timeout: 30,
|
1060
|
-
send_timeout: 30,
|
1061
|
-
batch_timeout: 120,
|
1062
|
-
search_timeout: 5
|
1063
|
-
}
|
1064
|
-
```
|
1065
|
-
|
1066
|
-
## Note on testing
|
1044
|
+
## Notes
|
1067
1045
|
|
1068
1046
|
To run the specs, please set the <code>ALGOLIA_APPLICATION_ID</code> and <code>ALGOLIA_API_KEY</code> environment variables. Since the tests are creating and removing indexes, DO NOT use your production account.
|
1069
1047
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.17.
|
1
|
+
1.17.1
|
data/lib/algoliasearch-rails.rb
CHANGED
@@ -66,7 +66,7 @@ module AlgoliaSearch
|
|
66
66
|
:unretrievableAttributes, :disableTypoToleranceOnWords, :disableTypoToleranceOnAttributes, :altCorrections,
|
67
67
|
:ignorePlurals, :maxValuesPerFacet, :distinct, :numericAttributesToIndex, :numericAttributesForFiltering,
|
68
68
|
:allowTyposOnNumericTokens, :allowCompressionOfIntegerArray,
|
69
|
-
:advancedSyntax]
|
69
|
+
:advancedSyntax, :disablePrefixOnAttributes, :disableTypoToleranceOnAttributes]
|
70
70
|
OPTIONS.each do |k|
|
71
71
|
define_method k do |v|
|
72
72
|
instance_variable_set("@#{k}", v)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: algoliasearch-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.17.
|
4
|
+
version: 1.17.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Algolia
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-01
|
11
|
+
date: 2017-02-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|