Configuration

Copy Markdown

The document configuration map is the central instruction set for creating a document table. It is used to create the document table and populate it from a source table, optionally including joined tables. The same config is passed to Collect.merge_deltas/3 to identify which tables to operate on.

The config map references a single source_table, but by passing a list of config maps to Collect.create_document_table/3, the document table can be sourced from multiple source tables.

Example configuration

config = %{
  source_table: "articles",
  add_identity_column_if_not_exists: true,
  identity_column: "identity",
  language: "english",
  data_fields: [
    %{field_name: "title", value_column: "title"},
    %{field_name: "summary", value_column: "summary"}
  ],
  search_fields: [
    %{field_name: "title", weight: 1},
    # missing weight: will be assigned 4
    %{field_name: "summary"}
  ]
}

Collect.create_document_table("collections", config)

This configuration will be used to:

  • Create the document table with the name passed to Collect.create_document_table/3 (or Collect.create_document_table_if_not_exists/3).
  • Add an integer identity column named identity to souce table articles - if no valid integer ID column already exists (controlled by add_identity_column_if_not_exists).
  • Populate the document table with:
    • JSON data in column data with keys title and summary.
    • A tsvector column with searchable texts from title and summary, where title gets the highest priority.
  • Set the search vector language to "english".

Check the Configuration options to see other capabilities.

Multiple sources

The document table can be extended to include multiple sources by passing a list of configs as the second parameter to Collect.create_document_table/3. In this way, the document table can be adapted to changing requirements. See Configuration changes ↓ below for the steps to take.

configs = [
  %{
    source_table: "articles",
    add_identity_column_if_not_exists: true,
    identity_column: "identity",
    language: "english",
    data_fields: [
      %{field_name: "title", value_column: "title"},
      %{field_name: "subtitle", value_column: "subtitle"},
    ],
    search_fields: [
      %{field_name: "title"},
    ]
  },
  %{
    source_table: "authors",
    add_identity_column_if_not_exists: true,
    identity_column: "identity",
    language: "english",
    data_fields: [
      %{field_name: "first_name", value_column: "first_name"},
      %{field_name: "last_name", value_column: "last_name"},
    ],
    search_fields: [
      %{field_name: "last_name"},
    ]
  }
]

Collect.create_document_table("collections", configs)

Configuration changes

Config changes are not applied automatically. The installed triggers, merge function, and existing documents were generated from the config as it was at install time; changing the config in code has no effect until you reinstall.

To apply a config change, drop the document table and recreate it:

Collect.drop_document_table(document_table, opts)
Collect.create_document_table(document_table, new_config_or_configs, opts)

This removes the old maintenance and documents, reinstalls maintenance from the new config, and backfills every document using the new logic.

Never use a raw DROP TABLE

The maintenance triggers live on your source tables, not the document table, so DROP TABLE document_table does not remove them. Left behind, they keep firing on every source write - and once the deltas table is gone, they fail, breaking writes to your source tables. Always tear down with Collect.drop_document_table/2, which removes the source-table triggers too.

Options

See: Configuration options