Skip to content

Commit

Permalink
Add support to new search params crop and highlight tags (#258)
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoocasali authored May 16, 2022
1 parent c29b239 commit b2bae50
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/Meilisearch/SearchQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,24 @@ public class SearchQuery
[JsonPropertyName("attributesToHighlight")]
public IEnumerable<string> AttributesToHighlight { get; set; }

/// <summary>
/// Gets or sets the crop marker to apply before and/or after cropped part selected within an attribute defined in `attributesToCrop` parameter.
/// </summary>
[JsonPropertyName("cropMarker")]
public string CropMarker { get; set; }

/// <summary>
/// Gets or sets the tag to put before the highlighted query terms.
/// </summary>
[JsonPropertyName("highlightPreTag")]
public string HighlightPreTag { get; set; }

/// <summary>
/// Gets or sets the tag to put after the highlighted query terms.
/// </summary>
[JsonPropertyName("highlightPostTag")]
public string HighlightPostTag { get; set; }

/// <summary>
/// Gets or sets the facets distribution for the query.
/// </summary>
Expand Down
41 changes: 41 additions & 0 deletions tests/Meilisearch.Tests/SearchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -311,5 +311,46 @@ public async Task CustomSearchWithSort()
Assert.Equal(2, movies.Hits.Count());
Assert.Equal("14", movies.Hits.First().Id);
}

[Fact]
public async Task CustomSearchWithCroppingParameters()
{
var movies = await _basicIndex.SearchAsync<FormattedMovie>(
"man",
new SearchQuery { CropLength = 1, AttributesToCrop = new string[] { "*" } }
);

Assert.NotEmpty(movies.Hits);
Assert.Equal("…Man", movies.Hits.First()._Formatted.Name);
}

[Fact]
public async Task CustomSearchWithCropMarker()
{
var movies = await _basicIndex.SearchAsync<FormattedMovie>(
"man",
new SearchQuery { CropLength = 1, AttributesToCrop = new string[] { "*" }, CropMarker = "[…] " }
);

Assert.NotEmpty(movies.Hits);
Assert.Equal("[…] Man", movies.Hits.First()._Formatted.Name);
}

[Fact]
public async Task CustomSearchWithCustomHighlightTags()
{
var movies = await _basicIndex.SearchAsync<FormattedMovie>(
"man",
new SearchQuery
{
AttributesToHighlight = new string[] { "*" },
HighlightPreTag = "<mark>",
HighlightPostTag = "</mark>"
}
);

Assert.NotEmpty(movies.Hits);
Assert.Equal("Iron <mark>Man</mark>", movies.Hits.First()._Formatted.Name);
}
}
}

0 comments on commit b2bae50

Please sign in to comment.