Camel Components

PGVector

Since Camel 4.19

Only producer is supported

The PGVector Component provides support for interacting with pgvector, the open-source vector similarity search extension for PostgreSQL.

URI format

pgvector:collection[?options]

Where collection represents the table name used to store vectors in the PostgreSQL database.

Configuring the DataSource

A javax.sql.DataSource must be provided. It is recommended to use a connection pooling DataSource (such as HikariCP) for production deployments. The DataSource can be set on the component or endpoint configuration, or autowired from the registry.

Actions

The action option of the endpoint, such as pgvector:myTable?action=SIMILARITY_SEARCH, or the CamelPgVectorAction header selects the action; the header, when set, wins over the option. Without either, the producer fails. The following actions are supported:

  • CREATE_TABLE - Creates the pgvector extension and a table with columns: id, text_content, metadata, embedding

  • CREATE_INDEX - Creates an HNSW index on the embedding column for faster approximate nearest neighbor search. The index uses the distance type configured on the endpoint (default: COSINE)

  • DROP_TABLE - Drops the table

  • UPSERT - Inserts or updates a vector record. The body is the vector: a List of numbers, such as a List<Float> or the List<Double> a JSON array unmarshals to, or a float[] or double[]; each number is converted to a float, and another body fails. Set CamelPgVectorRecordId for the ID (auto-generated UUID if not set), CamelPgVectorTextContent for text, and CamelPgVectorMetadata for metadata

  • DELETE - Deletes a record by CamelPgVectorRecordId

  • SIMILARITY_SEARCH - Searches for similar vectors. The body is the query vector, of the same types as for UPSERT. Set CamelPgVectorQueryTopK for max results (default 3). Optionally set CamelPgVectorFilter with a SQL WHERE clause to filter results (e.g., text_content LIKE '%hello%'). Returns a List<Map<String, Object>> with keys: id, text_content, metadata, distance

Defaults

  • dimension: 384 (matches common embedding models like all-MiniLM-L6-v2)

  • distanceType: COSINE (other options: EUCLIDEAN, INNER_PRODUCT)

Parameterized Filters

When using the SIMILARITY_SEARCH action, you can filter results using a SQL WHERE clause via the CamelPgVectorFilter header. For safe handling of dynamic values, use parameterized queries with ? placeholders and provide values via the CamelPgVectorFilterParams header:

Java-only: setting parameterized filter headers programmatically
from("direct:search")
    .setHeader(PgVectorHeaders.ACTION).constant(PgVectorAction.SIMILARITY_SEARCH)
    .setHeader(PgVectorHeaders.FILTER).constant("text_content LIKE ? AND metadata::jsonb->>'category' = ?")
    .setHeader(PgVectorHeaders.FILTER_PARAMS).constant(List.of("%hello%", "science"))
    .to("pgvector:documents");

Security

The CamelPgVectorFilter header value is appended directly as a SQL WHERE clause. When using static, developer-controlled filter expressions this is safe. However, never pass untrusted user input directly as the filter value without using parameterized queries (? placeholders with CamelPgVectorFilterParams), as this could lead to SQL injection.

OpenAI Integration

The component works directly with the OpenAI component for embedding generation. The OpenAI embeddings endpoint returns a List<Float>, which is exactly the body format expected by the pgvector UPSERT and SIMILARITY_SEARCH actions.

  • Java

  • YAML

// Index a document
from("direct:index")
    .setVariable("text", body())
    .to("openai:embeddings?embeddingModel=nomic-embed-text")
    .setHeader(PgVectorHeaders.ACTION).constant(PgVectorAction.UPSERT)
    .setHeader(PgVectorHeaders.TEXT_CONTENT).variable("text")
    .to("pgvector:documents");

// Similarity search
from("direct:search")
    .to("openai:embeddings?embeddingModel=nomic-embed-text")
    .setHeader(PgVectorHeaders.ACTION).constant(PgVectorAction.SIMILARITY_SEARCH)
    .setHeader(PgVectorHeaders.QUERY_TOP_K).constant(5)
    .to("pgvector:documents");
- route:
    from:
      uri: direct:index
      steps:
        - setVariable:
            name: text
            expression:
              simple:
                expression: "${body}"
        - to:
            uri: openai:embeddings
            parameters:
              embeddingModel: nomic-embed-text
        - setHeader:
            name: CamelPgVectorAction
            expression:
              constant:
                expression: UPSERT
        - setHeader:
            name: CamelPgVectorTextContent
            expression:
              simple:
                expression: "${variable.text}"
        - to:
            uri: pgvector:documents

- route:
    from:
      uri: direct:search
      steps:
        - to:
            uri: openai:embeddings
            parameters:
              embeddingModel: nomic-embed-text
        - setHeader:
            name: CamelPgVectorAction
            expression:
              constant:
                expression: SIMILARITY_SEARCH
        - setHeader:
            name: CamelPgVectorQueryTopK
            expression:
              constant:
                expression: 5
        - to:
            uri: pgvector:documents

LangChain4j Integration

This component provides data type transformers for LangChain4j Embeddings integration:

  • pgvector:embeddings - Transforms LangChain4j embedding output into a format suitable for the PGVector UPSERT action

  • pgvector:rag - Transforms similarity search results into a List<String> for RAG pipelines

  • Java

  • YAML

// Store embeddings
from("direct:store")
    .to("langchain4j-embeddings:embed")
    .setHeader(PgVectorHeaders.ACTION).constant(PgVectorAction.UPSERT)
    .transformDataType(new DataType("pgvector:embeddings"))
    .to("pgvector:myCollection");

// Similarity search for RAG
from("direct:search")
    .to("langchain4j-embeddings:embed")
    .transformDataType(new DataType("pgvector:embeddings"))
    .setHeader(PgVectorHeaders.ACTION, constant(PgVectorAction.SIMILARITY_SEARCH))
    .to("pgvector:myCollection")
    .transformDataType(new DataType("pgvector:rag"));
- route:
    from:
      uri: direct:store
      steps:
        - to:
            uri: langchain4j-embeddings:embed
        - setHeader:
            name: CamelPgVectorAction
            expression:
              constant:
                expression: UPSERT
        - transformDataType:
            toType: "pgvector:embeddings"
        - to:
            uri: pgvector:myCollection

- route:
    from:
      uri: direct:search
      steps:
        - to:
            uri: langchain4j-embeddings:embed
        - transformDataType:
            toType: "pgvector:embeddings"
        - setHeader:
            name: CamelPgVectorAction
            expression:
              constant:
                expression: SIMILARITY_SEARCH
        - to:
            uri: pgvector:myCollection
        - transformDataType:
            toType: "pgvector:rag"

Configuring Options

Camel components are configured on two separate levels:

  • component level

  • endpoint level

Configuring Component Options

At the component level, you set general and shared configurations that are, then, inherited by the endpoints. It is the highest configuration level.

For example, a component may have security settings, credentials for authentication, urls for network connection and so forth.

Some components only have a few options, and others may have many. Because components typically have pre-configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.

You can configure components using:

  • the Component DSL.

  • in a configuration file (application.properties, *.yaml files, etc).

  • directly in the Java code.

Configuring Endpoint Options

You usually spend more time setting up endpoints because they have many options. These options help you customize what you want the endpoint to do. The options are also categorized into whether the endpoint is used as a consumer (from), as a producer (to), or both.

Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL and DataFormat DSL as a type safe way of configuring endpoints and data formats in Java.

A good practice when configuring options is to use Property Placeholders.

Property placeholders provide a few benefits:

  • They help prevent using hardcoded urls, port numbers, sensitive information, and other settings.

  • They allow externalizing the configuration from the code.

  • They help the code to become more flexible and reusable.

The following two sections list all the options, firstly for the component followed by the endpoint.

Component Options

The PGVector component supports the following options which are listed below.

Name Description Default Type

action (producer)

The action to perform. The CamelPgVectorAction header, when set, wins over this option.

Enum values:

  • CREATE_TABLE

  • CREATE_INDEX

  • DROP_TABLE

  • UPSERT

  • DELETE

  • SIMILARITY_SEARCH

PgVectorAction

configuration (producer)

The configuration shared by the endpoints of the component. Each endpoint uses a copy of it, which its own options then change.

PgVectorConfiguration

dataSource (producer)

Autowired The javax.sql.DataSource of the PostgreSQL database. The endpoint fails to start when no DataSource is set or autowired. The producer takes a new connection from it for each message, so a pooling DataSource is recommended.

DataSource

dimension (producer)

The number of dimensions of the embedding column that CREATE_TABLE creates, as vector(n). It must match the size of the vectors of the embedding model, as PostgreSQL rejects a vector of another size. The other actions do not use it.

384

int

distanceType (producer)

The distance function of SIMILARITY_SEARCH and of the HNSW index that CREATE_INDEX creates: COSINE (cosine distance), EUCLIDEAN (L2 distance) or INNER_PRODUCT (the negative inner product, so the closest records have the lowest distance). A search uses the index only when both use the same distance type.

Enum values:

  • COSINE

  • EUCLIDEAN

  • INNER_PRODUCT

COSINE

PgVectorDistanceType

lazyStartProducer (producer)

Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing.

false

boolean

autowiredEnabled (advanced)

Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc.

true

boolean

Endpoint Options

The PGVector endpoint is configured using URI syntax:

pgvector:collection

With the following path and query parameters:

Path Parameters

Name Description Default Type

collection (producer)

Required The name of the table that holds the records. It must be a plain SQL identifier of letters, digits and underscores that does not start with a digit. CREATE_INDEX names the index after the table, followed by _embedding_idx.

String

Query Parameters

Name Description Default Type

action (producer)

The action to perform. The CamelPgVectorAction header, when set, wins over this option.

Enum values:

  • CREATE_TABLE

  • CREATE_INDEX

  • DROP_TABLE

  • UPSERT

  • DELETE

  • SIMILARITY_SEARCH

PgVectorAction

dataSource (producer)

Autowired The javax.sql.DataSource of the PostgreSQL database. The endpoint fails to start when no DataSource is set or autowired. The producer takes a new connection from it for each message, so a pooling DataSource is recommended.

DataSource

dimension (producer)

The number of dimensions of the embedding column that CREATE_TABLE creates, as vector(n). It must match the size of the vectors of the embedding model, as PostgreSQL rejects a vector of another size. The other actions do not use it.

384

int

distanceType (producer)

The distance function of SIMILARITY_SEARCH and of the HNSW index that CREATE_INDEX creates: COSINE (cosine distance), EUCLIDEAN (L2 distance) or INNER_PRODUCT (the negative inner product, so the closest records have the lowest distance). A search uses the index only when both use the same distance type.

Enum values:

  • COSINE

  • EUCLIDEAN

  • INNER_PRODUCT

COSINE

PgVectorDistanceType

lazyStartProducer (producer (advanced))

Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing.

false

boolean

Message Headers

The PGVector component supports the following message header(s), which is/are listed below:

Name Description Default Type

CamelPgVectorAction (producer)

Constant: ACTION

The action that the producer performs. It replaces the action option.

Enum values:

  • CREATE_TABLE

  • CREATE_INDEX

  • DROP_TABLE

  • UPSERT

  • DELETE

  • SIMILARITY_SEARCH

String

CamelPgVectorRecordId (producer)

Constant: RECORD_ID

The id of the record, of at most 36 characters. UPSERT inserts the record with this id or replaces it, and uses a random UUID when the header is not set. DELETE requires it.

String

CamelPgVectorQueryTopK (producer)

Constant: QUERY_TOP_K

The maximum number of results to return for similarity search.

3

Integer

CamelPgVectorTextContent (producer)

Constant: TEXT_CONTENT

The text that UPSERT stores in the text_content column of the record.

String

CamelPgVectorMetadata (producer)

Constant: METADATA

The metadata that UPSERT stores in the metadata column of the record, a TEXT column. The producer does not check the value; the pgvector:embeddings transformer sets it as a JSON object.

String

CamelPgVectorFilter (producer)

Constant: FILTER

Filter condition for similarity search. Applied as a SQL WHERE clause on the text_content and metadata columns. Supports parameterized queries using ? placeholders with values provided via the CamelPgVectorFilterParams header. WARNING: When not using parameterized queries, the filter value is appended directly as SQL. Never use untrusted input as the filter value without parameterization, as this could lead to SQL injection.

String

CamelPgVectorFilterParams (producer)

Constant: FILTER_PARAMS

The values of the ? placeholders of the CamelPgVectorFilter header, in order. For example the filter text_content LIKE ? AND metadata::jsonb→>'category' = ? with the List [%hello%, science]. The producer reads it only when that header is set.

List

Message Body

The message body at each side of the PGVector component: Consumer is the body of the messages the consumer creates, Consumer reply the bodies a consumer that answers its caller can reply with, Producer input the bodies the producer can send or write, and Producer output the body after the producer.

Side Body Description

Producer input

list or null

By the action in the action option or the CamelPgVectorAction header: the vector as a List of numbers, such as the List of Double a JSON array unmarshals to, or a float or double array for UPSERT and SIMILARITY_SEARCH, with the id, text and metadata of UPSERT in headers. CREATE_TABLE, CREATE_INDEX, DROP_TABLE and DELETE do not read the body, DELETE takes the id from the CamelPgVectorRecordId header. Another body fails: the pgvector:embeddings data type transformer builds the vector from the output of langchain4j-embeddings.

  • With action=UPSERT: list

  • With action=SIMILARITY_SEARCH: list

  • With action=CREATE_TABLE: any

  • With action=CREATE_INDEX: any

  • With action=DROP_TABLE: any

  • With action=DELETE: any

Producer output

value, text or list

By the action: a Boolean for CREATE_TABLE, CREATE_INDEX, DROP_TABLE and DELETE (false when no record had the id), the record id as a String for UPSERT, and a List of Map with the keys id, text_content, metadata and distance for SIMILARITY_SEARCH.

  • With action=CREATE_TABLE: value (java.lang.Boolean)

  • With action=CREATE_INDEX: value (java.lang.Boolean)

  • With action=DROP_TABLE: value (java.lang.Boolean)

  • With action=DELETE: value (java.lang.Boolean)

  • With action=UPSERT: text (java.lang.String)

  • With action=SIMILARITY_SEARCH: list (java.util.List<java.util.Map<java.lang.String, java.lang.Object>>)

The bodies are the ones with the default options. A side that is not listed is not known, and tooling does not guess it. The carriers, the sides and the tools that use them are described in Message Body Types.