Skip to content

Commit

Permalink
Merge #479
Browse files Browse the repository at this point in the history
479: Add V1.3 Define fields to search on at search-time r=brunoocasali a=davidpan

# Pull Request

## Related issue
Fixes #458 

## What does this PR do?
- Add the ability to receive a new param in the search method called attributesToSearchOn.

## PR checklist
Please check if your PR fulfills the following requirements:
- [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)?
- [x] Have you read the contributing guidelines?
- [x] Have you made sure that the title is accurate and descriptive of the changes?

Thank you so much for contributing to Meilisearch!


Co-authored-by: David Pan <12668+davidpan@users.noreply.github.com>
  • Loading branch information
meili-bors[bot] and davidpan authored Sep 1, 2023
2 parents 9dbae8b + 2f15d04 commit 7be1ef4
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,11 @@ search_parameter_guide_show_ranking_score_1: |-
client.index('movies').search('winter feast', {
show_ranking_score: true
})
search_parameter_guide_attributes_to_search_on_1: |-
client.index('movies').search('adventure', {
attributes_to_search_on: ['overview']
})
add_movies_json_1: |-
require 'json'
Expand Down
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,34 @@ You can enable it by querying PATCH /experimental-features with { "scoreDetails"

This feature is only available with Meilisearch v1.3 and newer (optional).

#### Custom Search With attributes on at search time <!-- omit in toc -->

[Customize attributes to search on at search time](https://www.meilisearch.com/docs/reference/api/search#customize-attributes-to-search-on-at-search-time).

you can perform the search :

```ruby
index.search('wonder', { attributes_to_search_on: ['genres'] })
```


JSON output:

```json
{
"hits":[],
"query":"wonder",
"processingTimeMs":0,
"limit":20,
"offset":0,
"estimatedTotalHits":0,
"nbHits":0
}
```

This feature is only available with Meilisearch v1.3 and newer (optional).


## 🤖 Compatibility with Meilisearch

This package guarantees compatibility with [version v1.x of Meilisearch](https://github.com/meilisearch/meilisearch/releases/latest), but some features may not be present. Please check the [issues](https://github.com/meilisearch/meilisearch-ruby/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22+label%3Aenhancement) for more info.
Expand Down
1 change: 1 addition & 0 deletions lib/meilisearch/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ def delete_all_documents!

# options: A Hash
# show_ranking_score - To see the ranking scores for returned documents
# attributes_to_search_on - Customize attributes to search on at search time.
def search(query, options = {})
attributes = { q: query.to_s }.merge(options.compact)

Expand Down
37 changes: 37 additions & 0 deletions spec/meilisearch/index/search/q_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,41 @@
expect(response.keys).to contain_exactly(*FINITE_PAGINATED_SEARCH_RESPONSE_KEYS)
end
end

context 'with attributes_to_search_on params' do
it 'responds with empty attributes_to_search_on' do
response = index.search('prince', { attributes_to_search_on: [] })
expect(response).to be_a(Hash)
expect(response.keys).to contain_exactly(*DEFAULT_SEARCH_RESPONSE_KEYS)
expect(response['hits']).to be_empty
end

it 'responds with nil attributes_to_search_on' do
response = index.search('prince', { attributes_to_search_on: nil })
expect(response).to be_a(Hash)
expect(response.keys).to contain_exactly(*DEFAULT_SEARCH_RESPONSE_KEYS)
expect(response['hits']).not_to be_empty
end

it 'responds with title attributes_to_search_on' do
response = index.search('prince', { attributes_to_search_on: ['title'] })
expect(response).to be_a(Hash)
expect(response.keys).to contain_exactly(*DEFAULT_SEARCH_RESPONSE_KEYS)
expect(response['hits']).not_to be_empty
end

it 'responds with genre attributes_to_search_on' do
response = index.search('prince', { attributes_to_search_on: ['genry'] })
expect(response).to be_a(Hash)
expect(response.keys).to contain_exactly(*DEFAULT_SEARCH_RESPONSE_KEYS)
expect(response['hits']).to be_empty
end

it 'responds with nil attributes_to_search_on and empty query' do
response = index.search('', { attributes_to_search_on: nil })
expect(response).to be_a(Hash)
expect(response.keys).to contain_exactly(*DEFAULT_SEARCH_RESPONSE_KEYS)
expect(response['hits'].count).to eq(documents.count)
end
end
end

0 comments on commit 7be1ef4

Please sign in to comment.