# Document Search in C# without Elasticsearch

> Build a full-text search engine in C# with keyword search, semantic search, hybrid ranking, and reranking. Index files and query them in 10 lines of code, with the index living on your own disk.

Source: https://kjarni.ai/blog/documentsearchengine/ (published 2026-02-11)

Index local files and search them by keyword, by meaning, or both, in about 10 lines of C#. The index is a directory on your disk and the search runs in your process.

```bash
dotnet add package Kjarni
```
[NuGet](https://www.nuget.org/packages/Kjarni)
```C#
using Kjarni;

if (!Directory.Exists("my_index"))
{
    using var indexer = new Indexer(model: "minilm-l6-v2", quiet: true);
    indexer.Create("my_index", new[] { "docs/" });
}

using var searcher = new Searcher(
    model: "minilm-l6-v2",
    rerankerModel: "minilm-l6-v2-cross-encoder");

var results = searcher.Search("my_index", "how do returns work?",
    mode: SearchMode.Hybrid);

foreach (var r in results)
    Console.WriteLine($"  {r.Score:F4}: {r.Text}");
```

Build the index once and reuse it. `Create` refuses to overwrite an existing
index rather than discarding it silently; pass `force: true` when you mean to
rebuild.

The indexer reads your files, splits them into chunks, encodes each chunk
as a vector, and builds a BM25 keyword index. The searcher queries both
indexes and combines the results. Everything runs locally on CPU.

## The Usual Options, and Why They're Heavy

Most search implementations fall into one of two camps: spin up an Elasticsearch cluster, or call a managed search API like Azure AI Search or Algolia. Both work. Both add infrastructure, configuration, and ongoing cost.

Kjarni is a third option: a NuGet package that gives you keyword search, semantic search, hybrid ranking, and reranking in a single library. The index is a folder on your disk and the search runs in your process, so your documents stay where they are.

## Setup

Create a few text files to search over:

```bash
mkdir -p docs
```

**docs/returns.txt:**
```
Our return policy allows customers to return any unused item within 30 days
of purchase for a full refund. Items must be in their original packaging.
Shipping costs are non-refundable.
```

**docs/shipping.txt:**
```
We ship to all 50 US states and internationally to over 40 countries.
Standard shipping takes 5-7 business days. Express shipping is available
for an additional fee.
```

**docs/account.txt:**
```
To reset your password, click "Forgot Password" on the login page.
You will receive an email with a reset link. The link expires after 24 hours.
```

Three short documents. In practice these could be product manuals,
support articles, internal wikis, or any text files.

## Indexing

```C#
using var indexer = new Indexer(model: "minilm-l6-v2", quiet: true);
indexer.Create("my_index", new[] { "docs/" });
```

The indexer does three things:

1. **Reads** all files in the given directories
2. **Chunks** each file into passages (for long documents)
3. **Encodes** each chunk into a 384-dimension vector using the embedding model

It also builds a BM25 keyword index over the same chunks.
The result is a local index on disk that you can query repeatedly
without re-indexing.

## Three Search Modes

### Keyword Search (BM25)

Matches documents that contain the query words. The same algorithm that
powers Elasticsearch and Solr.

```C#
var results = searcher.Search("my_index", "return policy refund",
    mode: SearchMode.Keyword);
```

```
  7.8795: Our return policy allows customers to return any unused item
          within 30 days of purchase for a full refund...
```

The searcher above has a reranker attached, so the score shown is the reranker's, in
every mode. BM25 picks the candidates; the cross-encoder scores them. A
`new Searcher(model: "minilm-l6-v2")` without a reranker returns the BM25 score itself,
3.2028 here.

This works because the query words ("return", "policy", "refund") appear
in the document. If you searched for "send items back and get money" instead,
keyword search would find nothing.

BM25 scores are relative to the corpus, not absolute: the same document and the same
query will score differently once you add more documents, because the rarity of each
term changes. Compare scores within one result set, never across two.

### Semantic Search

Matches documents by meaning, regardless of the exact words used.

```C#
var results = searcher.Search("my_index", "can I send items back and get money?",
    mode: SearchMode.Semantic);
```

This finds the returns document even though none of those exact words appear in it.
The embedding model understands that "send items back" means "return" and
"get money" means "refund."

For more on how embeddings and similarity work, see
[Semantic Search in C#](/blog/semanticsearch).

### Hybrid Search

Combines keyword and semantic results. This is usually the best default.

```C#
var results = searcher.Search("my_index", "how do returns work?",
    mode: SearchMode.Hybrid);
```

```
   1.3282: Our return policy allows customers to return any unused item
           within 30 days of purchase for a full refund. Items must be in
           their original packaging. Shipping costs are non-refundable.

 -10.5874: To reset your password, click "Forgot Password" on the login
           page. You will receive an email with a reset link. The link
           expires after 24 hours.

 -11.0939: We ship to all 50 US states and internationally to over 40
           countries. Standard shipping takes 5-7 business days. Express
           shipping is available for an additional fee.
```

Hybrid search catches both exact keyword matches and semantically
related content. The scores are from the reranker (more on that below),
which is why the gap between relevant and irrelevant results is so large.
The returns document scores 1.3, while the other two are deep in the negatives.

## Reranking

The results above use a **cross-encoder reranker**. This is the difference
between good search and great search.

### The Problem with Embeddings Alone

Embedding models are fast because they encode the query and each document
independently. But this means they can't model the interaction between
query and document directly. They're comparing summaries, not reading
both texts together.

### How Reranking Fixes This

A cross-encoder takes the query and a document as a single input and
outputs a relevance score. It reads both texts at the same time, so it
can attend to specific words in the document that answer the specific question.

```
Bi-encoder (embedding):     Query -> Vector    Document -> Vector    Compare
Cross-encoder (reranker):   [Query + Document] -> Relevance Score
```

The cross-encoder is slower because it processes each query-document pair
individually. That's why it's used as a second stage: the embedding model
retrieves candidates quickly, then the cross-encoder reranks the top results
precisely.

### Using the Reranker Directly

You can also use the reranker on its own:

```C#
using var reranker = new Reranker();

var results = reranker.Rerank(
    "What is machine learning?",
    new[] {
        "Machine learning is a subset of artificial intelligence.",
        "Deep learning uses neural networks with many layers.",
        "The weather today is sunny.",
    });

foreach (var r in results)
    Console.WriteLine($"  {r.Score:F4}: {r.Document}");
```

```
  10.5139: Machine learning is a subset of artificial intelligence.
  -5.5301: Deep learning uses neural networks with many layers.
 -11.1001: The weather today is sunny.
```

The scores are logits, not probabilities. What matters is the relative
ordering and the gap between scores. A positive score means the
cross-encoder thinks the document is relevant to the query. A negative
score means it's not.

A sigmoid puts them on a 0 to 1 scale, which reads more easily. The ordering
is unchanged:

```C#
static float Sigmoid(float x) =>
    x >= 0f ? 1f / (1f + MathF.Exp(-x)) : MathF.Exp(x) / (1f + MathF.Exp(x));

foreach (var r in results)
    Console.WriteLine($"  {Sigmoid(r.Score):P2}  {r.Document}");
```

```
  100.00%  Machine learning is a subset of artificial intelligence.
    0.40%  Deep learning uses neural networks with many layers.
    0.00%  The weather today is sunny.
```

## The Full Pipeline

Here's how the pieces fit together:

```
Query
  |
  +-- BM25 Keyword Index ----> Top N candidates by word match
  |
  +-- Vector Index ----------> Top N candidates by meaning
  |
  v
  Merge candidates (union or intersection)
  |
  v
  Cross-Encoder Reranker ----> Final ranked results
  |
  v
  Return to user
```

Each stage filters and refines. BM25 is cheap and catches exact matches.
The vector index catches semantic matches that keywords miss. The reranker
reads both query and document together to produce a precise ranking.

```C#
if (!Directory.Exists("my_index"))
{
    using var indexer = new Indexer(model: "minilm-l6-v2", quiet: true);
    indexer.Create("my_index", new[] { "docs/" });
}

using var searcher = new Searcher(
    model: "minilm-l6-v2",
    rerankerModel: "minilm-l6-v2-cross-encoder");

// Hybrid = BM25 + Semantic + Reranker
var results = searcher.Search("my_index", "how do returns work?",
    mode: SearchMode.Hybrid);
```

## When to Use Each Mode

| Mode | Best for | Misses |
|------|----------|--------|
| `Keyword` | Exact terms, error codes, IDs | Synonyms, rephrased queries |
| `Semantic` | Intent matching, fuzzy queries | Exact phrases, rare terms |
| `Hybrid` | General purpose (recommended) | Slightly slower |

Start with `Hybrid`. Switch to `Keyword` if your users search for exact
identifiers. Switch to `Semantic` if your users describe what they want
in natural language.

## Practical Patterns

### Searching Different File Types

The indexer reads text files from disk. For other formats, extract
the text first and write it to a file:

```C#
// Extract text from PDFs, HTML, etc. into a directory
ExtractTextFromPdfs("input/", "docs/");

using var indexer = new Indexer(model: "minilm-l6-v2", quiet: true);
indexer.Create("my_index", new[] { "docs/" });
```

### Re-indexing

When documents change, rebuild with `force`:

```C#
indexer.Create("my_index", new[] { "docs/" }, force: true);
```

Without `force` this throws rather than overwriting an index you may not have
meant to replace. It rebuilds everything; for large corpora where incremental
updates matter, you'd manage the vector storage separately.

### Filtering Results

Apply a score threshold to filter out irrelevant results:

```C#
var results = searcher.Search("my_index", query, mode: SearchMode.Hybrid);

var relevant = results.Where(r => r.Score > 0.0);
```

With reranking, a score above 0 is a reasonable default threshold for
"probably relevant." Adjust based on your precision/recall needs.

### Search + Classification

Find relevant documents, then classify their sentiment:

```C#
using var searcher = new Searcher(model: "minilm-l6-v2");
using var classifier = new Classifier("roberta-sentiment");

var results = searcher.Search("reviews_index", "battery life",
    mode: SearchMode.Hybrid);

foreach (var r in results.Take(10))
{
    var sentiment = classifier.Classify(r.Text);
    Console.WriteLine($"  {sentiment}  \"{r.Text}\"");
}
```

See [Sentiment Analysis in C#](/blog/sentimentanalysis)
for more on classification.

## How It Compares

| Approach | Setup | Latency | Cost | Offline |
|----------|-------|---------|------|---------|
| Elasticsearch | Cluster + config | Low | Server costs | No |
| Azure AI Search | Portal + API key | Low | Per-query pricing | No |
| Algolia | Dashboard + API key | Low | Per-search pricing | No |
| **Kjarni** | `dotnet add package` | Low | Free | Yes |

The tradeoff: Kjarni runs in-process on a single machine. If you need
distributed search across billions of documents, use Elasticsearch.
If you need search over thousands to millions of documents on a single
server, a local engine works well and eliminates a dependency.

## How It Works Under the Hood

[Kjarni](https://github.com/olafurjohannsson/kjarni) builds two
indexes per collection:

1. **BM25 index**: inverted index over tokenized text, with term
   frequency saturation and document length normalization
2. **Vector index**: encoded embeddings for each chunk, queried
   by cosine similarity

At search time, both indexes return candidates. The results are merged
and optionally reranked by a cross-encoder model that reads the query
and each candidate together.

The engine is written in Rust with SIMD-optimized kernels (AVX2/FMA on x86,
NEON on ARM). The C# package wraps a single native library that loads into your process:
no JVM, no external service.

For the full story on why Kjarni exists and how it compares to Python and
ONNX Runtime, see [Why I Built a Native ML Inference Engine in Rust](/blog/nativeinference).

```
Install:  dotnet add package Kjarni
NuGet:    https://www.nuget.org/packages/Kjarni
GitHub:   https://github.com/olafurjohannsson/kjarni
```

## Next Steps

- [Semantic Search in C#](/blog/semanticsearch): match text by meaning, not keywords
- [Sentiment Analysis in C#](/blog/sentimentanalysis): classify text as positive, negative, or neutral
- [Why I Built a Native ML Inference Engine in Rust](/blog/nativeinference): the technical story behind Kjarni
