Skip to content

Commit

Permalink
Add nested fields support (#260)
Browse files Browse the repository at this point in the history
* Add a new test class called Movie with nested data

* Add support to nested fields
  • Loading branch information
brunoocasali authored May 16, 2022
1 parent 5c069ef commit 1b25889
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 0 deletions.
60 changes: 60 additions & 0 deletions tests/Meilisearch.Tests/IndexFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,66 @@ public async Task<Index> SetUpIndexForFaceting(string indexUid)
return index;
}

public async Task<Index> SetUpIndexForNestedSearch(string indexUid)
{
var index = DefaultClient.Index(indexUid);
var movies = new[]
{
new MovieWithInfo
{
Id = "10",
Name = "Gladiator",
Info = new MovieInfo { Comment = "a movie about old times", ReviewNb = 700 }
},
new MovieWithInfo
{
Id = "11",
Name = "Interstellar",
Info = new MovieInfo { Comment = "the best movie", ReviewNb = 1000 }
},
new MovieWithInfo
{
Id = "12",
Name = "Star Wars",
Info = new MovieInfo { Comment = "a lot of wars in the stars", ReviewNb = 900 }
},
new MovieWithInfo
{
Id = "13",
Name = "Harry Potter",
Info = new MovieInfo { Comment = "a movie about a wizard boy", ReviewNb = 900 }
},
new MovieWithInfo
{
Id = "14",
Name = "Iron Man",
Info = new MovieInfo { Comment = "a movie about a rich man", ReviewNb = 800 }
},
new MovieWithInfo
{
Id = "15",
Name = "Spider-Man",
Info = new MovieInfo { Comment = "the spider bit the boy", ReviewNb = 900 }
},
new MovieWithInfo
{
Id = "16",
Name = "Amélie Poulain",
Info = new MovieInfo { Comment = "talks about hapiness", ReviewNb = 800 }
},
};
var task = await index.AddDocumentsAsync(movies);

// Check the documents have been added
var finishedTask = await index.WaitForTaskAsync(task.Uid);
if (finishedTask.Status != "succeeded")
{
throw new Exception("The documents were not added during SetUpIndexForNestedSearch. Impossible to run the tests.");
}

return index;
}

public async Task DeleteAllIndexes()
{
var indexes = await DefaultClient.GetAllIndexesAsync();
Expand Down
17 changes: 17 additions & 0 deletions tests/Meilisearch.Tests/Movie.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,23 @@ public class Movie
public string Genre { get; set; }
}

public class MovieInfo
{
public string Comment { get; set; }

public int ReviewNb { get; set; }
}

public class MovieWithInfo
{
public string Id { get; set; }

public string Name { get; set; }

public MovieInfo Info { get; set; }
}


public class MovieWithIntId
{
public int Id { get; set; }
Expand Down
44 changes: 44 additions & 0 deletions tests/Meilisearch.Tests/SearchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Meilisearch.Tests
public abstract class SearchTests<TFixture> : IAsyncLifetime where TFixture : IndexFixture
{
private Index _basicIndex;
private Index _nestedIndex;
private Index _indexForFaceting;
private Index _indexWithIntId;

Expand All @@ -26,6 +27,7 @@ public async Task InitializeAsync()
_basicIndex = await _fixture.SetUpBasicIndex("BasicIndex-SearchTests");
_indexForFaceting = await _fixture.SetUpIndexForFaceting("IndexForFaceting-SearchTests");
_indexWithIntId = await _fixture.SetUpBasicIndexWithIntId("IndexWithIntId-SearchTests");
_nestedIndex = await _fixture.SetUpIndexForNestedSearch("IndexForNestedDocs-SearchTests");
}

public Task DisposeAsync() => Task.CompletedTask;
Expand Down Expand Up @@ -352,5 +354,47 @@ public async Task CustomSearchWithCustomHighlightTags()
Assert.NotEmpty(movies.Hits);
Assert.Equal("Iron <mark>Man</mark>", movies.Hits.First()._Formatted.Name);
}

[Fact]
public async Task CustomSearchWithinNestedDocuments()
{
var movies = await _nestedIndex.SearchAsync<MovieWithInfo>("wizard");

Assert.NotEmpty(movies.Hits);
Assert.Equal("Harry Potter", movies.Hits.First().Name);
Assert.Equal("13", movies.Hits.First().Id);
Assert.Equal("a movie about a wizard boy", movies.Hits.First().Info.Comment);
}

[Fact]
public async Task CustomSearchWithinNestedDocumentsWithSearchableAttributesSettings()
{
var task = await _nestedIndex.UpdateSearchableAttributesAsync(new string[] { "name", "info.comment" });
await _nestedIndex.WaitForTaskAsync(task.Uid);

var movies = await _nestedIndex.SearchAsync<MovieWithInfo>("rich");

Assert.NotEmpty(movies.Hits);
Assert.Equal("Iron Man", movies.Hits.First().Name);
Assert.Equal("14", movies.Hits.First().Id);
Assert.Equal("a movie about a rich man", movies.Hits.First().Info.Comment);
}

[Fact]
public async Task CustomSearchWithinNestedDocumentsWithSearchableAndSortableAttributesSettings()
{
var searchTask = await _nestedIndex.UpdateSearchableAttributesAsync(new string[] { "name", "info.comment" });
await _nestedIndex.WaitForTaskAsync(searchTask.Uid);
var sortTask = await _nestedIndex.UpdateSortableAttributesAsync(new string[] { "info.reviewNb" });
await _nestedIndex.WaitForTaskAsync(sortTask.Uid);

var query = new SearchQuery { Sort = new string[] { "info.reviewNb:desc" } };
var movies = await _nestedIndex.SearchAsync<MovieWithInfo>("", query);

Assert.NotEmpty(movies.Hits);
Assert.Equal("Interstellar", movies.Hits.First().Name);
Assert.Equal("11", movies.Hits.First().Id);
Assert.Equal(1000, movies.Hits.First().Info.ReviewNb);
}
}
}

0 comments on commit 1b25889

Please sign in to comment.