Recursive AI

Deduplicate search results by field in C#

OneIndex is a free, open-source search library for .NET from Recursive AI. MIT or Apache-2.0, preview, runs in-process.

Use Distinct to show only the best match from each product’s variants.

Terminal
dotnet add package RecursiveAI.OneIndex --prerelease

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using OneIndex;
 
using var index =
    SearchIndex.InMemory<Product>();
Seed(index);
var hits = index.Search("mug")
    .Distinct(x => x.Group).Execute();
foreach (var hit in hits)
    Console.WriteLine(
        $"{hit.Document.Group}: {hit.Id}");
 
static void Seed(SearchIndex<Product> index)
{
    index.Upsert([
        new("1", "mug", "camp mug"),
        new("2", "mug", "mug"),
        new("3", "cup", "travel mug")
    ]);
    index.Commit();
}
 
public class Product(
    string id,
    string group,
    string title)
{
    public Product() : this("", "", "") { }
 
    [SearchId]
    public string Id { get; set; } = id;
    [Keyword, Filterable]
    public string Group { get; set; } = group;
    [FullText]
    public string Title { get; set; } = title;
}

Unzip the example, then dotnet run.

Text
mug: 2
cup: 3

This example uses OneIndex 0.4.0-preview.2. APIs and file formats may change between previews.

Mark the group key with [Keyword, Filterable] so OneIndex can group by the whole value. A property marked only [FullText] throws ArgumentException.

Variations

To get the second page of one group per page, replace the query and print loop with:

C#
var hits = index.Search("mug")
    .Distinct(x => x.Group)
    .Skip(1).Take(1).Execute();
foreach (var hit in hits)
    Console.WriteLine(
        $"{hit.Document.Group}: {hit.Id}");
Text
cup: 3

Paginate search results · Faceted search in C#


The short answer

Index each variant with its own [SearchId] and put the shared product key in a [Keyword, Filterable] property. Add .Distinct(x => x.Group) to your OneIndex query. Instead of returning every matching variant, it picks the highest-scoring match for each group. In the example, IDs 1 and 2 both match “mug”; only ID 2 represents the mug group.

How do you remove duplicates by grouping search results?

Use a stable product key, not the displayed title. Two unrelated products can share a title, and variants of one product can have different titles. Grouping is based on the key’s value, not on how similar two titles are. A document with no value forms a group of its own, so missing keys do not all collapse into one row. If a document has multiple values in the group field, OneIndex uses its first value. A single group key per variant makes the choice explicit.

OneIndex picks the representative from matching documents. This matters if you filter by stock status or by size: a variant that fails the filter cannot win its group. A higher relevance score wins; on a tie, OneIndex prefers the document it assigned the smaller number in the current index snapshot, not the one with the smaller product ID you supplied. The groups are ranked by their representatives’ scores. See the distinct rules.

Should I use distinct or group by?

Use Distinct when each result row should show a single representative hit, with its own title and other document fields. Use a terms aggregation when you want a bucket per group and a count or a metric for that group. A representative hit tells you which variant matched best, not how many variants matched or what the whole group’s price range is.

Don’t deduplicate only after getting one page of hits. If the first ten hits belong to two groups, removing eight duplicate cards leaves a two-card page even if other groups exist deeper in the results. With Distinct, OneIndex visits all matches, chooses the group winners, and then applies Skip and Take. TotalHits becomes the exact number of groups, not the number of matching variants. Use the same query and index snapshot across pages if you want that ordering to stay stable. The query still has a Skip + Take limit of 10,000, and its memory use depends on the number of groups, not the page size.

All oneindex recipes