Skip to content

Commit 5e2cae2

Browse files
committed
Merge branch 'main' into add-docker-composer-for-local-development
2 parents add86f5 + 24efc88 commit 5e2cae2

File tree

7 files changed

+83
-19
lines changed

7 files changed

+83
-19
lines changed

.code-samples.meilisearch.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,11 @@ update_ranking_rules_1: |-
123123
reset_ranking_rules_1: |-
124124
client.index('movies').reset_ranking_rules
125125
get_distinct_attribute_1: |-
126-
client.index('movies').distinct_attribute
126+
client.index('shoes').distinct_attribute
127127
update_distinct_attribute_1: |-
128-
client.index('movies').update_distinct_attribute('movie_id')
128+
client.index('shoes').update_distinct_attribute('skuid')
129129
reset_distinct_attribute_1: |-
130-
client.index('movies').reset_distinct_attribute
130+
client.index('shoes').reset_distinct_attribute
131131
get_searchable_attributes_1: |-
132132
client.index('movies').searchable_attributes
133133
update_searchable_attributes_1: |-

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,50 @@ JSON output:
152152
}
153153
```
154154

155+
#### Custom Search With Filters <!-- omit in toc -->
156+
157+
If you want to enable filtering, you must add your attributes to the `filterableAttributes` index setting.
158+
159+
```ruby
160+
index.update_filterable_attributes([
161+
'id',
162+
'genres'
163+
])
164+
```
165+
166+
You only need to perform this operation once.
167+
168+
Note that MeiliSearch will rebuild your index whenever you update `filterableAttributes`. Depending on the size of your dataset, this might take time. You can track the process using the [update status](https://docs.meilisearch.com/reference/api/updates.html#get-an-update-status).
169+
170+
Then, you can perform the search:
171+
172+
```ruby
173+
index.search('wonder', { filter: ['id > 1 AND genres = Action'] })
174+
```
175+
176+
JSON output:
177+
178+
```json
179+
{
180+
"hits": [
181+
{
182+
"id": 2,
183+
"title": "Wonder Woman",
184+
"genres": [
185+
"Action",
186+
"Adventure"
187+
]
188+
}
189+
],
190+
"nbHits": 1,
191+
"exhaustiveNbHits": false,
192+
"query": "wonder",
193+
"limit": 20,
194+
"offset": 0,
195+
"processingTimeMs": 0
196+
}
197+
```
198+
155199
## 🤖 Compatibility with MeiliSearch
156200

157201
This package only guarantees the compatibility with the [version v0.23.0 of MeiliSearch](https://github.com/meilisearch/MeiliSearch/releases/tag/v0.23.0).

lib/meilisearch/client.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@ module MeiliSearch
66
class Client < HTTPRequest
77
### INDEXES
88

9+
def raw_indexes
10+
http_get('/indexes')
11+
end
12+
913
def indexes
10-
http_get '/indexes'
14+
raw_indexes.map do |index_hash|
15+
index_object(index_hash['uid'], index_hash['primaryKey'])
16+
end
1117
end
1218

1319
# Usage:

lib/meilisearch/http_request.rb

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,47 +17,54 @@ def initialize(url, api_key = nil, options = {})
1717
'Content-Type' => 'application/json',
1818
'X-Meili-API-Key' => api_key
1919
}.compact
20+
@headers_no_body = {
21+
'X-Meili-API-Key' => api_key
22+
}.compact
2023
end
2124

2225
def http_get(relative_path = '', query_params = {})
2326
send_request(
2427
proc { |path, config| self.class.get(path, config) },
2528
relative_path,
26-
query_params
29+
query_params: query_params,
30+
headers: @headers_no_body
2731
)
2832
end
2933

3034
def http_post(relative_path = '', body = nil, query_params = nil)
3135
send_request(
3236
proc { |path, config| self.class.post(path, config) },
3337
relative_path,
34-
query_params,
35-
body
38+
query_params: query_params,
39+
body: body,
40+
headers: @headers
3641
)
3742
end
3843

3944
def http_put(relative_path = '', body = nil, query_params = nil)
4045
send_request(
4146
proc { |path, config| self.class.put(path, config) },
4247
relative_path,
43-
query_params,
44-
body
48+
query_params: query_params,
49+
body: body,
50+
headers: @headers
4551
)
4652
end
4753

4854
def http_delete(relative_path = '')
4955
send_request(
5056
proc { |path, config| self.class.delete(path, config) },
51-
relative_path
57+
relative_path,
58+
headers: @headers_no_body
5259
)
5360
end
5461

5562
private
5663

5764
SNAKE_CASE = /[^a-zA-Z0-9]+(.)/.freeze
5865

59-
def send_request(http_method, relative_path, query_params = nil, body = nil)
60-
config = http_config(query_params, body)
66+
def send_request(http_method, relative_path, query_params: nil, body: nil, headers: nil)
67+
config = http_config(query_params, body, headers)
6168
begin
6269
response = http_method.call(@base_url + relative_path, config)
6370
rescue Errno::ECONNREFUSED => e
@@ -66,11 +73,10 @@ def send_request(http_method, relative_path, query_params = nil, body = nil)
6673
validate(response)
6774
end
6875

69-
def http_config(query_params, body)
76+
def http_config(query_params, body, headers)
7077
body = transform_attributes(body).to_json
71-
7278
{
73-
headers: @headers,
79+
headers: headers,
7480
query: query_params,
7581
body: body,
7682
timeout: @options[:timeout] || 1,

spec/meilisearch/client/indexes_spec.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,18 @@
7979
end.to raise_meilisearch_api_error_with(400, 'invalid_index_uid', 'invalid_request_error')
8080
end
8181

82-
it 'gets list of indexes' do
82+
it 'gets list of indexes as MeiliSearch::Index' do
83+
client.create_index('index')
84+
85+
response = client.indexes.first
86+
87+
expect(response).to be_a(MeiliSearch::Index)
88+
end
89+
90+
it 'gets list of raw indexes' do
8391
['first_index', 'second_index', 'third_index'].each { |name| client.create_index(name) }
8492

85-
indexes = client.indexes
93+
indexes = client.raw_indexes
8694

8795
expect(indexes).to be_a(Array)
8896
expect(indexes.length).to eq(3)

spec/meilisearch/index/base_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
expect(MeiliSearch::Index).to receive(:get).with(
6363
"#{URL}/indexes/options",
6464
{
65-
headers: { 'Content-Type' => 'application/json', 'X-Meili-API-Key' => MASTER_KEY },
65+
headers: { 'X-Meili-API-Key' => MASTER_KEY },
6666
body: 'null',
6767
query: {},
6868
max_retries: 1,

spec/support/indexes_helpers.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module IndexesHelpers
44
def clear_all_indexes(client)
55
indexes = client.indexes
6-
uids = indexes.map { |index| index['uid'] }
6+
uids = indexes.map(&:uid)
77
uids.each do |uid|
88
client.delete_index(uid)
99
end

0 commit comments

Comments
 (0)