Getting Started
Install
The packages are in beta, so install them with --prerelease. Add the core package and one provider:
dotnet add package Bielu.BetterSearch.Core --prerelease
# Then one provider:
dotnet add package Bielu.BetterSearch.ElasticSearch --prerelease # Elasticsearch
dotnet add package Bielu.BetterSearch.OpenSearch --prerelease # OpenSearch
dotnet add package Bielu.BetterSearch.Meilisearch --prerelease # Meilisearch
dotnet add package Bielu.BetterSearch.Typesense --prerelease # Typesense
dotnet add package Bielu.BetterSearch.PostgreSql --prerelease # PostgreSQL full-text search
dotnet add package Bielu.BetterSearch.Lifti --prerelease # in-memory
| Package | Purpose |
|---|---|
Bielu.BetterSearch.Abstractions |
Query model (SearchQuery and its sub-queries), SearchDocument and the request models, and service interfaces |
Bielu.BetterSearch.Core |
AddBetterSearch and the indexing and search services |
Bielu.BetterSearch.FluentApi |
Fluent query builder |
Bielu.BetterSearch.ElasticSearch |
Elasticsearch provider |
Bielu.BetterSearch.OpenSearch |
OpenSearch provider |
Bielu.BetterSearch.Meilisearch |
Meilisearch provider |
Bielu.BetterSearch.Typesense |
Typesense provider |
Bielu.BetterSearch.PostgreSql |
PostgreSQL provider |
Bielu.BetterSearch.Lifti |
In-memory provider |
Register a provider
AddBetterSearch registers IIndexingServiceAsync and ISearchServiceAsync, backed by the provider you
pick. The quickest start is the in-memory provider, which needs no server:
using Bielu.BetterSearch.DependencyInjection;
builder.Services.AddBetterSearch(search => search.AddLiftiSearch());
The Elasticsearch, OpenSearch, Meilisearch, Typesense and PostgreSQL providers read their settings from configuration, so use the overload that takes it:
builder.Services.AddBetterSearch(builder.Configuration, search => search.AddElasticSearch());
AddBetterSearch registers the validator, the indexing and search services and the provider once, after
your callback has run, so a SetIndexingServiceType<T>(), SetSearchServiceType<T>() or
SetDocumentValidatorType<T>() call works before or after the provider extension. It uses TryAdd, so a
service you registered before calling AddBetterSearch is kept. Each call has its own configuration, so
two containers in one process (an app and a test host, say) don't share provider types. Without a provider
extension, every call that reaches the provider returns a failed Result saying that no provider is
registered. Calls that the services answer without the provider don't: an empty batch to
IndexMultipleDocumentsAsync or an empty id list to RemoveDocumentsAsync returns Ok(0), and a document
with a null field or one that fails validation returns that error instead.
IndexingServiceAsync checks its arguments before it calls the provider. A null document, list or request,
a document with a null or empty Id or Index, a DeleteDocumentRequest with a null or empty Id or
Type, and a DeleteAllDocumentsRequest with a null or empty IndexName return a failed Result that names
what is missing. A document the validator rejects returns Document <id> is not valid., and a validator that
fails or throws returns Validation of document <id> failed, with the validator's errors or the exception as
the cause. Either error holds the document's id in its id metadata. Cancellation still throws.
Each provider has its own page with its settings and, importantly, what it doesn't support:
| Provider | Register with | Configuration section |
|---|---|---|
| Elasticsearch | AddElasticSearch() |
Bielu:BetterSearch:ElasticSearch |
| OpenSearch | AddOpenSearch() |
Bielu:BetterSearch:OpenSearch |
| Meilisearch | AddMeilisearch() |
Bielu:BetterSearch:Meilisearch |
| Typesense | AddTypesense() |
Bielu:BetterSearch:Typesense |
| PostgreSQL | AddPostgreSql() |
Bielu:BetterSearch:PostgreSql |
| Lifti | AddLiftiSearch() |
none |
Use more than one provider
Each provider extension registers its provider under a key, its name by default (ElasticSearch,
OpenSearch, Meilisearch, Typesense, PostgreSql, Lifti, also in each extension class's DefaultKey), or the key you pass. For each
key, AddBetterSearch registers keyed services: ISearchProviderAsync, IIndexingProviderAsync,
ISearchServiceAsync, IIndexingServiceAsync and IDocumentValidatorAsync. So one container can hold several
engines, and you pick one with [FromKeyedServices]:
builder.Services.AddBetterSearch(builder.Configuration, search => search
.AddElasticSearch(key: "products")
.AddMeilisearch(key: "site"));
public class ProductSearch([FromKeyedServices("products")] ISearchServiceAsync search) { }
- With one provider, the unkeyed services use it, as before.
- With two or more, resolving an unkeyed
ISearchServiceAsync,IIndexingServiceAsync,ISearchProviderAsyncorIIndexingProviderAsyncthrows anInvalidOperationExceptionthat lists the registered keys, unless you choose a default withSetDefaultProvider("products"), or route indexes, which stops the unkeyed search and indexing services from throwing. A default that names no registered key makesAddBetterSearchthrow. - A key can be used once, and two keys that differ only in case are rejected; resolving by key is
case-sensitive, as in
Microsoft.Extensions.DependencyInjection. Each provider can be added once perAddBetterSearchcall: to reach another server of the same engine, use its per-index settings. SetIndexingServiceType<T>()andSetSearchServiceType<T>()apply to every key. The validator of a key is the unkeyedIDocumentValidatorAsyncunless you register one for that key before callingAddBetterSearch, for exampleservices.AddKeyedScoped<IDocumentValidatorAsync, ProductValidator>("products").
Route indexes to providers
To keep injecting the unkeyed ISearchServiceAsync and IIndexingServiceAsync with two or more providers, route
each index to a provider key, in code or in the Bielu:BetterSearch:Routes configuration section:
builder.Services.AddBetterSearch(builder.Configuration, search => search
.AddElasticSearch(key: "products")
.AddMeilisearch(key: "site")
.RouteIndex("products", "products")
.SetDefaultProvider("site"));
{ "Bielu": { "BetterSearch": { "Routes": { "products": "products", "pages": "site" } } } }
- With at least one route, the unkeyed services send each call to the keyed service of the provider routed for
its index:
SearchQuery.Index,SearchDocument.Index,DeleteDocumentRequest.Index, theindexofRemoveDocumentsAsync,DeleteAllDocumentsRequest.IndexName, and forIIndexLifecycleServiceAsyncthe index being rebuilt (IndexRebuild.Index). Index names compare ignoring case, and so do the keys a route names. - An index with no route goes to the default provider: the one
SetDefaultProvidernames, or the only one. With no default, the call returns a failedResultthat names the index and the registered keys; resolving the unkeyed services doesn't throw. - A route that names a key no provider is registered under, or a configured route with an empty value, makes
AddBetterSearchthrow. ARouteIndexcall replaces a configured route for the same index, and a laterRouteIndexcall for an index replaces an earlier one. IndexMultipleDocumentsAsyncchecks every document for null values and a provider, and validates each one with the validator of its provider, before indexing any of them. So a document whose index has no route when there is no default provider, or that its provider's validator rejects, fails the whole batch with nothing stored, as with one provider. It then splits the documents by provider and calls each provider's indexing service once. When one fails or throws, the others are still called, and the failedResulthas one error saying how many documents each provider indexed. As for a single provider, itsidsmetadata holds the ids not confirmed as stored, and its reasons have one error for each provider that failed, with the key inkeyand that provider's ids not confirmed as stored inids. A provider whose error has noidsmetadata, as when it threw, counts as having stored none of its documents. Cancellation is not caught: it stops the batch, and the providers already called keep what they stored.- Routing applies to the unkeyed
ISearchServiceAsyncandIIndexingServiceAsynconly. The unkeyedISearchProviderAsyncandIIndexingProviderAsyncstill use the default provider, or throw on resolution when there is none.
Index and search
Inject IIndexingServiceAsync and ISearchServiceAsync:
using Bielu.BetterSearch.Abstractions.Models;
using Bielu.BetterSearch.Abstractions.Query;
using Bielu.BetterSearch.Abstractions.Query.SubQueries;
using Bielu.BetterSearch.Abstractions.Services;
public class PeopleSearch(IIndexingServiceAsync indexing, ISearchServiceAsync search)
{
public async Task RunAsync(CancellationToken cancellationToken)
{
await indexing.IndexDocumentAsync(new SearchDocument
{
Id = "1",
Index = "people",
Fields = new Dictionary<string, IList<object>> { ["Name"] = ["Jane Doe"] },
}, cancellationToken);
var query = new SearchQuery { Index = "people", Page = 1, PageSize = 20 };
query.Add(Occurrence.Must, new PrefixSubQuery { Field = "Name", Value = "ja" });
var result = await search.SearchAsync(query, cancellationToken);
if (result.IsSuccess)
{
foreach (var hit in result.Value.Items)
{
// hit.Document.Id, hit.Document.Fields, hit.Score, ...
}
}
}
}
result is a FluentResults Result: check IsSuccess before reading Value, and read Errors when a
provider can't run the query. Each item is an ISearchHit: the stored Document, a SearchDocument as
GetDocumentAsync returns it and IndexDocumentAsync takes it, with its id unchanged and the query's Index, and
the Score the engine gave it, and the Highlights that HighlightQueries asked for, on providers that
highlight. Page counts from 1. A cancelled search throws OperationCanceledException
rather than returning a failed Result. The feature matrix compares every provider side
by side.
Run code after a write
The services have no events. To run code around an operation, such as logging every document indexed, decorate
the service in DI, as the search cache does. Register the decorator after AddBetterSearch, for
example with Scrutor's Decorate, which wraps the unkeyed service; with routed indexes that is the routing
service, so it sees every call made through the unkeyed service, whichever provider it goes to. A decorator sees
the Result of each call, so it can act on success only. Calls made on a keyed service, or on a provider directly,
don't go through it.
builder.Services.AddBetterSearch(builder.Configuration, search => search.AddElasticSearch());
builder.Services.Decorate<IIndexingServiceAsync, LoggingIndexingService>();
sealed class LoggingIndexingService(IIndexingServiceAsync inner, ILogger<LoggingIndexingService> logger)
: IIndexingServiceAsync
{
public async Task<Result> IndexDocumentAsync(SearchDocument document, CancellationToken cancellationToken = default)
{
var result = await inner.IndexDocumentAsync(document, cancellationToken);
if (result.IsSuccess)
{
logger.LogInformation("Indexed {Id}", document.Id);
}
return result;
}
public Task<Result<int>> IndexMultipleDocumentsAsync(IEnumerable<SearchDocument> documents, CancellationToken cancellationToken = default) =>
inner.IndexMultipleDocumentsAsync(documents, cancellationToken);
public Task<Result> RemoveDocumentAsync(DeleteDocumentRequest document, CancellationToken cancellationToken = default) =>
inner.RemoveDocumentAsync(document, cancellationToken);
public Task<Result<int>> RemoveDocumentsAsync(string index, IEnumerable<string> ids, CancellationToken cancellationToken = default) =>
inner.RemoveDocumentsAsync(index, ids, cancellationToken);
public Task<Result<long?>> RemoveAllDocumentsAsync(DeleteAllDocumentsRequest request, CancellationToken cancellationToken = default) =>
inner.RemoveAllDocumentsAsync(request, cancellationToken);
public Task<Result<bool>> UpdateDocumentAsync(string index, string id, IDictionary<string, IList<object>> fields,
CancellationToken cancellationToken = default) =>
inner.UpdateDocumentAsync(index, id, fields, cancellationToken);
}
For metrics and traces, use the built-in OpenTelemetry instrumentation instead: add the
Bielu.BetterSearch.OpenTelemetry package and call AddBetterSearchInstrumentation() on both the tracer and the
meter provider builder.
Filter by culture
To search one culture, index it in the plain Culture field and filter on it with the fluent API
(Bielu.BetterSearch.FluentApi):
using Bielu.BetterSearch.Abstractions.Fields;
using Bielu.BetterSearch.FluentApi;
using Bielu.BetterSearch.FluentApi.Fluent.Extensions;
document.Fields[CultureField.Name] = [CultureField.Value("en-US")];
var query = FluentApiSearchQueryCreator.Create()
.WithSearchQuery(search => search.WithIndex("pages"))
.CreateSearchQuery(search => search
.CreateTermQuery(term => term.WithField("Title").WithValue("welcome"))
.FilterByCulture("en-US"));
On Lifti the field is ignored, so a document holding the culture value in another field matches too.
Each clause takes the occurrence of the query or bool it is created on, Must unless you choose another. Call
Must(), Should(), MustNot() or Filter() on the sub-query itself to choose its own:
.CreateBoolQuery(cities => cities
.CreateTermQuery(term => term.WithField("City").WithValue("Warsaw").Should())
.CreateTermQuery(term => term.WithField("City").WithValue("Krakow").Should()))
Note
Returning a failed Result instead of throwing is the contract every provider is moving to, but in this
beta some paths still throw, such as invalid or missing connection settings. The
roadmap lists known defects.
Run the engines locally
The repository's Aspire AppHost, src/examples/Bielu.BetterSearch.Examples.AppHost, starts Elasticsearch,
OpenSearch and Meilisearch in Docker, with meilisearch-ui for
browsing Meilisearch indexes. Run aspire run from src/, or dotnet run the project; the Meilisearch master
key is the meilisearch-masterKey parameter shown in the Aspire dashboard.