Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Distinct attribute at search time #548

Merged
merged 5 commits into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Meilisearch/SearchQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,11 @@ public class SearchQuery
/// </summary>
[JsonPropertyName("page")]
public int? Page { get; set; }

/// <summary>
/// Sets distinct attribute at search time.
/// </summary>
[JsonPropertyName("distinct")]
public string Distinct { get; set; }
}
}
2 changes: 2 additions & 0 deletions tests/Meilisearch.Tests/Datasets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ internal static class Datasets
public static readonly string MoviesForFacetingJsonPath = Path.Combine(BasePath, "movies_for_faceting.json");
public static readonly string MoviesWithIntIdJsonPath = Path.Combine(BasePath, "movies_with_int_id.json");
public static readonly string MoviesWithInfoJsonPath = Path.Combine(BasePath, "movies_with_info.json");

public static readonly string ProductsForDistinctJsonPath = Path.Combine(BasePath, "products_for_distinct_search.json");
}

public class DatasetSmallMovie
Expand Down
100 changes: 100 additions & 0 deletions tests/Meilisearch.Tests/Datasets/products_for_distinct_search.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
[
{
"id": 1,
"description": "Leather Jacket",
"brand": "Lee Jeans",
"product_id": "123456",
"color": "Brown"
},
{
"id": 2,
"description": "Leather Jacket",
"brand": "Lee Jeans",
"product_id": "123456",
"color": "Black"
},
{
"id": 3,
"description": "Leather Jacket",
"brand": "Lee Jeans",
"product_id": "123456",
"color": "Blue"
},
{
"id": 4,
"description": "T-Shirt",
"brand": "Nike",
"product_id": "789012",
"color": "Red"
},
{
"id": 5,
"description": "T-Shirt",
"brand": "Nike",
"product_id": "789012",
"color": "Blue"
},
{
"id": 6,
"description": "Running Shoes",
"brand": "Adidas",
"product_id": "456789",
"color": "Black"
},
{
"id": 7,
"description": "Running Shoes",
"brand": "Adidas",
"product_id": "456789",
"color": "White"
},
{
"id": 8,
"description": "Hoodie",
"brand": "Puma",
"product_id": "987654",
"color": "Gray"
},
{
"id": 9,
"description": "Sweater",
"brand": "Gap",
"product_id": "234567",
"color": "Green"
},
{
"id": 10,
"description": "Sweater",
"brand": "Gap",
"product_id": "234567",
"color": "Red"
},
{
"id": 11,
"description": "Sweater",
"brand": "Gap",
"product_id": "234567",
"color": "Blue"
},
{
"id": 12,
"description": "Jeans",
"brand": "Levi's",
"product_id": "345678",
"color": "Indigo"
},
{
"id": 13,
"description": "Jeans",
"brand": "Levi's",
"product_id": "345678",
"color": "Black"
},
{
"id": 14,
"description": "Jeans",
"brand": "Levi's",
"product_id": "345678",
"color": "Stone Wash"
}
]
32 changes: 32 additions & 0 deletions tests/Meilisearch.Tests/IndexFixture.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;

using Xunit;
Expand Down Expand Up @@ -123,6 +124,37 @@ public async Task<Index> SetUpIndexForNestedSearch(string indexUid)

return index;
}
public async Task<Index> SetUpIndexForDistinctProductsSearch(string indexUid)
{
var index = DefaultClient.Index(indexUid);
var products = await JsonFileReader.ReadAsync<List<Product>>(Datasets.ProductsForDistinctJsonPath);
var task = await index.AddDocumentsAsync(products, primaryKey: "id");
// Check the documents have been added
var finishedTask = await index.WaitForTaskAsync(task.TaskUid);
if (finishedTask.Status != TaskInfoStatus.Succeeded)
{
throw new Exception($"The documents were not added during SetUpIndexForDistinctProductsSearch.\n" +
$"Impossible to run the tests.\n" +
$"{JsonSerializer.Serialize(finishedTask.Error)}");
}

var settings = new Settings
{
FilterableAttributes = new string[] { "product_id" },
};
task = await index.UpdateSettingsAsync(settings);

// Check the settings have been added
finishedTask = await index.WaitForTaskAsync(task.TaskUid);
if (finishedTask.Status != TaskInfoStatus.Succeeded)
{
throw new Exception($"The documents were not added during SetUpIndexForDistinctProductsSearch.\n" +
$"Impossible to run the tests.\n" +
$"{JsonSerializer.Serialize(finishedTask.Error)}");
}

return index;
}

public async Task DeleteAllIndexes()
{
Expand Down
22 changes: 22 additions & 0 deletions tests/Meilisearch.Tests/Product.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Text.Json.Serialization;

namespace Meilisearch.Tests
{
public class Product
{
[JsonPropertyName("id")]
public int Id { get; set; }

[JsonPropertyName("description")]
public string Description { get; set; }

[JsonPropertyName("brand")]
public string Brand { get; set; }

[JsonPropertyName("product_id")]
public string ProductId { get; set; }

[JsonPropertyName("color")]
public string Color { get; set; }
}
}
22 changes: 22 additions & 0 deletions tests/Meilisearch.Tests/SearchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public abstract class SearchTests<TFixture> : IAsyncLifetime where TFixture : In
private Index _nestedIndex;
private Index _indexForFaceting;
private Index _indexWithIntId;
private Index _productIndexForDistinct;

private readonly TFixture _fixture;

Expand All @@ -28,6 +29,7 @@ public async Task InitializeAsync()
_indexForFaceting = await _fixture.SetUpIndexForFaceting("IndexForFaceting-SearchTests");
_indexWithIntId = await _fixture.SetUpBasicIndexWithIntId("IndexWithIntId-SearchTests");
_nestedIndex = await _fixture.SetUpIndexForNestedSearch("IndexForNestedDocs-SearchTests");
_productIndexForDistinct = await _fixture.SetUpIndexForDistinctProductsSearch("IndexForDistinctProducts-SearchTests");
}

public Task DisposeAsync() => Task.CompletedTask;
Expand Down Expand Up @@ -503,5 +505,25 @@ public async Task CustomSearchWithShowRankingScore()
var movies = await _basicIndex.SearchAsync<MovieWithRankingScore>("iron man", searchQuery);
Assert.NotNull(movies.Hits.First()._RankingScore);
}
[Fact]
public async Task CustomSearchProductsWithoutDistinct()
{
var searchQuery = new SearchQuery()
{

};
var products = await _productIndexForDistinct.SearchAsync<Product>("", searchQuery);
products.Hits.Count.Should().Be(14);
}
[Fact]
public async Task CustomSearchProductsWithDistinct()
{
var searchQuery = new SearchQuery()
{
Distinct = "product_id"
};
var products = await _productIndexForDistinct.SearchAsync<Product>("", searchQuery);
products.Hits.Count.Should().Be(6);
}
}
}
Loading