Neo4j
Since Camel 4.10
Only producer is supported
The Neo4j component provides support for Neo4j graph database.
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-neo4j</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency>
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 Neo4j component supports 19 options, which are listed below.
Name | Description | Default | Type |
---|---|---|---|
Node alias. | String | ||
The configuration;. | Neo4jConfiguration | ||
Url for connecting to Neo database. | String | ||
Detach a relationship - set true if want to delete a node and detach its relationships to other nodes at same time. | false | boolean | |
Dimension of Vector Index. | Integer | ||
Node Label. | String | ||
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 | |
Maximum results for Vector Similarity search. | 3 | int | |
Minimum score for Vector Similarity search. | 0 | double | |
Cypher Query. | String | ||
Similarity Function of Vector Index. Enum values:
| cosine | Neo4jSimilarityFunction | |
Vector Index Name. | String | ||
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 | |
Autowired Advanced - Driver. | Driver | ||
Kerberos Authentication encoded base64 ticket. | String | ||
Basic authentication database password. | String | ||
Basic authentication database realm. | String | ||
Bearer authentication database realm. | String | ||
Basic authentication database user. | String |
Endpoint Options
The Neo4j endpoint is configured using URI syntax:
neo4j:name
With the following path and query parameters:
Query Parameters (17 parameters)
Name | Description | Default | Type |
---|---|---|---|
Node alias. | String | ||
Url for connecting to Neo database. | String | ||
Detach a relationship - set true if want to delete a node and detach its relationships to other nodes at same time. | false | boolean | |
Dimension of Vector Index. | Integer | ||
Node Label. | String | ||
Maximum results for Vector Similarity search. | 3 | int | |
Minimum score for Vector Similarity search. | 0 | double | |
Cypher Query. | String | ||
Similarity Function of Vector Index. Enum values:
| cosine | Neo4jSimilarityFunction | |
Vector Index Name. | String | ||
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 | |
Autowired Advanced - Driver. | Driver | ||
Kerberos Authentication encoded base64 ticket. | String | ||
Basic authentication database password. | String | ||
Basic authentication database realm. | String | ||
Bearer authentication database realm. | String | ||
Basic authentication database user. | String |
Message Headers
The Neo4j component supports 12 message header(s), which is/are listed below:
Name | Description | Default | Type |
---|---|---|---|
CamelNeo4jOperation (producer) Constant: | The operation to be performed. Enum values:
| String | |
CamelNeo4jMatchProperties (producer) Constant: | MATCH properties for the generated MATCH query. Needed only if we are matching properties and values. Example: \{name: 'Alice'}. | String | |
CamelNeo4jQueryResult (producer) Constant: | Query Result. | String | |
CamelNeo4jQueryResultNodesCreated (producer) Constant: | Query Number of nodes created. | Long | |
CamelNeo4jQueryResultNodesDeleted (producer) Constant: | Query Number of nodes deleted. | Long | |
CamelNeo4jQueryResultContainsUpdates (producer) Constant: | Query executed contains update. | Boolean | |
CamelNeo4jQueryResultRelationshipsCreated (producer) Constant: | Query executed number of relationships created. | Long | |
CamelNeo4jQueryResultRelationshipsDeleted (producer) Constant: | Query executed number of relationships deleted. | Long | |
CamelNeo4jQueryResultRetrieveSize (producer) Constant: | Number of nodes retrieved. | Long | |
CamelNeo4jQueryResultListNeo4jNodes (producer) Constant: | Query execution time in Milliseconds. | Long | |
CamelNeo4jVectorEmbeddingId (producer) Constant: | Vector Id for the embedding. | String | |
Constant: | Label for the Node - used when inserting from Embeddings. | String |
Spring Boot Auto-Configuration
When using neo4j with Spring Boot make sure to use the following Maven dependency to have support for auto configuration:
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-neo4j-starter</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency>
The component supports 20 options, which are listed below.
Name | Description | Default | Type |
---|---|---|---|
Node alias. | String | ||
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 | |
The configuration;. The option is a org.apache.camel.component.neo4j.Neo4jConfiguration type. | Neo4jConfiguration | ||
Url for connecting to Neo database. | String | ||
Detach a relationship - set true if want to delete a node and detach its relationships to other nodes at same time. | false | Boolean | |
Dimension of Vector Index. | Integer | ||
Advanced - Driver. The option is a org.neo4j.driver.Driver type. | Driver | ||
Whether to enable auto configuration of the neo4j component. This is enabled by default. | Boolean | ||
Kerberos Authentication encoded base64 ticket. | String | ||
Node Label. | String | ||
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 | |
Maximum results for Vector Similarity search. | 3 | Integer | |
Minimum score for Vector Similarity search. | Double | ||
Basic authentication database password. | String | ||
Cypher Query. | String | ||
Basic authentication database realm. | String | ||
Similarity Function of Vector Index. | cosine | Neo4jSimilarityFunction | |
Bearer authentication database realm. | String | ||
Basic authentication database user. | String | ||
Vector Index Name. | String |
Graph database usage - simple operations
Create a Node
To create a node in a database named test
, define the body as a string containing the JSON body. Use the operation CREATE_NODE
. The URI endpoint should contain also specify the label and the alias.
var body = "{name: 'Alice', email: 'alice@example.com', age: 30}";
Exchange result = fluentTemplate.to("neo4j:test?alias=u1&label=User")
.withBodyAs(body, String.class)
.withHeader(Neo4j.Headers.OPERATION, Neo4Operation.CREATE_NODE)
.request(Exchange.class);
Create a Node with properties
To create a node in a database named test
with properties, define the body as a Map containing the properties and values. Use the operation CREATE_NODE
. The URI endpoint should contain also specify the label and the alias.
Map<String, Object> params = Map.of(
"name", "Bob",
"email", "bob@example.com",
"age", 25);
Exchange result = fluentTemplate.to("neo4j:test?alias=u2&label=User")
.withBodyAs(params, Map.class)
.withHeader(Neo4j.Headers.OPERATION, Neo4Operation.CREATE_NODE)
.request(Exchange.class);
Retrieve Node
To retrieve a node in a database named test
. Define the header MATCH_PROPERTIES
as a string containing the match query. Use the operation RETRIEVE_NODES
. The URI endpoint should also specify the label and the alias. The response is a List of Map<String, Object>. Each map represents the list of properties of a single node.
Exchange result = fluentTemplate.to("neo4j:test?alias=u&label=User")
.withHeader(Neo4j.Headers.OPERATION, Neo4Operation.RETRIEVE_NODES)
.withHeader(Neo4j.Headers.MATCH_PROPERTIES, "{name: 'Alice'}")
.request(Exchange.class);
To retrieve all nodes with label=User
, use the same request without specifying any MATCH_PROPERTIES
.
Exchange result = fluentTemplate.to("neo4j:test?alias=u&label=User")
.withHeader(Neo4j.Headers.OPERATION, Neo4Operation.RETRIEVE_NODES)
.request(Exchange.class);
Delete Node
To delete a node in a database named test
. Define the header MATCH_PROPERTIES
as a string containing the match query. Use the operation DELETE_NODE
. The URI endpoint should also specify the label and the alias.
Exchange result = fluentTemplate.to("neo4j:test?alias=u&label=User")
.withHeader(Neo4j.Headers.OPERATION, Neo4Operation.DELETE_NODE)
.withHeader(Neo4j.Headers.MATCH_PROPERTIES, "{name: 'Alice'}")
.request(Exchange.class);
Delete Node with relationships
If a node has relationships, it won’t be deleted unless we either delete the relationships or delete it with detached relationships. To delete a node with detached relationships, set the option detachRelationship
to true
.
Exchange result = fluentTemplate.to("neo4j:test?alias=u&label=User&detachRelationship=true")
.withHeader(Neo4j.Headers.OPERATION, Neo4Operation.DELETE_NODE)
.withHeader(Neo4j.Headers.MATCH_PROPERTIES, "{name: 'Alice'}")
.request(Exchange.class);
Graph database usage - use Cypher Queries
Create / Delete Nodes with a Cypher query
To create or delete nodes with a Cypher query in a database named test
, set the Cypher query in the body. Use the operation ADD_OR_DELETE_NODE_WITH_CYPHER_QUERY
. The operation can be used too to create multiple nodes and relationships between nodes.
var cypherQuery = "CREATE (u3:User {name: 'Charlie', email: 'charlie@example.com', age: 35})";
Exchange result = fluentTemplate.to("neo4j:test")
.withHeader(Neo4j.Headers.OPERATION, Neo4Operation.ADD_OR_DELETE_NODE_WITH_CYPHER_QUERY)
.withBodyAs(cypherQuery, String.class)
.request(Exchange.class);
var cypherQuery = "MATCH (u:User {name: 'Bob'}) DELETE u";
Exchange result = fluentTemplate.to("neo4j:test")
.withHeader(Neo4j.Headers.OPERATION, Neo4Operation.ADD_OR_DELETE_NODE_WITH_CYPHER_QUERY)
.withBodyAs(cypherQuery, String.class)
.request(Exchange.class);
Retrieve / Update nodes with a Cypher query
To retrieve or update nodes with Cypher Query in a database named test
, set the Cypher query in the body. Use the operation RETRIEVE_NODES_AND_UPDATE_WITH_CYPHER_QUERY
.
The operation is the same as ADD_OR_DELETE_NODE_WITH_CYPHER_QUERY
, except that it returns a list of retrieved or updated nodes represented as Map<String, Object>
.
var cypherQuery = "MATCH " +
"(u:User {name: 'Ethan'})" +
"SET u.age=41 " +
"RETURN u";
Exchange result = fluentTemplate.to("neo4j:test")
.withBodyAs(cypherQuery, String.class)
.withHeader(Neo4j.Headers.OPERATION, Neo4Operation.RETRIEVE_NODES_AND_UPDATE_WITH_CYPHER_QUERY)
.request(Exchange.class);
Vector database embeddings usage
Create a vector index
To use Neo4j as a Vector database, we must create first a vector index for a label.
To create a vector index in a database named test
, use the operation CREATE_VECTOR_INDEX
. The URI endpoint should also specify the label, the alias, the vector index name and the dimension of embeddings.
Exchange result = fluentTemplate.to("neo4j:test?vectorIndexName=movieIdx&alias=m&label=Movie&dimension=2")
.withHeader(Neo4j.Headers.OPERATION, Neo4Operation.CREATE_VECTOR_INDEX)
.request(Exchange.class);
Drop a vector index
To drop a vector index in a database named test
, use the operation DROP_VECTOR_INDEX
. The URI endpoint should contain also specify the vector index name.
Exchange result = fluentTemplate.to("neo4j:test?vectorIndexName=movieIdx")
.withHeader(Neo4j.Headers.OPERATION, Neo4Operation.DROP_VECTOR_INDEX)
.request(Exchange.class);
Create a vector
To create a vector in a database named test
, use the operation CREATE_VECTOR
. The URI endpoint should also specify the label, the alias and the vector index name.
Camel Neo4j will create the node and store the vector as an embedding
property.
Exchange result = fluentTemplate.to("neo4j:test?vectorIndexName=movieIdx&label=Movie&alias=m")
.withHeader(Neo4j.Headers.OPERATION, Neo4Operation.CREATE_VECTOR)
.withHeader(Neo4j.Headers.VECTOR_ID, testData.getId())
.withBody(List.of(0.8f, 0.6f))
.request(Exchange.class);
Similarity search
To perform a similarity search using vectors in a database named test
, use the operation VECTOR_SIMILARITY_SEARCH
. The URI endpoint should also specify the label, the alias and the vector index name.
Exchange result = fluentTemplate.to("neo4j:test?vectorIndexName=movieIdx&label=Movie&alias=m")
.withHeader(Neo4j.Headers.OPERATION, Neo4Operation.CREATE_VECTOR)
.withHeader(Neo4j.Headers.VECTOR_ID, testData.getId())
.withBody(List.of(0.75f, 0.65f))
.request(Exchange.class);
Generate Embeddings with Langchain4j-embeddings
You can generate embeddings with an Embedding Models using the camel Lancghain4j Embeddings components. Camel Neo4j introduces a DataType neo4j:embeddings
that automates the transformations of the Lancghain4j embeddings to Neo4j vectors.
from("direct:in")
.to("langchain4j-embeddings:test")
.setHeader(Neo4j.Headers.OPERATION).constant(Neo4Operation.CREATE_VECTOR)
.setHeader(Neo4j.Headers.LABEL).constant("Test")
.transform(new DataType("neo4j:embeddings"))
.to("neo4j:test");