Search and contents

Document file sources: documents from a directory

A file source gives a Node the plain-text and markdown files of one directory as documents of one document type. The Node reads the directory on a schedule, ingests each new or changed file by the path every other document takes (see Retrieval & Ingestion Pipeline), and never writes to the directory.

Configure a source

A source is an entry under document_intake.providers, beside the Node's embedding and, optionally, document_types sections:

"document_intake": {
  "providers": {
    "sops": {
      "provider": "file",
      "source_directory": "/srv/documents/sops",
      "format": "markdown",
      "document_type": "sop",
      "on_remove": "deactivate"
    },
    "work-instructions": {
      "provider": "file",
      "source_directory": "/srv/documents/work-instructions",
      "format": "text",
      "document_type": "work_instruction"
    }
  }
}
FieldMeaning
source_directory Absolute path of the directory. Subdirectories are read; nothing is moved or written.
format text (files ending .txt) or markdown (.md, .markdown), in any case. One format per source.
document_type The document type of every file of this source, a name from the document_types catalogue.
on_remove report (default) or deactivate: what happens to a document whose file is gone.
id_prefix Optional text put before each document's id (see below). May not contain @, and is at most 100 characters.
poll_interval_seconds How often the directory is read. Default 300 seconds.
max_file_bytes The largest file taken. Default 16 MiB, at most 256 MiB.
settle_seconds A file changed this recently waits for a later read. Default 5 seconds; 0 to 3600.

The Node refuses a source it cannot serve when it starts, and python3 -m engine.offline_check_cli reports the same faults: a document_type the catalogue does not define, a relative source_directory, an @ in id_prefix, two sources reading one directory (compared after .. and repeated / are resolved), one directory inside another and two sources for one type. Two names for one directory through a symbolic link, a bind mount or a case-insensitive mount are not seen.

A source is a document type

The documents of a type come from the source configured for it. Once a file source names a type, the Node refuses a document of that type from any other provider, the REST routes included; the sender is told document_type: wrong_source when it asks for the status of the call. The Node logs a warning at start when it has a REST provider as well as file sources, naming the types the REST provider will be refused. A type that no source names stays open to every provider. To move a type to another source, change the entry; to feed one type from two directories, put them under one directory.

A type that keeps each revision (revisions.policy: keep, such as the starter's design_note) cannot be a file source: a file does not say which revision it is. Send those documents through the document routes, which carry a revision label.

What a file becomes

  • Its id is its path below the source directory, written with /, after id_prefix: sop/seal-housing.md in a source with id_prefix: "plant1/" is document plant1/sop/seal-housing.md. Ids are site-wide, so give each source a prefix when two sources could hold the same relative path. Moving or renaming a file makes a new document and, with on_remove: deactivate, withdraws the old.
  • Its revision is the file's modification time. A file is ingested once per modification time:
    • An unchanged file is not read again, and a configuration change alone ingests nothing.
    • A file that is saved again with the same content is ingested again with the same result.
    • A file whose modification time moves back (restored with an old timestamp) is older than what was handled. It is skipped with a warning that says to touch the file. Touching it gives it a new modification time, and it is read on the first poll after the settle window. (reingest does not help here: it takes only documents made under an earlier configuration.)
  • Its title and document number come from the type's identity first-line rule; with none, the title is the id.
  • Its text is the file as UTF-8, with a leading byte order mark removed.

Files that are not taken

Each of these is logged once, by its path and the reason and never by its content, and left where it is; the other files of the source are taken as usual.

Reason
not a <format> file a file of another ending, such as a PDF beside the markdown
symbolic link, not followed a link, to a file or a directory, is never followed
larger than max_file_bytes
not UTF-8 text, or holds a NUL
empty nothing but whitespace
could not be opened, read or examined the log line names the error type

Files and directories whose names start with . are ignored without a message. A file changed while it was being read is left for the next read.

When a file is removed

A document whose file is gone is noticed after two reads in a row without it, so a file saved by an editor that deletes and recreates it is not withdrawn. With on_remove: report the log names the document and its chunks stay in retrieval. With on_remove: deactivate every chunk of the document is withdrawn from retrieval and the Activity Log records the change; nothing is deleted, and activate (below) returns it.

Nothing is judged removed from a read that did not finish cleanly (a subdirectory that could not be listed, more than 100,000 files or a depth over 32), from a read of a directory that could not be listed at all, or from a read that found no file of the format. A share that is not mounted, or a permission change, therefore withdraws nothing. A file that comes back is not reactivated automatically: the log says the document is still deactivated, and activate restores it. A withdrawn document is also forgotten by the Node's record of what it handled, so a restart does not withdraw it again after an activate. Once that has been done, a file that returns is read as a new document (the record is dropped at the end of a read, and tried again at the next if the database was out).

Operator commands

python3 -m engine.document_tools acts on a Node's documents while the Node runs. Every command takes --config, --node-instance-id and, when the Node Type directory is not node-types/mes, --node-type-dir, as the Node does (and --shared-config-dir where the config uses shared fragments). It prints document ids and counts, never text.

# Withdraw a document from retrieval, or return it. The id is the document's id as above.
python3 -m engine.document_tools --config node.json --node-instance-id mes-1 \
    deactivate --id sop/seal-housing.md --reason "under review"
python3 -m engine.document_tools --config node.json --node-instance-id mes-1 \
    activate --id sop/seal-housing.md --reason "review complete"

# Read the files of one type again after its chunking configuration changed.
python3 -m engine.document_tools --config node.json --node-instance-id mes-1 reingest --type sop

deactivate and activate take the id a provider knows the document by and act on every revision of it the store holds; --revision-label narrows them to one revision of a type that keeps them. The routes of the management API do the same for a sender and need a rest provider; these commands do not.

reingest

reingest brings documents made under an earlier configuration of a type up to date. Changing a type's chunking changes nothing already stored. After it, reingest --type <type> does this:

  1. It selects that type's documents below the configuration version now in force. --below-version N chooses another bound, and --include-inactive includes withdrawn documents, which stay withdrawn.
  2. It finds each one's file in the type's source directory.
  3. It ingests each again, regardless of the handled-once rule.

It lists the documents that have no file any more and leaves them as they are. The type must have a file source.

Exit statusMeaning
0 Everything selected was ingested.
1 Something was refused, held, failed or unreadable, or a document was not found.
2 The request or the configuration was wrong, a configuration or Node Type file that cannot be read included.
3 The source directory or the database could not be reached.

Give the commands the configuration file the Node runs. reingest builds the Node's own collaborators, so a configuration the history has not seen is recorded there, as it is when a Node starts with it. It prints the configuration version it selected documents below. deactivate and activate record no configuration version.

Limits

  • Reading a directory is cheap: for 5,000 files of 19 KiB each, listing takes about 13 ms, reading in full about 119 ms and a read where nothing changed about 24 ms (a development machine; your figures will differ). Ingesting is what takes time, at the pace of the embedding endpoint; at most two documents are embedded at once, however many sources there are.
  • A 16 MiB document of ordinary prose is about 24,000 chunks and is chunked in under 0.2 s, but embedding that many chunks takes minutes; split documents that large.
  • Only plain text and markdown are read. A source holding other formats leaves them in place and logs them once.

Release: v0.11.0