algoliasearch 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ data.tar.gz: f1833d8ef8fe5af6c44ce31e169929b7c84b049c
4
+ metadata.gz: e02ffcaf7597034df5b829752a7b8cc2069cbd19
5
+ SHA512:
6
+ data.tar.gz: b5a0bcf7afb047ffb0cc5a9b53a770e48c586a912fa03d1c49a3a7ef007be42e08dcd7fea13ca5c61e459256537f74a35ef7e4fcab5411f3eaaf2f2cffdaaf74
7
+ metadata.gz: 536122b2dc1a59f5e50f09819af7b3b996c2b8ca0e1093534a799e4d5f4a5108e66402c16389882c965a7cbf15e4ab65528f29244359511fb4e4dbdcc75b99c6
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Add dependencies to develop your gem here.
4
+ # Include everything needed to run rake, tests, features, etc.
5
+ group :development do
6
+ gem "bundler"
7
+ gem "jeweler"
8
+ gem "test-unit"
9
+ gem "rspec"
10
+ gem 'shoulda-context'
11
+ gem 'webmock'
12
+ gem 'redgreen'
13
+ end
14
+
15
+ gem "curb"
@@ -0,0 +1,44 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ addressable (2.3.4)
5
+ crack (0.3.2)
6
+ curb (0.8.4)
7
+ diff-lcs (1.1.3)
8
+ git (1.2.5)
9
+ jeweler (1.8.4)
10
+ bundler (~> 1.0)
11
+ git (>= 1.2.5)
12
+ rake
13
+ rdoc
14
+ json (1.8.0)
15
+ rake (10.0.4)
16
+ rdoc (3.12.2)
17
+ json (~> 1.4)
18
+ redgreen (1.2.2)
19
+ rspec (2.11.0)
20
+ rspec-core (~> 2.11.0)
21
+ rspec-expectations (~> 2.11.0)
22
+ rspec-mocks (~> 2.11.0)
23
+ rspec-core (2.11.1)
24
+ rspec-expectations (2.11.3)
25
+ diff-lcs (~> 1.1.3)
26
+ rspec-mocks (2.11.2)
27
+ shoulda-context (1.0.2)
28
+ test-unit (2.5.4)
29
+ webmock (1.9.0)
30
+ addressable (>= 2.2.7)
31
+ crack (>= 0.1.7)
32
+
33
+ PLATFORMS
34
+ ruby
35
+
36
+ DEPENDENCIES
37
+ bundler
38
+ curb
39
+ jeweler
40
+ redgreen
41
+ rspec
42
+ shoulda-context
43
+ test-unit
44
+ webmock
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 Algolia
4
+ http://www.algolia.com/
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in
14
+ all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ THE SOFTWARE.
@@ -0,0 +1,395 @@
1
+ Algolia Search API Client for Ruby
2
+ ==================
3
+
4
+ This Ruby client let you easily use the Algolia Search API from your backend.
5
+ The service is currently in Beta, you can request an invite on our [website](http://www.algolia.com/pricing/).
6
+
7
+ Table of Content
8
+ -------------
9
+ **Get started**
10
+
11
+ 1. [Setup](#setup)
12
+ 1. [Quick Start](#quick-start)
13
+
14
+ **Commands reference**
15
+
16
+ 1. [Search](#search)
17
+ 1. [Add a new object](#add-a-new-object-in-the-index)
18
+ 1. [Update an object](#update-an-existing-object-in-the-index)
19
+ 1. [Get an object](#get-an-object)
20
+ 1. [Delete an object](#delete-an-object)
21
+ 1. [Index settings](#index-settings)
22
+ 1. [List indexes](#list-indexes)
23
+ 1. [Delete an index](#delete-an-index)
24
+ 1. [Wait indexing](#wait-indexing)
25
+ 1. [Batch writes](#batch-writes)
26
+ 1. [Security / User API Keys](#security--user-api-keys)
27
+
28
+ Setup
29
+ -------------
30
+ To setup your project, follow these steps:
31
+
32
+ 2. Install AlgoliaSearch using <code>gem install algoliasearch</code>.
33
+ 2. Initialize the client with your ApplicationID and API-Key (you can find all of them on your Algolia account).
34
+
35
+ ```ruby
36
+ require 'rubygems'
37
+ require 'algoliasearch'
38
+
39
+ Algolia.init :application_id => "YourApplicationID",
40
+ :api_key => "YourAPIKey"
41
+ ```
42
+
43
+ Quick Start
44
+ -------------
45
+ This quick start is a 30 seconds tutorial where you can discover how to index and search objects.
46
+
47
+ Without any prior-configuration, you can index [500 contacts](https://github.com/algolia/algoliasearch-client-ruby/blob/master/contacts.json) in the ```contacts``` index with the following code:
48
+ ```ruby
49
+ index = Algolia::Index.new("contacts")
50
+ batch = JSON.parse(File.read("contacts.json"))
51
+ index.add_objects(batch)
52
+ ```
53
+
54
+ You can then start to search for a contact firstname, lastname, company, ... (even with typos):
55
+ ```ruby
56
+ # search by firstname
57
+ puts index.search('jimmie').to_json
58
+ # search a firstname with typo
59
+ puts index.search('jimie').to_json
60
+ # search for a company
61
+ puts index.search('california paint').to_json
62
+ # search for a firstname & company
63
+ puts index.search('jimmie paint').to_json
64
+ ```
65
+
66
+ Settings can be customized to tune the search behavior. For example you can add a custom sort by number of followers to the already good out-of-the-box relevance:
67
+ ```ruby
68
+ index.set_settings({"customRanking" => ["desc(followers)"]})
69
+ ```
70
+ You can also configure the list of attributes you want to index by order of importance (first = most important):
71
+ ```ruby
72
+ index.set_settings({"attributesToIndex" => ["lastname", "firstname", "company",
73
+ "email", "city", "address"]})
74
+ ```
75
+
76
+ Since the engine is designed to suggest results as you type, you'll generally search by prefix. In this case the order of attributes is very important to decide which hit is the best:
77
+ ```ruby
78
+ puts index.search('or').to_json
79
+ puts index.search('jim').to_json
80
+ ```
81
+
82
+ Search
83
+ -------------
84
+ > **Opening note:** If you are building a web application, you may be more interested in using our [javascript client](https://github.com/algolia/algoliasearch-client-js) to send queries. It brings two benefits: (i) your users get a better response time by avoiding to go threw your servers, and (ii) it will offload your servers of unnecessary tasks.
85
+
86
+ To perform a search, you just need to initialize the index and perform a call to the search function.<br/>
87
+ You can use the following optional arguments:
88
+
89
+ * **attributes**: a string that contains the names of attributes to retrieve separated by a comma.<br/>By default all attributes are retrieved.
90
+ * **attributesToHighlight**: a string that contains the names of attributes to highlight separated by a comma.<br/>By default indexed attributes are highlighted. Numerical attributes cannot be highlighted. A **matchLevel** is returned for each highlighted attribute and can contain: "full" if all the query terms were found in the attribute, "partial" if only some of the query terms were found, or "none" if none of the query terms were found.
91
+ * **attributesToSnippet**: a string that contains the names of attributes to snippet alongside the number of words to return (syntax is 'attributeName:nbWords'). Attributes are separated by a comma (Example: "attributesToSnippet=name:10,content:10").<br/>By default no snippet is computed.
92
+ * **minWordSizeForApprox1**: the minimum number of characters in a query word to accept one typo in this word.<br/>Defaults to 3.
93
+ * **minWordSizeForApprox2**: the minimum number of characters in a query word to accept two typos in this word.<br/>Defaults to 7.
94
+ * **getRankingInfo**: if set to 1, the result hits will contain ranking information in _rankingInfo attribute.
95
+ * **page**: *(pagination parameter)* page to retrieve (zero base).<br/>Defaults to 0.
96
+ * **hitsPerPage**: *(pagination parameter)* number of hits per page.<br/>Defaults to 10.
97
+ * **aroundLatLng**: search for entries around a given latitude/longitude (specified as two floats separated by a comma).<br/>For example `aroundLatLng=47.316669,5.016670`).<br/>You can specify the maximum distance in meters with the **aroundRadius** parameter (in meters) and the precision for ranking with **aroundPrecision** (for example if you set aroundPrecision=100, two objects that are distant of less than 100m will be considered as identical for "geo" ranking parameter).<br/>At indexing, you should specify geoloc of an object with the _geoloc attribute (in the form `{"_geoloc":{"lat":48.853409, "lng":2.348800}}`)
98
+ * **insideBoundingBox**: search entries inside a given area defined by the two extreme points of a rectangle (defined by 4 floats: p1Lat,p1Lng,p2Lat,p2Lng).<br/>For example `insideBoundingBox=47.3165,4.9665,47.3424,5.0201`).<br/>At indexing, you should specify geoloc of an object with the _geoloc attribute (in the form `{"_geoloc":{"lat":48.853409, "lng":2.348800}}`)
99
+ * **queryType**: select how the query words are interpreted:
100
+ * **prefixAll**: all query words are interpreted as prefixes (default behavior).
101
+ * **prefixLast**: only the last word is interpreted as a prefix. This option is recommended if you have a lot of content to speedup the processing.
102
+ * **prefixNone**: no query word is interpreted as a prefix. This option is not recommended.
103
+ * **tags**: filter the query by a set of tags. You can AND tags by separating them by commas. To OR tags, you must add parentheses. For example, `tags=tag1,(tag2,tag3)` means *tag1 AND (tag2 OR tag3)*.<br/>At indexing, tags should be added in the _tags attribute of objects (for example `{"_tags":["tag1","tag2"]}` )
104
+
105
+ ```ruby
106
+ index = Algolia::Index.new("contacts")
107
+ res = index.search("query string")
108
+ res = index.search("query string", { "attributes" => "firstname,lastname", "hitsPerPage" => 20})
109
+ ```
110
+
111
+ The server response will look like:
112
+
113
+ ```javascript
114
+ {
115
+ "hits": [
116
+ {
117
+ "firstname": "Jimmie",
118
+ "lastname": "Barninger",
119
+ "company": "California Paint & Wlpaper Str",
120
+ "address": "Box #-4038",
121
+ "city": "Modesto",
122
+ "county": "Stanislaus",
123
+ "state": "CA",
124
+ "zip": "95352",
125
+ "phone": "209-525-7568",
126
+ "fax": "209-525-4389",
127
+ "email": "jimmie@barninger.com",
128
+ "web": "http://www.jimmiebarninger.com",
129
+ "followers": 3947,
130
+ "objectID": "433",
131
+ "_highlightResult": {
132
+ "firstname": {
133
+ "value": "<em>Jimmie</em>",
134
+ "matchLevel": "partial"
135
+ },
136
+ "lastname": {
137
+ "value": "Barninger",
138
+ "matchLevel": "none"
139
+ },
140
+ "company": {
141
+ "value": "California <em>Paint</em> & Wlpaper Str",
142
+ "matchLevel": "partial"
143
+ },
144
+ "address": {
145
+ "value": "Box #-4038",
146
+ "matchLevel": "none"
147
+ },
148
+ "city": {
149
+ "value": "Modesto",
150
+ "matchLevel": "none"
151
+ },
152
+ "email": {
153
+ "value": "<em>jimmie</em>@barninger.com",
154
+ "matchLevel": "partial"
155
+ }
156
+ }
157
+ }
158
+ ],
159
+ "page": 0,
160
+ "nbHits": 1,
161
+ "nbPages": 1,
162
+ "hitsPerPage": 20,
163
+ "processingTimeMS": 1,
164
+ "query": "jimmie paint",
165
+ "params": "query=jimmie+paint&"
166
+ }
167
+ ```
168
+
169
+ Add a new object in the Index
170
+ -------------
171
+
172
+ Each entry in an index has a unique identifier called `objectID`. You have two ways to add en entry in the index:
173
+
174
+ 1. Using automatic `objectID` assignement, you will be able to retrieve it in the answer.
175
+ 2. Passing your own `objectID`
176
+
177
+ You don't need to explicitely create an index, it will be automatically created the first time you add an object.
178
+ Objects are schema less, you don't need any configuration to start indexing. The settings section provide details about advanced settings.
179
+
180
+ Example with automatic `objectID` assignement:
181
+
182
+ ```ruby
183
+ res = index.add_object({"firstname" => "Jimmie",
184
+ "lastname" => "Barninger"})
185
+ puts "ObjectID=" + res["objectID"]
186
+ ```
187
+
188
+ Example with manual `objectID` assignement:
189
+
190
+ ```ruby
191
+ res = index.add_object({"firstname" => "Jimmie",
192
+ "lastname" => "Barninger"}, "myID")
193
+ puts "ObjectID=" + res["objectID"]
194
+ ```
195
+
196
+ Update an existing object in the Index
197
+ -------------
198
+
199
+ You have two options to update an existing object:
200
+
201
+ 1. Replace all its attributes.
202
+ 2. Replace only some attributes.
203
+
204
+ Example to replace all the content of an existing object:
205
+
206
+ ```ruby
207
+ index.save_object({"firstname" => "Jimmie",
208
+ "lastname" => "Barninger",
209
+ "city" => "New York",
210
+ "objectID" => "myID"})
211
+ ```
212
+
213
+ Example to update only the city attribute of an existing object:
214
+
215
+ ```ruby
216
+ index.partial_update_object({"city" => "San Francisco",
217
+ "objectID" => "myID"})
218
+ ```
219
+
220
+ Get an object
221
+ -------------
222
+
223
+ You can easily retrieve an object using its `objectID` and optionnaly a list of attributes you want to retrieve (using comma as separator):
224
+
225
+ ```ruby
226
+ # Retrieves all attributes
227
+ index.get_object("myID")
228
+ # Retrieves firstname and lastname attributes
229
+ res = index.get_object("myID", "firstname,lastname")
230
+ # Retrieves only the firstname attribute
231
+ res = index.get_object("myID", "fistname")
232
+ ```
233
+
234
+ Delete an object
235
+ -------------
236
+
237
+ You can delete an object using its `objectID`:
238
+
239
+ ```ruby
240
+ index.delete_object("myID")
241
+ ```
242
+
243
+ Index Settings
244
+ -------------
245
+
246
+ You can retrieve all settings using the `getSettings` function. The result will contains the following attributes:
247
+
248
+ * **minWordSizeForApprox1**: (integer) the minimum number of characters to accept one typo (default = 3).
249
+ * **minWordSizeForApprox2**: (integer) the minimum number of characters to accept two typos (default = 7).
250
+ * **hitsPerPage**: (integer) the number of hits per page (default = 10).
251
+ * **attributesToRetrieve**: (array of strings) default list of attributes to retrieve in objects.
252
+ * **attributesToHighlight**: (array of strings) default list of attributes to highlight.
253
+ * **attributesToSnippet**: (array of strings) default list of attributes to snippet alongside the number of words to return (syntax is 'attributeName:nbWords')<br/>By default no snippet is computed.
254
+ * **attributesToIndex**: (array of strings) the list of fields you want to index.<br/>By default all textual attributes of your objects are indexed, but you should update it to get optimal results.<br/>This parameter has two important uses:
255
+ * *Limits the attributes to index*.<br/>For example if you store a binary image in base64, you want to store it and be able to retrieve it but you don't want to search in the base64 string.
256
+ * *Controls part of the ranking*.<br/>Matches in attributes at the beginning of the list will be considered more important than matches in attributes further down the list.
257
+ * **ranking**: (array of strings) controls the way hits are sorted.<br/>We have five available criteria:
258
+ * **typo**: sort according to number of typos,
259
+ * **geo**: sort according to decreasing distance when performing a geo-location based search,
260
+ * **proximity**: sort according to the proximity of query words in hits,
261
+ * **attribute**: sort according to the order of attributes defined by **attributesToIndex**,
262
+ * **exact**: sort according to the number of words that are matched identical to query word (and not as a prefix),
263
+ * **custom**: sort according to a user defined formula set in **customRanking** attribute.
264
+ <br/>The default order is `["typo", "geo", "proximity", "attribute", "exact", "custom"]`. We strongly recommend to keep this configuration.
265
+ * **customRanking**: (array of strings) lets you specify part of the ranking.<br/>The syntax of this condition is an array of strings containing attributes prefixed by asc (ascending order) or desc (descending order) operator.
266
+ For example `"customRanking" => ["desc(population)", "asc(name)"]`
267
+ * **queryType**: select how the query words are interpreted:
268
+ * **prefixAll**: all query words are interpreted as prefixes (default behavior).
269
+ * **prefixLast**: only the last word is interpreted as a prefix. This option is recommended if you have a lot of content to speedup the processing.
270
+ * **prefixNone**: no query word is interpreted as a prefix. This option is not recommended.
271
+
272
+ You can easily retrieve settings or update them:
273
+
274
+ ```ruby
275
+ res = index.get_settings
276
+ puts settings.to_json
277
+ ```
278
+
279
+ ```ruby
280
+ index.set_settings({"customRanking" => ["desc(followers)"]})
281
+ ```
282
+
283
+ List indexes
284
+ -------------
285
+ You can list all your indexes with their associated information (number of entries, disk size, etc.) with the `list_indexes` method:
286
+
287
+ ```ruby
288
+ Algolia.list_indexes
289
+ ```
290
+
291
+ Delete an index
292
+ -------------
293
+ You can delete an index using its name:
294
+
295
+ ```ruby
296
+ index = Algolia::Index.new("contacts")
297
+ index.delete
298
+ ```
299
+
300
+ Wait indexing
301
+ -------------
302
+
303
+ All write operations return a `taskID` when the job is securely stored on our infrastructure but not when the job is published in your index. Even if it's extremely fast, you can easily ensure indexing is complete using the same method with a `!`.
304
+
305
+ For example, to wait for indexing of a new object:
306
+ ```ruby
307
+ res = index.add_object!({"firstname" => "Jimmie",
308
+ "lastname" => "Barninger"})
309
+ ```
310
+
311
+ If you want to ensure multiple objects have been indexed, you can only check the biggest taskID.
312
+
313
+ Batch writes
314
+ -------------
315
+
316
+ You may want to perform multiple operations with one API call to reduce latency.
317
+ We expose two methods to perform batch:
318
+ * `addObjects`: add an array of object using automatic `objectID` assignement
319
+ * `saveObjects`: add or update an array of object that contains an `objectID` attribute
320
+
321
+ Example using automatic `objectID` assignement:
322
+ ```ruby
323
+ res = index.add_objects([{"firstname" => "Jimmie",
324
+ "lastname" => "Barninger"},
325
+ {"firstname" => "Warren",
326
+ "lastname" => "Speach"}])
327
+ ```
328
+
329
+ Example with user defined `objectID` (add or update):
330
+ ```ruby
331
+ res = index.save_objects([{"firstname" => "Jimmie",
332
+ "lastname" => "Barninger",
333
+ "objectID" => "myID1"},
334
+ {"firstname" => "Warren",
335
+ "lastname" => "Speach",
336
+ "objectID" => "myID2"}])
337
+ ```
338
+
339
+ Security / User API Keys
340
+ -------------
341
+
342
+ The admin API key provides full control of all your indexes.
343
+ You can also generate user API keys to control security.
344
+ These API keys can be restricted to a set of operations or/and restricted to a given index.
345
+
346
+ To list existing keys, you can use `list_user_keys` method:
347
+ ```ruby
348
+ # Lists global API Keys
349
+ Algolia.list_user_keys
350
+ # Lists API Keys that can access only to this index
351
+ index.list_user_keys
352
+ ```
353
+
354
+ Each key is defined by a set of rights that specify the authorized actions. The different rights are:
355
+ * **search**: allows to search,
356
+ * **addObject**: allows to add/update an object in the index,
357
+ * **deleteObject**: allows to delete an existing object,
358
+ * **deleteIndex**: allows to delete index content,
359
+ * **settings**: allows to get index settings,
360
+ * **editSettings**: allows to change index settings.
361
+
362
+ Example of API Key creation:
363
+ ```ruby
364
+ # Creates a new global API key that can only perform search actions
365
+ res = Algolia.add_user_key(["search"])
366
+ puts res['key']
367
+ # Creates a new API key that can only perform search action on this index
368
+ res = index.add_user_key(["search"])
369
+ puts res['key']
370
+ ```
371
+ You can also create a temporary API key that will be valid only for a specific period of time (in seconds):
372
+ ```ruby
373
+ # Creates a new global API key that is valid for 300 seconds
374
+ res = Algolia.add_user_key(["search"], 300)
375
+ puts res['key']
376
+ # Creates a new index specific API key valid for 300 seconds
377
+ res = index.add_user_key(["search"], 300)
378
+ puts res['key']
379
+ ```
380
+
381
+ Get the rights of a given key:
382
+ ```ruby
383
+ # Gets the rights of a global key
384
+ Algolia.get_user_key("f420238212c54dcfad07ea0aa6d5c45f")
385
+ # Gets the rights of an index specific key
386
+ index.get_user_key("71671c38001bf3ac857bc82052485107")
387
+ ```
388
+
389
+ Delete an existing key:
390
+ ```ruby
391
+ # Deletes a global key
392
+ Algolia.delete_user_key("f420238212c54dcfad07ea0aa6d5c45f")
393
+ # Deletes an index specific key
394
+ index.delete_user_key("71671c38001bf3ac857bc82052485107")
395
+ ```