barrel_ngram (barrel_ngram v0.7.1)
View Sourcebarrel_ngram: exact substring search over barrel_docdb.
A byte-level trigram index giving exact lexical recall (identifiers, error strings, config keys) that semantic search misses. A corpus is bound to a database and a gram selector; indexing is driven by the database's changes feed, and every query result is confirmed against the real document text.
M1 usage
ok = barrel_ngram:open(<<"code">>, #{db => <<"mydb">>}),
{ok, _} = barrel_ngram:index(<<"code">>),
{ok, Hits} = barrel_ngram:search(<<"code">>, <<"connect_timeout">>, #{}).Requires the barrel_ngram application to be started.
Summary
Functions
Close a corpus, stopping every shard.
Compact every shard's live segments, physically evicting superseded and deleted entries. Returns {error, busy} if a background compaction is already running on a shard.
Catch the corpus up to the current head of its database's changes feed and freeze the buffer. The index is kept live in the background by a feed subscription; this is the synchronous catch-up point for tests and ops. Alias of refresh/1.
Whether a corpus is currently open (cheap metadata check).
Create or re-attach a corpus bound to a database.
Synchronously drain the changes feed up to now and freeze every shard's buffer into a segment.
Equivalent to regex(Corpus, Regex, #{}).
Regex search (PCRE syntax). Returns hits with the matching id and the match spans within its corpus text. {error, {bad_regex, _}} if the pattern does not compile.
Equivalent to search(Corpus, Literal, #{}).
Substring search. Returns hits with the matching document id and the match spans within its corpus text.
Types
Functions
-spec close(corpus()) -> ok.
Close a corpus, stopping every shard.
Compact every shard's live segments, physically evicting superseded and deleted entries. Returns {error, busy} if a background compaction is already running on a shard.
Catch the corpus up to the current head of its database's changes feed and freeze the buffer. The index is kept live in the background by a feed subscription; this is the synchronous catch-up point for tests and ops. Alias of refresh/1.
Whether a corpus is currently open (cheap metadata check).
Create or re-attach a corpus bound to a database.
There is no separate create step: this creates the corpus if it does not exist and re-attaches (resuming from its on-disk state) if it does. It starts a feed subscription that keeps the index in sync. selector, shards, and postings are fixed for the life of a corpus.
Options:
db(required) - the barrel_docdb database name to index.selector- gram selector module (defaultbarrel_ngram_selector_dense).selector_opts- selector tuning map (default#{}), e.g. the sparse selector'sradiusandsample_rate.fields-allor a list of field names to index (defaultall).shards- number of shards to spread the corpus across by rendezvous hashing (default 1).postings- posting-list codec,varint(default) orroaring(a native bitmap AND for large dense corpora).data_dir- segment storage directory (default from app env); segments live underdata_dir/<corpus>/.freeze_threshold- buffer size before an automatic freeze (default 1000).compact_threshold- live segment count before an automatic compaction (default 16;infinitydisables it).
Synchronously drain the changes feed up to now and freeze every shard's buffer into a segment.
-spec regex(corpus(), binary()) -> {ok, [barrel_ngram_query:hit()]} | {error, term()}.
Equivalent to regex(Corpus, Regex, #{}).
-spec regex(corpus(), binary(), map()) -> {ok, [barrel_ngram_query:hit()]} | {error, term()}.
Regex search (PCRE syntax). Returns hits with the matching id and the match spans within its corpus text. {error, {bad_regex, _}} if the pattern does not compile.
-spec search(corpus(), binary()) -> {ok, [barrel_ngram_query:hit()]} | {error, term()}.
Equivalent to search(Corpus, Literal, #{}).
-spec search(corpus(), binary(), map()) -> {ok, [barrel_ngram_query:hit()]} | {error, term()}.
Substring search. Returns hits with the matching document id and the match spans within its corpus text.