Skip to content

Commit

Permalink
Merge #342
Browse files Browse the repository at this point in the history
342: added attributesToSearchOn r=brunoocasali a=ahmednfwela

# Pull Request

## Related issue
Fixes #336 

## What does this PR do?
- Adds `attributesToSearchOn` to `SearchQuery` and  `IndexSearchQuery`
- Added `search_parameter_guide_attributes_to_search_on_1` to code sample
- Added tests

## 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: Ahmed Fwela <[email protected]>
  • Loading branch information
meili-bors[bot] and ahmednfwela authored Aug 14, 2023
2 parents 533cc8c + 5adec2b commit 2b46429
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# the documentation on build
# You can read more on https://github.com/meilisearch/documentation/tree/master/.vuepress/code-samples
---
search_parameter_guide_attributes_to_search_on_1: |-
await client.index('movies').search('adventure', SearchQuery(attributesToSearchOn: ['overview']));
get_documents_post_1: |-
await client.index('movies').getDocuments(
params: DocumentsQuery(
Expand Down
3 changes: 3 additions & 0 deletions lib/src/query_parameters/index_search_query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class IndexSearchQuery extends SearchQuery {
super.highlightPreTag,
super.highlightPostTag,
super.matchingStrategy,
super.attributesToSearchOn,
});

@override
Expand Down Expand Up @@ -60,6 +61,7 @@ class IndexSearchQuery extends SearchQuery {
String? highlightPreTag,
String? highlightPostTag,
MatchingStrategy? matchingStrategy,
List<String>? attributesToSearchOn,
}) =>
IndexSearchQuery(
query: query ?? this.query,
Expand All @@ -82,5 +84,6 @@ class IndexSearchQuery extends SearchQuery {
highlightPreTag: highlightPreTag ?? this.highlightPreTag,
highlightPostTag: highlightPostTag ?? this.highlightPostTag,
matchingStrategy: matchingStrategy ?? this.matchingStrategy,
attributesToSearchOn: attributesToSearchOn ?? this.attributesToSearchOn,
);
}
5 changes: 5 additions & 0 deletions lib/src/query_parameters/search_query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class SearchQuery extends Queryable {
final String? highlightPreTag;
final String? highlightPostTag;
final MatchingStrategy? matchingStrategy;
final List<String>? attributesToSearchOn;

const SearchQuery({
this.offset,
Expand All @@ -38,6 +39,7 @@ class SearchQuery extends Queryable {
this.highlightPreTag,
this.highlightPostTag,
this.matchingStrategy,
this.attributesToSearchOn,
});

@override
Expand All @@ -59,6 +61,7 @@ class SearchQuery extends Queryable {
'highlightPreTag': highlightPreTag,
'highlightPostTag': highlightPostTag,
'matchingStrategy': matchingStrategy?.name,
'attributesToSearchOn': attributesToSearchOn,
};
}

Expand All @@ -80,6 +83,7 @@ class SearchQuery extends Queryable {
String? highlightPreTag,
String? highlightPostTag,
MatchingStrategy? matchingStrategy,
List<String>? attributesToSearchOn,
}) =>
SearchQuery(
offset: offset ?? this.offset,
Expand All @@ -100,5 +104,6 @@ class SearchQuery extends Queryable {
highlightPreTag: highlightPreTag ?? this.highlightPreTag,
highlightPostTag: highlightPostTag ?? this.highlightPostTag,
matchingStrategy: matchingStrategy ?? this.matchingStrategy,
attributesToSearchOn: attributesToSearchOn ?? this.attributesToSearchOn,
);
}
32 changes: 32 additions & 0 deletions test/search_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,38 @@ void main() {
},
});
});
group('attributesToSearchOn', () {
setUp(() async {
await index.updateSearchableAttributes(
['title', 'info.comment'],
).waitFor(client: client);
});

test('empty result', () async {
var response = await index.search(
'An awesome',
SearchQuery(attributesToSearchOn: ['title']),
);

expect(response.hits, isEmpty);
});

test('non-empty result', () async {
var response = await index.search(
'An awesome',
SearchQuery(attributesToSearchOn: ['info.comment']),
);

expect(response.hits[0], {
"id": 5,
"title": 'The Hobbit',
"info": {
"comment": 'An awesome book',
"reviewNb": 900,
},
});
});
});

test('searches on nested content with content with sort', () async {
await index
Expand Down

0 comments on commit 2b46429

Please sign in to comment.