Use FacetValues to find category labels like “Trail shoes” when someone types “sho”.
dotnet add package RecursiveAI.OneIndex --prereleaseRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
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, then dotnet run.
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:
foreach (var value in index.Search() .Where(x => x.Section == "outdoor") .FacetValues(x => x.Category, "sho", limit: 1)) Console.WriteLine( $"{value.Value}: {value.Count}");
Road shoes: 1Related recipes
Faceted search in C# · 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.
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.