algoliasearch-rails 1.8.1 → 1.8.2
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 +67 -14
- data/algoliasearch-rails.gemspec +1 -1
- data/lib/algoliasearch-rails.rb +30 -5
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 936875f8253de8a3c68dd94f553d9db5b333e520
|
4
|
+
data.tar.gz: a8066cbfcb37003ce63683ad9431f7aa7839e089
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7393a5f1fe82912c9f322194491fea507eec9362275bde80242ceb464dcc686ebf7f822ed4ac56f2bac7cac6f38959f913b073856882aebe92cd64738fe81f99
|
7
|
+
data.tar.gz: 7d76df4a05fdf8c71dc9aa65ef4e5915becc5ccde12b831163a450ad518c149187d67594573b0e857fa2613d68812705ce57fdc2dcbc2392d7cd2be8f1d18d1e
|
data/ChangeLog
CHANGED
data/README.md
CHANGED
@@ -19,8 +19,9 @@ Table of Content
|
|
19
19
|
1. [Configuration example](#configuration-example)
|
20
20
|
1. [Indexing](#indexing)
|
21
21
|
1. [Tags](#tags)
|
22
|
+
1. [Search](#search)
|
23
|
+
1. [Faceting](#faceting)
|
22
24
|
1. [Geo-search](#geo-search)
|
23
|
-
1. [Search settings](#search-settings)
|
24
25
|
1. [Typeahead UI](#typeahead-ui)
|
25
26
|
1. [Note on testing](#note-on-testing)
|
26
27
|
|
@@ -357,45 +358,97 @@ end
|
|
357
358
|
|
358
359
|
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.
|
359
360
|
|
360
|
-
|
361
|
-
|
361
|
+
Search
|
362
|
+
----------
|
362
363
|
|
363
|
-
|
364
|
+
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-js) to perform queries to decrease the overall latency and offload your servers.
|
365
|
+
|
366
|
+
|
367
|
+
```ruby
|
368
|
+
hits = Contact.search("jon doe")
|
369
|
+
p hits
|
370
|
+
p hits.raw_answer # to get the original JSON raw answer
|
371
|
+
```
|
372
|
+
|
373
|
+
If you want to retrieve the raw JSON answer from the API, without re-loading the objects from the database, you can use:
|
374
|
+
|
375
|
+
```ruby
|
376
|
+
json_answer = Contact.raw_search("jon doe")
|
377
|
+
p json_answer
|
378
|
+
p json_answer['hits']
|
379
|
+
p json_answer['facets']
|
380
|
+
```
|
381
|
+
|
382
|
+
Search parameters can be specified either through the index's [settings](https://github.com/algolia/algoliasearch-client-ruby#index-settings) 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:
|
364
383
|
|
365
384
|
```ruby
|
366
385
|
class Contact < ActiveRecord::Base
|
367
386
|
include AlgoliaSearch
|
368
387
|
|
369
388
|
algoliasearch do
|
370
|
-
|
389
|
+
attribute :first_name, :last_name, :email
|
390
|
+
|
391
|
+
# default search parameters stored in the index settings
|
392
|
+
minWordSizeForApprox1 4
|
393
|
+
minWordSizeForApprox2 8
|
394
|
+
hitsPerPage 42
|
371
395
|
end
|
372
396
|
end
|
373
397
|
```
|
374
398
|
|
375
|
-
|
399
|
+
```ruby
|
400
|
+
# dynamical search parameters
|
401
|
+
p Contact.search("jon doe", { :hitsPerPage => 5, :page => 2 })
|
402
|
+
```
|
376
403
|
|
377
|
-
|
378
|
-
|
404
|
+
Faceting
|
405
|
+
---------
|
379
406
|
|
380
|
-
|
407
|
+
Facets can be retrieved calling the extra ```facets``` method of the search answer.
|
381
408
|
|
382
409
|
```ruby
|
383
410
|
class Contact < ActiveRecord::Base
|
384
411
|
include AlgoliaSearch
|
385
412
|
|
386
413
|
algoliasearch do
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
414
|
+
# [...]
|
415
|
+
|
416
|
+
# specify the list of attributes available for faceting
|
417
|
+
attributesForFaceting [:company, :zip_code]
|
391
418
|
end
|
392
419
|
end
|
393
420
|
```
|
394
421
|
|
395
422
|
```ruby
|
396
|
-
|
423
|
+
hits = Contact.search("jon doe", { :facets => '*' })
|
424
|
+
p hits # ORM-compliant array of objects
|
425
|
+
p hits.facets # extra method added to retrieve facets
|
426
|
+
p hits.facets['company'] # facet values+count of facet 'company'
|
427
|
+
p hits.facets['zip_code'] # facet values+count of facet 'zip_code'
|
428
|
+
```
|
429
|
+
|
430
|
+
```ruby
|
431
|
+
raw_json = Contact.raw_search("jon doe", { :facets => '*' })
|
432
|
+
p raw_json['facets']
|
397
433
|
```
|
398
434
|
|
435
|
+
Geo-Search
|
436
|
+
-----------
|
437
|
+
|
438
|
+
Use the <code>geoloc</code> method to localize your record:
|
439
|
+
|
440
|
+
```ruby
|
441
|
+
class Contact < ActiveRecord::Base
|
442
|
+
include AlgoliaSearch
|
443
|
+
|
444
|
+
algoliasearch do
|
445
|
+
geoloc :lat_attr, :lng_attr
|
446
|
+
end
|
447
|
+
end
|
448
|
+
```
|
449
|
+
|
450
|
+
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.
|
451
|
+
|
399
452
|
Typeahead UI
|
400
453
|
-------------
|
401
454
|
|
data/algoliasearch-rails.gemspec
CHANGED
data/lib/algoliasearch-rails.rb
CHANGED
@@ -205,13 +205,35 @@ module AlgoliaSearch
|
|
205
205
|
@algolia_index = nil
|
206
206
|
end
|
207
207
|
|
208
|
-
def algolia_raw_search(q,
|
208
|
+
def algolia_raw_search(q, params = {})
|
209
209
|
algolia_ensure_init
|
210
|
-
@algolia_index.search(q, Hash[
|
210
|
+
@algolia_index.search(q, Hash[params.map { |k,v| [k.to_s, v.to_s] }])
|
211
211
|
end
|
212
212
|
|
213
|
-
|
214
|
-
|
213
|
+
module AdditionalMethods
|
214
|
+
def self.extended(base)
|
215
|
+
class <<base
|
216
|
+
alias_method :raw_answer, :algolia_raw_answer unless method_defined? :raw_answer
|
217
|
+
alias_method :facets, :algolia_facets unless method_defined? :facets
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
def algolia_raw_answer
|
222
|
+
@algolia_json
|
223
|
+
end
|
224
|
+
|
225
|
+
def algolia_facets
|
226
|
+
@algolia_json['facets']
|
227
|
+
end
|
228
|
+
|
229
|
+
private
|
230
|
+
def algolia_init_raw_answer(json)
|
231
|
+
@algolia_json = json
|
232
|
+
end
|
233
|
+
end
|
234
|
+
|
235
|
+
def algolia_search(q, params = {})
|
236
|
+
json = algolia_raw_search(q, params)
|
215
237
|
results = json['hits'].map do |hit|
|
216
238
|
o = algolia_options[:type].where(algolia_object_id_method => hit['objectID']).first
|
217
239
|
if o
|
@@ -219,7 +241,10 @@ module AlgoliaSearch
|
|
219
241
|
o
|
220
242
|
end
|
221
243
|
end.compact
|
222
|
-
AlgoliaSearch::Pagination.create(results, json['nbHits'].to_i, algolia_options.merge({ page: json['page'] + 1 }))
|
244
|
+
res = AlgoliaSearch::Pagination.create(results, json['nbHits'].to_i, algolia_options.merge({ page: json['page'] + 1 }))
|
245
|
+
res.extend(AdditionalMethods)
|
246
|
+
res.send(:algolia_init_raw_answer, json)
|
247
|
+
res
|
223
248
|
end
|
224
249
|
|
225
250
|
def algolia_index
|