---
title: "Search within facet values in C#"
url: https://recursiveai.org/oneindex/cookbook/search-facet-values/
description: "Filter a long list of facet values as a user types in C#. Use OneIndex to find matching categories and count them within the current search filters."
version: "0.4.0-preview.2"
---

# Search within facet values in C#

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

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneIndex) · [GitHub](https://github.com/recursive-ai-oss/oneindex-csharp) · [Docs](https://recursiveai.org/oneindex/docs/) · [Download example (.zip)](https://recursiveai.org/oneindex/cookbook/search-facet-values/search-facet-values.zip)

Use `FacetValues` to find category labels like “Trail shoes” when someone types “sho”.

```shell
dotnet add package RecursiveAI.OneIndex --prerelease
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using OneIndex;

using var index =
    SearchIndex.InMemory<Item>();
Seed(index);
foreach (var value in index.Search()
    .Where(x => x.Section == "outdoor")
    .FacetValues(x => x.Category, "sho"))
    Console.WriteLine(
        $"{value.Value}: {value.Count}");

static void Seed(SearchIndex<Item> index)
{
    index.Upsert([
        new("1", "Trail shoes", "outdoor"),
        new("2", "Road shoes", "outdoor"),
        new("3", "Dress shoes", "formal"),
        new("4", "Trail boots", "outdoor")
    ]);
    index.Commit();
}

public class Item(
    string id,
    string category,
    string section)
{
    public Item() : this("", "", "") { }

    [SearchId]
    public string Id { get; set; } = id;
    [Keyword, Facet]
    public string Category { get; set; } = category;
    [Keyword, Filterable]
    public string Section { get; set; } = section;
}
```

Unzip [the example](https://recursiveai.org/oneindex/cookbook/search-facet-values/search-facet-values.zip), then `dotnet run`.

```text
Road shoes: 1
Trail shoes: 1
```

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

Mark the category with `[Keyword, Facet]` so OneIndex can look up the whole label. A property marked only `[FullText]` throws `ArgumentException`.

## Variations

Pass `limit` to cap the list of suggestions. In the example, replace the `foreach` loop with this one to return only the first matching category:

```csharp
foreach (var value in index.Search()
    .Where(x => x.Section == "outdoor")
    .FacetValues(x => x.Category, "sho", limit: 1))
    Console.WriteLine(
        $"{value.Value}: {value.Count}");
```

```text
Road shoes: 1
```

## Related recipes

[Faceted search in C#](https://recursiveai.org/oneindex/cookbook/faceted-search/) · [Typo-tolerant search](https://recursiveai.org/oneindex/cookbook/typo-tolerant-search/)

---

## The short answer

Call `FacetValues(x => x.Category, "sho")` on the OneIndex query that powers your results. The string is searched against the category labels, not the product titles. The example applies the `outdoor` filter first, so “Dress shoes” is absent. “Trail boots” is also absent: it is outdoors, but none of its words matches “sho”. The remaining labels each have one matching product.

## What does facet search mean?

A facet is a set of field values and their counts. A shoe catalog might have one facet for brand and another for category. Facet value search is the search box *inside* one of those facets. Users can type a few letters to find a label in a long list instead of scrolling for it. It’s a separate operation from searching for a product by name.

`FacetValues` searches the whole matching value list, then applies `limit`. It doesn’t just search the values visible in a menu you already rendered. The default limit is 100, and the default sorts labels by value (ordinal and case-sensitive). Set `sortBy` to `OneIndex.Aggregations.FacetSort.Count` to put the highest counts first, with ties broken by value. These orders apply to the suggestions, not the hits. See [facet value search](https://recursiveai.org/oneindex/docs/facets/#facet-value-search).

Matching is case- and accent-insensitive. A typed word can match the start of any word in the label, which is why “sho” finds “Trail shoes” even though the label starts with “Trail”. With multiple input words, each must match a word in the label. A word of up to four code points allows no typos; from five it allows one edit, and from nine it allows two.

## What is the difference between searching and filtering?

Typing into the facet search box narrows the list of choices. Selecting a choice narrows the actual results: use a `Where` filter on the selected category for that. Keep the selected category and the text typed in the facet box as separate values in your UI.

Send the active document filters with the facet query so users only see choices that occur in their current result set. Counts include all matching documents, not just the visible page. If the active filters leave no matching label, `FacetValues` returns an empty list. If you want shoppers to switch categories without first clearing their current one, request suggestions without the current category filter while keeping the other filters in place.
