Craticula

MongoDB and OpenSearch adapters

This page covers the two optional packages that run Craticula queries on a document store: Craticula.MongoDB.Driver for MongoDB and Craticula.OpenSearch.Client for OpenSearch and Elasticsearch. It is for a developer who uses one of those stores and wants Craticula to run the query as well as write it.

What the core gives you, and what the adapters add #

The compilers write the query and stop there.

Neither compiler depends on a client library, and neither opens a connection. You can run their output yourself.

The adapters run it for you. Install them from NuGet: dotnet add package Craticula.MongoDB.Driver or dotnet add package Craticula.OpenSearch.Client. See the C# quickstart.

MongoDB #

Craticula.MongoDB.Driver compiles with Craticula.Mongo and runs the pipeline through the official MongoDB driver, in the range [3.14.1, 4.0).

MongoGridPage result = await MongoGridQuery.FetchPageAsync(
    database,
    new MongoQueryRequest { Schema = schema, Filter = filter, Sort = sort, Page = page },
    includeRowCount: true);

What it gives you:

What it needs from you:

OpenSearch and Elasticsearch #

Craticula.OpenSearch.Client compiles with Craticula.Search and runs the body through the low-level client of OpenSearch.Client, in the range [2.2.0, 3.0). It sends the compiled JSON as it is. It does not build the request a second time with the typed client.

OpenSearchGridPage result = await OpenSearchGridQuery.FetchPageAsync(
    client,
    new SearchQueryRequest { Schema = schema, Filter = filter, Sort = sort, Page = page },
    gridSessionId,
    cache);

Here client is an IOpenSearchLowLevelClient, gridSessionId is a string you choose for the grid session, and cache is an ISearchCursorCache.

What it gives you:

What it needs from you:

The cursor cache #

OpenSearch refuses plain paging past 10,000 rows. The adapter switches to a point in time and search_after, and it remembers where the last block ended. That memory is the ISearchCursorCache you pass in.

InMemorySearchCursorCache is the one that ships. Create it once for the process and share it. A new cache for each request makes every deep page start again from row zero. InMemorySearchCursorCache keeps its entries in one process. If the requests of one grid session can reach several servers, a deep page on another server has no cursor and walks from row zero again. A cache that all servers share would avoid that, but none ships.

The same cursor serves grouped levels. A grouped level uses a composite aggregation, which cannot start at a non-zero row. The adapter keeps the key of the last group and sends it back to ask for the next block.

Options #

OpenSearchDeepPagingOptions changes the paging:

SettingDefaultWhat it does
MaxResultWindow10,000The row count after which the adapter switches to the cursor.
WalkBatchSize1,000How many rows one catch-up step advances.
PitKeepAlive5 minutesHow long an idle cursor stays valid.
ExactRowCountfalseAsk the engine for the exact count when you request a row count.
FlavourOpenSearchThe engine, OpenSearch or Elasticsearch.

Without ExactRowCount, a count above the window is reported as unknown, which is cheap. With it, the engine counts every match on each first request.

Elasticsearch #

The compiled request is the same for both engines. Only the calls that open and close a point in time differ. Set the flavour on both option records when the cluster is Elasticsearch:

var paging = new OpenSearchDeepPagingOptions { Flavour = SearchEngineFlavour.Elasticsearch };
var export = new OpenSearchExportOptions { Flavour = SearchEngineFlavour.Elasticsearch };

With the default flavour against Elasticsearch, a window past 10,000 rows and every export fail with HTTP 400.

What they never do #

Where this comes from #