algoliasearch-rails 1.8.1 → 1.8.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 54a35c426a09e202829b65c9183d2e3708d60f0b
4
- data.tar.gz: 28fdaad10e6ae2c0c8dee4df2a2f81df61793664
3
+ metadata.gz: 936875f8253de8a3c68dd94f553d9db5b333e520
4
+ data.tar.gz: a8066cbfcb37003ce63683ad9431f7aa7839e089
5
5
  SHA512:
6
- metadata.gz: 2507961914f1a3b87d09e0a812e6d0f0b49c1a6d49ffc2ea5d36a1dea58cc510aabe13e4b8df511208549aaf368841893d28997394023ae4147d70eaee42c031
7
- data.tar.gz: eecad726fd78ef5e6afeea940ccc85abf02a52c7e50f83eb1e187e8636f648eaa0dcd7a2650963dc3d0e3f1941ad65e74a414ac8f0ab25ed3b2c7181b08b28ce
6
+ metadata.gz: 7393a5f1fe82912c9f322194491fea507eec9362275bde80242ceb464dcc686ebf7f822ed4ac56f2bac7cac6f38959f913b073856882aebe92cd64738fe81f99
7
+ data.tar.gz: 7d76df4a05fdf8c71dc9aa65ef4e5915becc5ccde12b831163a450ad518c149187d67594573b0e857fa2613d68812705ce57fdc2dcbc2392d7cd2be8f1d18d1e
data/ChangeLog CHANGED
@@ -1,5 +1,9 @@
1
1
  CHANGELOG
2
2
 
3
+ 2014-02-15 1.8.2
4
+
5
+ * Ability to retrieve facets and raw answer as well
6
+
3
7
  2014-02-14 1.8.1
4
8
 
5
9
  * Fixed backend pagination not taking current page into account at display time.
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
- Geo-Search
361
- -----------
361
+ Search
362
+ ----------
362
363
 
363
- Use the <code>geoloc</code> method to localize your record:
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
- geoloc :lat_attr, :lng_attr
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
- 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.
399
+ ```ruby
400
+ # dynamical search parameters
401
+ p Contact.search("jon doe", { :hitsPerPage => 5, :page => 2 })
402
+ ```
376
403
 
377
- Search settings
378
- ----------
404
+ Faceting
405
+ ---------
379
406
 
380
- All [settings](https://github.com/algolia/algoliasearch-client-ruby#index-settings) can be specified either statically in your model or dynamically at search time using [search options](https://github.com/algolia/algoliasearch-client-ruby#search):
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
- attribute :first_name, :last_name, :email
388
- minWordSizeForApprox1 2
389
- minWordSizeForApprox2 5
390
- hitsPerPage 42
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
- p Contact.search("jon doe", hitsPerPage: 5, page: 2)
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
 
@@ -6,7 +6,7 @@
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "algoliasearch-rails"
9
- s.version = "1.8.1"
9
+ s.version = "1.8.2"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
12
  s.authors = ["Algolia"]
@@ -205,13 +205,35 @@ module AlgoliaSearch
205
205
  @algolia_index = nil
206
206
  end
207
207
 
208
- def algolia_raw_search(q, settings = {})
208
+ def algolia_raw_search(q, params = {})
209
209
  algolia_ensure_init
210
- @algolia_index.search(q, Hash[settings.map { |k,v| [k.to_s, v.to_s] }])
210
+ @algolia_index.search(q, Hash[params.map { |k,v| [k.to_s, v.to_s] }])
211
211
  end
212
212
 
213
- def algolia_search(q, settings = {})
214
- json = algolia_raw_search(q, settings)
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: algoliasearch-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.8.1
4
+ version: 1.8.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Algolia