Skip to content

Commit

Permalink
Prepare for Meilisearch v0.11 (#39)
Browse files Browse the repository at this point in the history
* 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 <sjimenezre@gmail.com>

* Update spec/meilisearch/index/search_spec.rb

Co-authored-by: Samuel Jimenez <sjimenezre@gmail.com>

* Update spec/meilisearch/index/search_spec.rb

Co-authored-by: Samuel Jimenez <sjimenezre@gmail.com>

* Update spec/meilisearch/index/search_spec.rb

Co-authored-by: Samuel Jimenez <sjimenezre@gmail.com>

* Update spec/meilisearch/index/search_spec.rb

Co-authored-by: Samuel Jimenez <sjimenezre@gmail.com>

Co-authored-by: Samuel Jimenez <sjimenezre@gmail.com>

* 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 <sjimenezre@gmail.com>
  • Loading branch information
curquiza and eskombro committed Jun 15, 2020
1 parent 7f4cd43 commit ceaa640
Show file tree
Hide file tree
Showing 18 changed files with 287 additions and 161 deletions.
10 changes: 5 additions & 5 deletions .rubocop_todo.yml
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2020-06-03 19:41:42 +0200 using RuboCop version 0.85.0.
# on 2020-06-04 16:06:38 +0200 using RuboCop version 0.85.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand All @@ -10,17 +10,17 @@
# Configuration parameters: CountComments, ExcludedMethods.
# ExcludedMethods: refine
Metrics/BlockLength:
Max: 434
Max: 463

# Offense count: 1
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 171
Max: 190

# Offense count: 1
# Offense count: 2
# Configuration parameters: CountComments, ExcludedMethods.
Metrics/MethodLength:
Max: 12
Max: 11

# Offense count: 1
Naming/AccessorMethodName:
Expand Down
53 changes: 47 additions & 6 deletions README.md
Expand Up @@ -36,7 +36,9 @@
- [Search](#search)
- [⚙️ Development Workflow](#️-development-workflow)
- [Install dependencies](#install-dependencies)
- [Tests and Linter](#tests-and-linter)
- [Tests](#tests)
- [Linter](#linter)
- [Want to debug?](#want-to-debug)
- [Release](#release)

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

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

### Tests and Linter
### Tests

Each PR should pass the tests and the linter to be accepted.
Each PR should pass the tests to be accepted.

```bash
# Tests
$ docker run -p 7700:7700 getmeili/meilisearch:latest ./meilisearch --master-key=masterKey --no-analytics
$ bundle exec rspec
# Linter
```

To launch a specific folder or file:

```bash
$ bundle exec rspec spec/meilisearch/index/base_spec.rb
```

To launch a single test in a specific file:

```bash
$ bundle exec rspec spec/meilisearch/index/search_spec.rb -e 'does a basic search in index'
```

### Linter

Each PR should pass the linter to be accepted.

```bash
# Check the linter error
$ bundle exec rubocop lib/ spec/
# Linter with auto-correct
# Auto-correct
$ bundle exec rubocop -a lib/ spec/
```

Once you think the remaining linter errors as valid, do not add any `rubocop` comment line in the code.<br>
This project uses a `rubocop_todo.yml` file that is generated. Do not modify this file manually.<br>
To update it, run the following command:

```bash
$ bundle exec rubocop --auto-gen-config
```

### Want to debug?

You can use the [`byebug` gem](https://github.com/deivid-rodriguez/byebug) that is already imported in the all files of this package.

To create a breakpoint, just add this line in you code:

```ruby
...
byebug
...
```

### Release

MeiliSearch tools follow the [Semantic Versioning Convention](https://semver.org/).
Expand Down
23 changes: 8 additions & 15 deletions lib/meilisearch/client.rb
Expand Up @@ -15,15 +15,10 @@ def show_index(index_uid)
end

# Usage:
# create_index('indexUID')
# create_index(uid: 'indexUID')
# create_index(uid: 'indexUID', primaryKey: 'id')
def create_index(attributes)
body = if attributes.is_a?(Hash)
attributes
else
{ uid: attributes }
end
# client.create_index('indexUID')
# client.create_index('indexUID', primaryKey: 'id')
def create_index(index_uid, options = {})
body = { uid: index_uid }.merge(options)
res = http_post '/indexes', body
index_object(res['uid'])
end
Expand All @@ -33,13 +28,11 @@ def delete_index(index_uid)
end

# Usage:
# index('indexUID')
# index(uid: 'indexUID')
def index(attribute)
uid = attribute.is_a?(Hash) ? attribute[:uid] : attribute
raise IndexUidError if uid.nil?
# client.index('indexUID')
def index(index_uid)
raise IndexUidError if index_uid.nil?

index_object(uid)
index_object(index_uid)
end
alias get_index index

Expand Down
31 changes: 16 additions & 15 deletions lib/meilisearch/http_request.rb
Expand Up @@ -16,39 +16,40 @@ def initialize(url, api_key = nil)
}.compact
end

def http_get(path = '', query = {})
def http_get(path = '', query_params = {})
response = self.class.get(
@base_url + path,
query: query,
query: query_params,
headers: @headers,
timeout: 1
)
validate(response)
end

def http_post(path = '', body = nil, params = nil)
def http_post(path = '', body = nil, query_params = nil)
body = body.to_json unless body.nil?
params = {
body: body,
query: params,
headers: @headers,
timeout: 1
}.compact
response = self.class.post(
@base_url + path,
params
{
body: body,
query: query_params,
headers: @headers,
timeout: 1
}.compact
)
validate(response)
end

def http_put(path = '', body = nil, params = nil)
def http_put(path = '', body = nil, query_params = nil)
body = body.to_json unless body.nil?
response = self.class.put(
@base_url + path,
body: body,
query: params,
headers: @headers,
timeout: 1
{
body: body,
query: query_params,
headers: @headers,
timeout: 1
}.compact
)
validate(response)
end
Expand Down
34 changes: 29 additions & 5 deletions lib/meilisearch/index.rb
Expand Up @@ -48,14 +48,14 @@ def documents(options = {})

def add_documents(documents, primary_key = nil)
documents = [documents] if documents.is_a?(Hash)
http_post "/indexes/#{@uid}/documents", documents, primaryKey: primary_key
http_post "/indexes/#{@uid}/documents", documents, { primaryKey: primary_key }.compact
end
alias replace_documents add_documents
alias add_or_replace_documents add_documents

def update_documents(documents, primary_key = nil)
documents = [documents] if documents.is_a?(Hash)
http_put "/indexes/#{@uid}/documents", documents, primaryKey: primary_key
http_put "/indexes/#{@uid}/documents", documents, { primaryKey: primary_key }.compact
end
alias add_or_update_documents update_documents

Expand All @@ -81,7 +81,16 @@ def delete_all_documents
### SEARCH

def search(query, options = {})
http_get "/indexes/#{@uid}/search", { q: query }.merge(options)
parsed_options = options.transform_keys(&:to_sym).map do |k, v|
if [:facetFilters, :facetsDistribution].include?(k)
[k, v.inspect]
elsif v.is_a?(Array)
[k, v.join(',')]
else
[k, v]
end
end.to_h
http_get "/indexes/#{@uid}/search", { q: query }.merge(parsed_options)
end

### UPDATES
Expand Down Expand Up @@ -125,8 +134,8 @@ def last_update
stats['lastUpdate']
end

def fields_frequency
stats['fieldsFrequency']
def fields_distribution
stats['fieldsDistribution']
end

### SETTINGS - GENERAL
Expand Down Expand Up @@ -245,5 +254,20 @@ def accept_new_fields
def update_accept_new_fields(accept_new_fields)
http_post "/indexes/#{@uid}/settings/accept-new-fields", accept_new_fields
end

### SETTINGS - ATTRIBUTES FOR FACETING

def attributes_for_faceting
http_get "/indexes/#{@uid}/settings/attributes-for-faceting"
end
alias get_attributes_for_faceting attributes_for_faceting

def update_attributes_for_faceting(attributes_for_faceting)
http_post "/indexes/#{@uid}/settings/attributes-for-faceting", attributes_for_faceting
end

def reset_attributes_for_faceting
http_delete "/indexes/#{@uid}/settings/attributes-for-faceting"
end
end
end
2 changes: 1 addition & 1 deletion lib/meilisearch/version.rb
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module MeiliSearch
VERSION = '0.10.0'
VERSION = '0.11.0'
end
14 changes: 1 addition & 13 deletions meilisearch.gemspec
Expand Up @@ -19,19 +19,7 @@ Gem::Specification.new do |s|
'lib/meilisearch/http_request.rb',
'lib/meilisearch/client.rb',
'lib/meilisearch/index.rb',
'lib/meilisearch/version.rb',
'lib/meilisearch/client/health.rb',
'lib/meilisearch/client/stats.rb',
'lib/meilisearch/client/keys.rb',
'lib/meilisearch/client/indexes.rb',
'lib/meilisearch/index/base.rb',
'lib/meilisearch/index/search.rb',
'lib/meilisearch/index/documents.rb',
'lib/meilisearch/index/stats.rb',
'lib/meilisearch/index/updates.rb',
'lib/meilisearch/index/stop_words.rb',
'lib/meilisearch/index/synonyms.rb',
'lib/meilisearch/index/settings.rb'
'lib/meilisearch/version.rb'
]

s.add_dependency 'httparty', '>= 0.17.1', '< 0.19.0'
Expand Down
2 changes: 1 addition & 1 deletion spec/meilisearch/client/health_spec.rb
@@ -1,6 +1,6 @@
# frozen_string_literal: true

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

Expand Down
28 changes: 9 additions & 19 deletions spec/meilisearch/client/indexes_spec.rb
@@ -1,12 +1,11 @@
# frozen_string_literal: true

RSpec.describe MeiliSearch::Client do
RSpec.describe 'MeiliSearch::Client - Indexes' do
before(:all) do
@client = MeiliSearch::Client.new($URL, $MASTER_KEY)
clear_all_indexes(@client)
@uid1 = 'uid1'
@uid2 = 'uid2'
@uid3 = 'uid3'
@primary_key = 'objectId'
end

Expand All @@ -17,17 +16,10 @@
expect(index.primary_key).to be_nil
end

it 'creates an index without primary-key as an Hash' do
index = @client.create_index(uid: @uid2)
expect(index).to be_a(MeiliSearch::Index)
expect(index.uid).to eq(@uid2)
expect(index.primary_key).to be_nil
end

it 'creates an index with primary-key' do
index = @client.create_index(uid: @uid3, primaryKey: @primary_key)
index = @client.create_index(@uid2, primaryKey: @primary_key)
expect(index).to be_a(MeiliSearch::Index)
expect(index.uid).to eq(@uid3)
expect(index.uid).to eq(@uid2)
expect(index.primary_key).to eq(@primary_key)
end

Expand All @@ -46,22 +38,22 @@
it 'gets list of indexes' do
response = @client.indexes
expect(response).to be_a(Array)
expect(response.count).to eq(3)
expect(response.count).to eq(2)
uids = response.map { |elem| elem['uid'] }
expect(uids).to contain_exactly(@uid1, @uid2, @uid3)
expect(uids).to contain_exactly(@uid1, @uid2)
end

it 'shows a specific index' do
response = @client.show_index(@uid3)
response = @client.show_index(@uid2)
expect(response).to be_a(Hash)
expect(response['uid']).to eq(@uid3)
expect(response['uid']).to eq(@uid2)
expect(response['primaryKey']).to eq(@primary_key)
end

it 'returns an index object based on uid' do
index = @client.index(@uid3)
index = @client.index(@uid2)
expect(index).to be_a(MeiliSearch::Index)
expect(index.uid).to eq(@uid3)
expect(index.uid).to eq(@uid2)
expect(index.primary_key).to eq(@primary_key)
end

Expand All @@ -70,8 +62,6 @@
expect { @client.show_index(@uid1) }.to raise_meilisearch_http_error_with(404)
expect(@client.delete_index(@uid2)).to be_nil
expect { @client.show_index(@uid2) }.to raise_meilisearch_http_error_with(404)
expect(@client.delete_index(@uid3)).to be_nil
expect { @client.show_index(@uid3) }.to raise_meilisearch_http_error_with(404)
expect(@client.indexes.count).to eq(0)
end

Expand Down

0 comments on commit ceaa640

Please sign in to comment.