PgSearchIndex

org.llm4s.rag.permissions.pg.PgSearchIndex
See thePgSearchIndex companion object
final class PgSearchIndex extends SearchIndex

PostgreSQL implementation of SearchIndex with permission-based filtering.

Uses a JDBC connection pool - one it creates with HikariCP, or a caller-owned javax.sql.DataSource (see PgSearchIndex.fromDataSource) - and pgvector for efficient vector similarity search.

The implementation:

  1. Extends the vectors table with collection_id and readable_by columns
  2. Uses GIN indexes for efficient array containment queries
  3. Applies two-level permission filtering (collection + document)
  4. Gracefully handles corrupt embeddings without failing queries

Corrupt Embedding Handling:

  • Records with unparseable embeddings are skipped during query operations
  • Corrupt records are logged with id and embedding_dim for debugging
  • Queries continue successfully and return topK valid results
  • No silent data corruption - failures are observable via logs

Value parameters

_pgConfig

the configuration the index's pool was built from, if it built one

dataSource

JDBC connection pool

ownsDataSource

whether close closes dataSource

vectorTableName

Name of the vectors table

Attributes

Companion
object
Source
PgSearchIndex.scala
Graph
Supertypes
trait SearchIndex
class Object
trait Matchable
class Any

Members list

Value members

Concrete methods

override def clearCollection(collectionPath: CollectionPath): Result[Long]

Delete all documents in a collection.

Delete all documents in a collection.

Does not delete the collection itself.

Value parameters

collectionPath

The collection to clear

Attributes

Returns

Number of chunks deleted

Definition Classes
Source
PgSearchIndex.scala
override def close(): Unit

Closes the connection pool only if this index created it; a caller-owned DataSource is left open.

Closes the connection pool only if this index created it; a caller-owned DataSource is left open.

Attributes

Definition Classes
Source
PgSearchIndex.scala

Access to collection management

Access to collection management

Attributes

Definition Classes
Source
PgSearchIndex.scala
override def deleteDocument(collectionPath: CollectionPath, documentId: String): Result[Long]

Delete a document from a collection.

Delete a document from a collection.

Removes all chunks associated with the document.

Value parameters

collectionPath

The collection containing the document

documentId

The document identifier

Attributes

Returns

Number of chunks deleted

Definition Classes
Source
PgSearchIndex.scala
override def deleteDocumentFromAllCollections(documentId: String): Result[Long]

One statement over every collection: each chunk ingest writes carries its document id as docId.

One statement over every collection: each chunk ingest writes carries its document id as docId.

Attributes

Definition Classes
Source
PgSearchIndex.scala
override def dropSchema(): Result[Unit]

Drop the permission schema.

Drop the permission schema.

WARNING: This is destructive and will delete all permission data. Only use for testing or complete reset.

Attributes

Returns

Success or error

Definition Classes
Source
PgSearchIndex.scala
override def ingest(collectionPath: CollectionPath, documentId: String, chunks: Seq[ChunkWithEmbedding], metadata: Map[String, String], readableBy: Set[PrincipalId]): Result[Seq[String]]

Ingest a document into a specific collection.

Ingest a document into a specific collection.

The collection must:

  • Exist (call ensureExists first if needed)
  • Be a leaf collection (can contain documents)

Value parameters

chunks

Pre-chunked content with embeddings

collectionPath

Target collection path

documentId

Unique document identifier

metadata

Document metadata

readableBy

Document-level permission override (empty = inherit from collection)

Attributes

Returns

the ids the chunks are stored under, one per chunk in the order given: the id a search result for each carries

Definition Classes
Source
PgSearchIndex.scala
override def initializeSchema(): Result[Unit]

Initialize the permission schema.

Initialize the permission schema.

Creates necessary tables and indexes if they don't exist. Safe to call multiple times (idempotent).

Attributes

Returns

Success or error

Definition Classes
Source
PgSearchIndex.scala
override def keywordQuery(auth: UserAuthorization, collectionPattern: CollectionPattern, queryText: String, topK: Int, additionalFilter: Option[MetadataFilter]): Result[Seq[KeywordSearchResult]]

Full-text search over the vectors table's content, with the permission filter of query.

Full-text search over the vectors table's content, with the permission filter of query.

The query is parsed with websearch_to_tsquery (quoted phrases, - to exclude, or) and ranked with ts_rank_cd, under the english text search configuration - as PgKeywordIndex does by default. initializeSchema creates the GIN index this search uses.

Attributes

Definition Classes
Source
PgSearchIndex.scala
override def pgConfig: Option[PgConfig]

Expose PostgreSQL configuration for automatic RAG integration

Expose PostgreSQL configuration for automatic RAG integration

Attributes

Definition Classes
Source
PgSearchIndex.scala
override def principals: PrincipalStore

Access to principal (user/group) management

Access to principal (user/group) management

Attributes

Definition Classes
Source
PgSearchIndex.scala
override def query(auth: UserAuthorization, collectionPattern: CollectionPattern, queryVector: Array[Float], topK: Int, additionalFilter: Option[MetadataFilter]): Result[Seq[ScoredRecord]]

Query for the most similar vectors matching the user's permissions.

Query for the most similar vectors matching the user's permissions.

Note: if the database contains corrupt embeddings, those rows are silently skipped. Because filtering happens after the SQL LIMIT topK, fewer than topK results may be returned when corrupt rows are present.

Attributes

Definition Classes
Source
PgSearchIndex.scala

Inherited methods

def stats(collectionPath: CollectionPath): Result[CollectionStats]

Get statistics for a collection.

Get statistics for a collection.

Value parameters

collectionPath

The collection path

Attributes

Returns

Collection statistics

Inherited from:
SearchIndex
Source
SearchIndex.scala