Custom Search Box Filters
This page shows how to add your own inline filters to a search box, in the style of the built-in @file and ext: filters. The running example is a project-number filter: typing proj:5678 into the box restricts the search to project 5678 before the results are ranked.
The built-in filters and how a user types them are described in Search Box Filters. This page covers adding new ones.
Where custom filters can go
| Search box | Custom filters |
|---|---|
| The product's own search boxes: the Search and Ask box on the home view, the search results page, the search command palette and a folder's search box | No. Their filter set is fixed in code (OmniBoxSearchFilters), and no setting adds to it. |
A SearchArea your application builds in its own front end |
Yes, registered in code on the area's AdvancedOmniBox. |
UI.Neighbors(...) and SimilarSearchArea |
Yes, the same way: both accept the same Action<AdvancedOmniBox>. |
So a project filter belongs on a search view your application owns, for example a "Project documents" page. If you don't have a custom front end yet, start with Custom Front-End and the SearchArea recipe.
The two kinds of filter
| Kind | Typed as | Takes a value | Registered with | Use for |
|---|---|---|---|---|
| Snap | @word |
No | RegisterSnapFilter(SnapHandler, Action<SearchRequest>) |
A fixed restriction, such as "only open tickets" |
| Value filter | word:value |
Yes | RegisterFilterSnap(FilterSnapHandler, Action<SearchRequest, string>) |
A restriction with a parameter, such as a project number |
A project number is a value, so it is a value filter, typed proj:5678. The @ prefix is reserved for snaps, which carry no value: @proj:5678 is not recognised as a filter. If one project needs a shortcut of its own, register a snap for it, for example @apollo for project 5678 (see A snap for a fixed project).
What a filter does
A filter is two things:
- A handler that tells the box about the filter: its id, the trigger words, the suggested values, an icon and a tooltip. The box turns a matching trigger into a chip.
- A callback that runs when the search is built, once per chip in the box. It narrows the
SearchRequest, usually by adding a facet withapplyBefore: trueso the restriction applies before ranking.
The chip is not part of the text query. The rest of the text in the box is searched as usual.
How the callback narrows the request depends on how the project number is modelled in your schema. Pick the matching section below.
Project number as a field
Use this when each searchable node stores the project number in a field, for example Document.ProjectNumber.
SearchArea()
.OnSearch(sr => sr.SetBeforeTypesFacet(nameof(Document)))
.SearchBox(b => b
.WithBasicSearchFilters()
.RegisterFilterSnap(
new OmniBox.FilterSnapHandler(
"project", // id: matches the chip back to the callback
"Project".t(), // name shown on the chip and in the help
new[] { "proj", "project" }, // trigger words: proj:5678, project:5678
new string[0], // suggested values (none: any value is accepted)
Icon(UIcons.Briefcase),
"Only show one project".t(),
exampleValue: "5678"),
(sr, value) => sr.SetValueFacet(
"Document.ProjectNumber",
keepOtherTypes: false,
invertedBehaviour: false,
applyBefore: true,
values: new[] { value.Trim() })))
- The facet key is
<NodeType>.<Field>. The node type is the type being searched, not a separate project type. - A value facet compares the field's string value exactly, so store the project number as a
stringand normalise the typed value the same way it was ingested (trim, case, leading zeros). keepOtherTypes: falsedrops results of every other node type. Set it totruewhen the area also searches types that have noProjectNumberfield and those should stay in the results.- Two
proj:chips in the box add two values to the same facet, which matches either project.
Configuring a property facet on the field is not required for the filter to work, but it lets the same field appear in the facet sidebar, and an index on the field speeds up the filter on large node types.
Project as a node
Use this when projects are their own node type, keyed by the project number, with an edge to each document. This is the better model when a project has its own properties (name, owner, status) or when documents move between projects, because the documents don't duplicate the number.
.RegisterFilterSnap(
new OmniBox.FilterSnapHandler("project", "Project".t(), new[] { "proj", "project" }, new string[0], Icon(UIcons.Briefcase), "Only show one project".t(), exampleValue: "5678"),
(sr, value) => sr.WithTargetQuery(
Mosaik.API.Query.StartAt(nameof(Project), value.Trim()) // the project node, by key
.Out(nameof(Document), "HasDocument") // its documents
.TakeAll()))
StartAt(nodeType, key)finds the project node by its key, so the value must be the key the connector ingested.WithTargetQuerylimits the search to the nodes the query returns. Keep the.TakeAll(): without it the query stops at the default limit of 50 nodes.- A request carries one target query, so a second
proj:chip replaces the first instead of adding to it. Use the field model if users need to search several projects at once.
If a related facet is configured from Document to Project (see Related facets), the facet sidebar can offer the same restriction as a list of projects.
Suggesting values
The fourth constructor argument is what the box suggests after proj:. It takes either form:
| Form | Use when |
|---|---|
string[] |
The values are few and known when the box is built, such as priorities. |
Func<string, Task<string[]>> |
The values come from the workspace. The function receives what the user has typed after the colon. |
Suggestions don't restrict the input: a value that is not in the list still becomes a chip. For project numbers, an async provider backed by a custom endpoint keeps the list current:
new OmniBox.FilterSnapHandler(
"project", "Project".t(), new[] { "proj", "project" },
async typed => await Mosaik.API.Endpoints.CallAsync<string[]>("project-numbers", typed),
Icon(UIcons.Briefcase), "Only show one project".t(), exampleValue: "5678")
The endpoint receives the typed text in the request body and returns the matching project numbers. Return a short list (tens, not thousands) and filter on the server.
A snap for a fixed project
A snap takes no value, so it suits a restriction that never changes. This one makes @apollo mean "project 5678":
.RegisterSnapFilter(
new OmniBox.SnapHandler("apollo", "Apollo".t(), new[] { "apollo" }, Icon(UIcons.Rocket), "Only show the Apollo project (5678)".t()),
sr => sr.SetValueFacet("Document.ProjectNumber", keepOtherTypes: false, invertedBehaviour: false, applyBefore: true, values: new[] { "5678" }))
Typing @ lists every registered snap whose trigger word contains what follows it.
Rules
- Build new handlers for each box. A handler carries the icon component it renders, so don't share one instance between two boxes. Create them inside the
SearchBox(b => ...)callback. - Keep ids unique within a box. The id is how a chip is matched to its callback. Don't reuse a built-in id (
file,webpage,filetype,ext,src) in a box that also callsWithBasicSearchFilters(). - Trigger words are matched without regard to case, so
PROJ:5678andproj:5678are the same filter. - Pick trigger words that are not field names users type. A trigger word followed by a colon is taken as a filter, so registering
titlewould stoptitle:reportfrom being afield:valuerestriction. - Translate the user-visible strings (
.t()), as the built-in filters do. Trigger words are not translated.
Limits
- Custom chips are not restored from a stored request. When a box is handed a request from outside (a reloaded URL, a history entry, a saved search), it rebuilds chips only for the built-in filters (
OmniBoxSearchFilters.SnapIdsIn/FilterSnapsIn). A custom filter's facet still applies to that one search, but no chip shows it, and the next search typed into the box drops it. A user who needs the restriction again types the filter again.
See also
- Search Box Filters: the built-in filters and how they map to facets.
- SearchArea recipe: the search area these filters are registered on.
- Filters and Facets: property, related and time facets.
- Omni Box: the Tesserae component that renders the chips.