Skip to content

Commit ceaa640

Browse files
curquizaeskombro
andauthored
Prepare for Meilisearch v0.11 (#39)
* Rename fields_frequency to fields_distribution according to MeiliSearch v0.11 * Add method to use faceting sub routes (#44) * Implement faceting in search (#46) * Implement faceting in search * Update spec/meilisearch/index/search_spec.rb Co-authored-by: Samuel Jimenez <[email protected]> * Update spec/meilisearch/index/search_spec.rb Co-authored-by: Samuel Jimenez <[email protected]> * Update spec/meilisearch/index/search_spec.rb Co-authored-by: Samuel Jimenez <[email protected]> * Update spec/meilisearch/index/search_spec.rb Co-authored-by: Samuel Jimenez <[email protected]> * Update spec/meilisearch/index/search_spec.rb Co-authored-by: Samuel Jimenez <[email protected]> Co-authored-by: Samuel Jimenez <[email protected]> * Upd tests according to change in MeiliSearch about anthentication (#48) * Add debugging part in README * Add test with multiple facetFilters (#51) * Change create_index prototype (#50) Co-authored-by: Samuel Jimenez <[email protected]>
1 parent 7f4cd43 commit ceaa640

18 files changed

+287
-161
lines changed

.rubocop_todo.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2020-06-03 19:41:42 +0200 using RuboCop version 0.85.0.
3+
# on 2020-06-04 16:06:38 +0200 using RuboCop version 0.85.0.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
@@ -10,17 +10,17 @@
1010
# Configuration parameters: CountComments, ExcludedMethods.
1111
# ExcludedMethods: refine
1212
Metrics/BlockLength:
13-
Max: 434
13+
Max: 463
1414

1515
# Offense count: 1
1616
# Configuration parameters: CountComments.
1717
Metrics/ClassLength:
18-
Max: 171
18+
Max: 190
1919

20-
# Offense count: 1
20+
# Offense count: 2
2121
# Configuration parameters: CountComments, ExcludedMethods.
2222
Metrics/MethodLength:
23-
Max: 12
23+
Max: 11
2424

2525
# Offense count: 1
2626
Naming/AccessorMethodName:

README.md

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@
3636
- [Search](#search)
3737
- [⚙️ Development Workflow](#️-development-workflow)
3838
- [Install dependencies](#install-dependencies)
39-
- [Tests and Linter](#tests-and-linter)
39+
- [Tests](#tests)
40+
- [Linter](#linter)
41+
- [Want to debug?](#want-to-debug)
4042
- [Release](#release)
4143

4244
## 🔧 Installation
@@ -126,7 +128,7 @@ You can check out [the API documentation](https://docs.meilisearch.com/reference
126128
# Create an index
127129
client.create_index('books')
128130
# Create an index and give the primary-key
129-
client.create_index(uid: 'books', primaryKey: 'book_id')
131+
client.create_index('books', primaryKey: 'book_id')
130132
```
131133

132134
#### List all indexes <!-- omit in toc -->
@@ -252,20 +254,59 @@ Thank you for your interest in a MeiliSearch tool! ♥️
252254
$ bundle install
253255
```
254256

255-
### Tests and Linter
257+
### Tests
256258

257-
Each PR should pass the tests and the linter to be accepted.
259+
Each PR should pass the tests to be accepted.
258260

259261
```bash
260262
# Tests
261263
$ docker run -p 7700:7700 getmeili/meilisearch:latest ./meilisearch --master-key=masterKey --no-analytics
262264
$ bundle exec rspec
263-
# Linter
265+
```
266+
267+
To launch a specific folder or file:
268+
269+
```bash
270+
$ bundle exec rspec spec/meilisearch/index/base_spec.rb
271+
```
272+
273+
To launch a single test in a specific file:
274+
275+
```bash
276+
$ bundle exec rspec spec/meilisearch/index/search_spec.rb -e 'does a basic search in index'
277+
```
278+
279+
### Linter
280+
281+
Each PR should pass the linter to be accepted.
282+
283+
```bash
284+
# Check the linter error
264285
$ bundle exec rubocop lib/ spec/
265-
# Linter with auto-correct
286+
# Auto-correct
266287
$ bundle exec rubocop -a lib/ spec/
267288
```
268289

290+
Once you think the remaining linter errors as valid, do not add any `rubocop` comment line in the code.<br>
291+
This project uses a `rubocop_todo.yml` file that is generated. Do not modify this file manually.<br>
292+
To update it, run the following command:
293+
294+
```bash
295+
$ bundle exec rubocop --auto-gen-config
296+
```
297+
298+
### Want to debug?
299+
300+
You can use the [`byebug` gem](https://github.com/deivid-rodriguez/byebug) that is already imported in the all files of this package.
301+
302+
To create a breakpoint, just add this line in you code:
303+
304+
```ruby
305+
...
306+
byebug
307+
...
308+
```
309+
269310
### Release
270311

271312
MeiliSearch tools follow the [Semantic Versioning Convention](https://semver.org/).

lib/meilisearch/client.rb

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,10 @@ def show_index(index_uid)
1515
end
1616

1717
# Usage:
18-
# create_index('indexUID')
19-
# create_index(uid: 'indexUID')
20-
# create_index(uid: 'indexUID', primaryKey: 'id')
21-
def create_index(attributes)
22-
body = if attributes.is_a?(Hash)
23-
attributes
24-
else
25-
{ uid: attributes }
26-
end
18+
# client.create_index('indexUID')
19+
# client.create_index('indexUID', primaryKey: 'id')
20+
def create_index(index_uid, options = {})
21+
body = { uid: index_uid }.merge(options)
2722
res = http_post '/indexes', body
2823
index_object(res['uid'])
2924
end
@@ -33,13 +28,11 @@ def delete_index(index_uid)
3328
end
3429

3530
# Usage:
36-
# index('indexUID')
37-
# index(uid: 'indexUID')
38-
def index(attribute)
39-
uid = attribute.is_a?(Hash) ? attribute[:uid] : attribute
40-
raise IndexUidError if uid.nil?
31+
# client.index('indexUID')
32+
def index(index_uid)
33+
raise IndexUidError if index_uid.nil?
4134

42-
index_object(uid)
35+
index_object(index_uid)
4336
end
4437
alias get_index index
4538

lib/meilisearch/http_request.rb

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,40 @@ def initialize(url, api_key = nil)
1616
}.compact
1717
end
1818

19-
def http_get(path = '', query = {})
19+
def http_get(path = '', query_params = {})
2020
response = self.class.get(
2121
@base_url + path,
22-
query: query,
22+
query: query_params,
2323
headers: @headers,
2424
timeout: 1
2525
)
2626
validate(response)
2727
end
2828

29-
def http_post(path = '', body = nil, params = nil)
29+
def http_post(path = '', body = nil, query_params = nil)
3030
body = body.to_json unless body.nil?
31-
params = {
32-
body: body,
33-
query: params,
34-
headers: @headers,
35-
timeout: 1
36-
}.compact
3731
response = self.class.post(
3832
@base_url + path,
39-
params
33+
{
34+
body: body,
35+
query: query_params,
36+
headers: @headers,
37+
timeout: 1
38+
}.compact
4039
)
4140
validate(response)
4241
end
4342

44-
def http_put(path = '', body = nil, params = nil)
43+
def http_put(path = '', body = nil, query_params = nil)
4544
body = body.to_json unless body.nil?
4645
response = self.class.put(
4746
@base_url + path,
48-
body: body,
49-
query: params,
50-
headers: @headers,
51-
timeout: 1
47+
{
48+
body: body,
49+
query: query_params,
50+
headers: @headers,
51+
timeout: 1
52+
}.compact
5253
)
5354
validate(response)
5455
end

lib/meilisearch/index.rb

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ def documents(options = {})
4848

4949
def add_documents(documents, primary_key = nil)
5050
documents = [documents] if documents.is_a?(Hash)
51-
http_post "/indexes/#{@uid}/documents", documents, primaryKey: primary_key
51+
http_post "/indexes/#{@uid}/documents", documents, { primaryKey: primary_key }.compact
5252
end
5353
alias replace_documents add_documents
5454
alias add_or_replace_documents add_documents
5555

5656
def update_documents(documents, primary_key = nil)
5757
documents = [documents] if documents.is_a?(Hash)
58-
http_put "/indexes/#{@uid}/documents", documents, primaryKey: primary_key
58+
http_put "/indexes/#{@uid}/documents", documents, { primaryKey: primary_key }.compact
5959
end
6060
alias add_or_update_documents update_documents
6161

@@ -81,7 +81,16 @@ def delete_all_documents
8181
### SEARCH
8282

8383
def search(query, options = {})
84-
http_get "/indexes/#{@uid}/search", { q: query }.merge(options)
84+
parsed_options = options.transform_keys(&:to_sym).map do |k, v|
85+
if [:facetFilters, :facetsDistribution].include?(k)
86+
[k, v.inspect]
87+
elsif v.is_a?(Array)
88+
[k, v.join(',')]
89+
else
90+
[k, v]
91+
end
92+
end.to_h
93+
http_get "/indexes/#{@uid}/search", { q: query }.merge(parsed_options)
8594
end
8695

8796
### UPDATES
@@ -125,8 +134,8 @@ def last_update
125134
stats['lastUpdate']
126135
end
127136

128-
def fields_frequency
129-
stats['fieldsFrequency']
137+
def fields_distribution
138+
stats['fieldsDistribution']
130139
end
131140

132141
### SETTINGS - GENERAL
@@ -245,5 +254,20 @@ def accept_new_fields
245254
def update_accept_new_fields(accept_new_fields)
246255
http_post "/indexes/#{@uid}/settings/accept-new-fields", accept_new_fields
247256
end
257+
258+
### SETTINGS - ATTRIBUTES FOR FACETING
259+
260+
def attributes_for_faceting
261+
http_get "/indexes/#{@uid}/settings/attributes-for-faceting"
262+
end
263+
alias get_attributes_for_faceting attributes_for_faceting
264+
265+
def update_attributes_for_faceting(attributes_for_faceting)
266+
http_post "/indexes/#{@uid}/settings/attributes-for-faceting", attributes_for_faceting
267+
end
268+
269+
def reset_attributes_for_faceting
270+
http_delete "/indexes/#{@uid}/settings/attributes-for-faceting"
271+
end
248272
end
249273
end

lib/meilisearch/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module MeiliSearch
4-
VERSION = '0.10.0'
4+
VERSION = '0.11.0'
55
end

meilisearch.gemspec

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,7 @@ Gem::Specification.new do |s|
1919
'lib/meilisearch/http_request.rb',
2020
'lib/meilisearch/client.rb',
2121
'lib/meilisearch/index.rb',
22-
'lib/meilisearch/version.rb',
23-
'lib/meilisearch/client/health.rb',
24-
'lib/meilisearch/client/stats.rb',
25-
'lib/meilisearch/client/keys.rb',
26-
'lib/meilisearch/client/indexes.rb',
27-
'lib/meilisearch/index/base.rb',
28-
'lib/meilisearch/index/search.rb',
29-
'lib/meilisearch/index/documents.rb',
30-
'lib/meilisearch/index/stats.rb',
31-
'lib/meilisearch/index/updates.rb',
32-
'lib/meilisearch/index/stop_words.rb',
33-
'lib/meilisearch/index/synonyms.rb',
34-
'lib/meilisearch/index/settings.rb'
22+
'lib/meilisearch/version.rb'
3523
]
3624

3725
s.add_dependency 'httparty', '>= 0.17.1', '< 0.19.0'

spec/meilisearch/client/health_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
RSpec.describe MeiliSearch::Client do
3+
RSpec.describe 'MeiliSearch::Client - Health' do
44
let(:client) { MeiliSearch::Client.new($URL, $MASTER_KEY) }
55
let(:wrong_client) { MeiliSearch::Client.new('bad_url') }
66

spec/meilisearch/client/indexes_spec.rb

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
# frozen_string_literal: true
22

3-
RSpec.describe MeiliSearch::Client do
3+
RSpec.describe 'MeiliSearch::Client - Indexes' do
44
before(:all) do
55
@client = MeiliSearch::Client.new($URL, $MASTER_KEY)
66
clear_all_indexes(@client)
77
@uid1 = 'uid1'
88
@uid2 = 'uid2'
9-
@uid3 = 'uid3'
109
@primary_key = 'objectId'
1110
end
1211

@@ -17,17 +16,10 @@
1716
expect(index.primary_key).to be_nil
1817
end
1918

20-
it 'creates an index without primary-key as an Hash' do
21-
index = @client.create_index(uid: @uid2)
22-
expect(index).to be_a(MeiliSearch::Index)
23-
expect(index.uid).to eq(@uid2)
24-
expect(index.primary_key).to be_nil
25-
end
26-
2719
it 'creates an index with primary-key' do
28-
index = @client.create_index(uid: @uid3, primaryKey: @primary_key)
20+
index = @client.create_index(@uid2, primaryKey: @primary_key)
2921
expect(index).to be_a(MeiliSearch::Index)
30-
expect(index.uid).to eq(@uid3)
22+
expect(index.uid).to eq(@uid2)
3123
expect(index.primary_key).to eq(@primary_key)
3224
end
3325

@@ -46,22 +38,22 @@
4638
it 'gets list of indexes' do
4739
response = @client.indexes
4840
expect(response).to be_a(Array)
49-
expect(response.count).to eq(3)
41+
expect(response.count).to eq(2)
5042
uids = response.map { |elem| elem['uid'] }
51-
expect(uids).to contain_exactly(@uid1, @uid2, @uid3)
43+
expect(uids).to contain_exactly(@uid1, @uid2)
5244
end
5345

5446
it 'shows a specific index' do
55-
response = @client.show_index(@uid3)
47+
response = @client.show_index(@uid2)
5648
expect(response).to be_a(Hash)
57-
expect(response['uid']).to eq(@uid3)
49+
expect(response['uid']).to eq(@uid2)
5850
expect(response['primaryKey']).to eq(@primary_key)
5951
end
6052

6153
it 'returns an index object based on uid' do
62-
index = @client.index(@uid3)
54+
index = @client.index(@uid2)
6355
expect(index).to be_a(MeiliSearch::Index)
64-
expect(index.uid).to eq(@uid3)
56+
expect(index.uid).to eq(@uid2)
6557
expect(index.primary_key).to eq(@primary_key)
6658
end
6759

@@ -70,8 +62,6 @@
7062
expect { @client.show_index(@uid1) }.to raise_meilisearch_http_error_with(404)
7163
expect(@client.delete_index(@uid2)).to be_nil
7264
expect { @client.show_index(@uid2) }.to raise_meilisearch_http_error_with(404)
73-
expect(@client.delete_index(@uid3)).to be_nil
74-
expect { @client.show_index(@uid3) }.to raise_meilisearch_http_error_with(404)
7565
expect(@client.indexes.count).to eq(0)
7666
end
7767

0 commit comments

Comments
 (0)