diff --git a/commands/graph.config-get.md b/commands/graph.config-get.md index 4e27cab..2f5da28 100644 --- a/commands/graph.config-get.md +++ b/commands/graph.config-get.md @@ -13,34 +13,67 @@ FalkorDB configuration parameters are detailed [here](/configuration). `*` can be used to retrieve the value of all FalkorDB configuration parameters. -``` -127.0.0.1:6379> graph.config get * - 1) 1) "TIMEOUT" - 2) (integer) 0 - 2) 1) "CACHE_SIZE" - 2) (integer) 25 - 3) 1) "ASYNC_DELETE" - 2) (integer) 1 - 4) 1) "OMP_THREAD_COUNT" - 2) (integer) 8 - 5) 1) "THREAD_COUNT" - 2) (integer) 8 - 6) 1) "RESULTSET_SIZE" - 2) (integer) -1 - 7) 1) "VKEY_MAX_ENTITY_COUNT" - 2) (integer) 100000 - 8) 1) "MAX_QUEUED_QUERIES" - 2) (integer) 4294967295 - 9) 1) "QUERY_MEM_CAPACITY" - 2) (integer) 0 -10) 1) "DELTA_MAX_PENDING_CHANGES" - 2) (integer) 10000 -11) 1) "NODE_CREATION_BUFFER" - 2) (integer) 16384 -``` - -``` -127.0.0.1:6379> graph.config get TIMEOUT -1) "TIMEOUT" -2) (integer) 0 -``` +{% capture shell_0 %} +graph.config get * +# Output: +# 1) 1) "TIMEOUT" +# 2) (integer) 0 +# ... +{% endcapture %} + +{% capture python_0 %} +from falkordb import FalkorDB +client = FalkorDB() +config = client.get_config('*') +print(config) +{% endcapture %} + +{% capture javascript_0 %} +import { FalkorDB } from 'falkordb'; +const client = await FalkorDB.connect(); +const config = await client.getConfig('*'); +console.log(config); +{% endcapture %} + +{% capture java_0 %} +FalkorDB client = new FalkorDB(); +Map config = client.getConfig("*"); +System.out.println(config); +{% endcapture %} + +{% capture rust_0 %} +let client = FalkorDB::connect_default(); +let config = client.get_config("*")?; +println!("{:?}", config); +{% endcapture %} + +{% include code_tabs.html id="config_get_tabs" shell=shell_0 python=python_0 javascript=javascript_0 java=java_0 rust=rust_0 %} + +{% capture shell_1 %} +graph.config get TIMEOUT +# Output: +# 1) "TIMEOUT" +# 2) (integer) 0 +{% endcapture %} + +{% capture python_1 %} +timeout = client.get_config('TIMEOUT') +print(timeout) +{% endcapture %} + +{% capture javascript_1 %} +const timeout = await client.getConfig('TIMEOUT'); +console.log(timeout); +{% endcapture %} + +{% capture java_1 %} +Object timeout = client.getConfig("TIMEOUT"); +System.out.println(timeout); +{% endcapture %} + +{% capture rust_1 %} +let timeout = client.get_config("TIMEOUT")?; +println!("{:?}", timeout); +{% endcapture %} + +{% include code_tabs.html id="config_get_timeout_tabs" shell=shell_1 python=python_1 javascript=javascript_1 java=java_1 rust=rust_1 %} diff --git a/commands/graph.config-set.md b/commands/graph.config-set.md index 87555d6..d929738 100644 --- a/commands/graph.config-set.md +++ b/commands/graph.config-set.md @@ -15,18 +15,83 @@ FalkorDB configuration parameters are detailed [here](/configuration). Note: As detailed in the link above, not all FalkorDB configuration parameters can be set at run-time. -``` -127.0.0.1:6379> graph.config get TIMEOUT -1) "TIMEOUT" -2) (integer) 0 -127.0.0.1:6379> graph.config set TIMEOUT 10000 -OK -127.0.0.1:6379> graph.config get TIMEOUT -1) "TIMEOUT" -2) (integer) 10000 -``` - -``` -127.0.0.1:6379> graph.config set THREAD_COUNT 10 -(error) This configuration parameter cannot be set at run-time -``` +{% capture shell_0 %} +graph.config get TIMEOUT +graph.config set TIMEOUT 10000 +graph.config get TIMEOUT +# Output: +# 1) "TIMEOUT" +# 2) (integer) 0 +# OK +# 1) "TIMEOUT" +# 2) (integer) 10000 +{% endcapture %} + +{% capture python_0 %} +from falkordb import FalkorDB +client = FalkorDB() +print(client.get_config('TIMEOUT')) +client.set_config('TIMEOUT', 10000) +print(client.get_config('TIMEOUT')) +{% endcapture %} + +{% capture javascript_0 %} +import { FalkorDB } from 'falkordb'; +const client = await FalkorDB.connect(); +console.log(await client.getConfig('TIMEOUT')); +await client.setConfig('TIMEOUT', 10000); +console.log(await client.getConfig('TIMEOUT')); +{% endcapture %} + +{% capture java_0 %} +FalkorDB client = new FalkorDB(); +System.out.println(client.getConfig("TIMEOUT")); +client.setConfig("TIMEOUT", 10000); +System.out.println(client.getConfig("TIMEOUT")); +{% endcapture %} + +{% capture rust_0 %} +let client = FalkorDB::connect_default(); +println!("{:?}", client.get_config("TIMEOUT")?); +client.set_config("TIMEOUT", 10000)?; +println!("{:?}", client.get_config("TIMEOUT")?); +{% endcapture %} + +{% include code_tabs.html id="config_set_tabs" shell=shell_0 python=python_0 javascript=javascript_0 java=java_0 rust=rust_0 %} + +{% capture shell_1 %} +graph.config set THREAD_COUNT 10 +# Output: +# (error) This configuration parameter cannot be set at run-time +{% endcapture %} + +{% capture python_1 %} +try: + client.set_config('THREAD_COUNT', 10) +except Exception as e: + print(e) +{% endcapture %} + +{% capture javascript_1 %} +try { + await client.setConfig('THREAD_COUNT', 10); +} catch (e) { + console.error(e); +} +{% endcapture %} + +{% capture java_1 %} +try { + client.setConfig("THREAD_COUNT", 10); +} catch (Exception e) { + System.out.println(e); +} +{% endcapture %} + +{% capture rust_1 %} +if let Err(e) = client.set_config("THREAD_COUNT", 10) { + println!("{}", e); +} +{% endcapture %} + +{% include code_tabs.html id="config_set_error_tabs" shell=shell_1 python=python_1 javascript=javascript_1 java=java_1 rust=rust_1 %} diff --git a/commands/graph.constraint-create.md b/commands/graph.constraint-create.md index 6ac3b9d..25ddad6 100644 --- a/commands/graph.constraint-create.md +++ b/commands/graph.constraint-create.md @@ -125,65 +125,110 @@ is a list of `propCount` property names. To create a unique constraint for all nodes with label `Person` enforcing uniqueness on the combination of values of attributes `first_name` and `last_name`, issue the following commands: -```sh +{% capture shell_0 %} redis> GRAPH.QUERY g "CREATE INDEX FOR (p:Person) ON (p.first_name, p.last_name)" -1) 1) "Indices created: 2" - 2) "Cached execution: 0" - 3) "Query internal execution time: 25.779500 milliseconds" redis> GRAPH.CONSTRAINT CREATE g UNIQUE NODE Person PROPERTIES 2 first_name last_name -PENDING -``` - -Since v2.12 indexes are constructed asynchronously. The constraint construction will start once the index is fully constructed. +# Output: PENDING +{% endcapture %} + +{% capture python_0 %} +from falkordb import FalkorDB +client = FalkorDB() +graph = client.select_graph('g') +graph.query("CREATE INDEX FOR (p:Person) ON (p.first_name, p.last_name)") +result = client.create_constraint('g', 'UNIQUE', 'NODE', 'Person', ['first_name', 'last_name']) +print(result) +{% endcapture %} + +{% capture javascript_0 %} +import { FalkorDB } from 'falkordb'; +const client = await FalkorDB.connect(); +const graph = client.selectGraph('g'); +await graph.query("CREATE INDEX FOR (p:Person) ON (p.first_name, p.last_name)"); +const result = await client.createConstraint('g', 'UNIQUE', 'NODE', 'Person', ['first_name', 'last_name']); +console.log(result); +{% endcapture %} + +{% capture java_0 %} +FalkorDB client = new FalkorDB(); +Graph graph = client.selectGraph("g"); +graph.query("CREATE INDEX FOR (p:Person) ON (p.first_name, p.last_name)"); +String result = client.createConstraint("g", "UNIQUE", "NODE", "Person", Arrays.asList("first_name", "last_name")); +System.out.println(result); +{% endcapture %} + +{% capture rust_0 %} +let client = FalkorDB::connect_default(); +let graph = client.select_graph("g"); +graph.query("CREATE INDEX FOR (p:Person) ON (p.first_name, p.last_name)")?; +let result = client.create_constraint("g", "UNIQUE", "NODE", "Person", &["first_name", "last_name"])?; +println!("{}", result); +{% endcapture %} + +{% include code_tabs.html id="unique_constraint_tabs" shell=shell_0 python=python_0 javascript=javascript_0 java=java_0 rust=rust_0 %} ### Creating a mandatory constraint for a relationship type To create a mandatory constraint for all edges with relationship-type `Visited`, enforcing the existence of a `date` attribute, issue the following command: -```sh +{% capture shell_1 %} redis> GRAPH.CONSTRAINT CREATE g MANDATORY RELATIONSHIP Visited PROPERTIES 1 date -PENDING -``` +# Output: PENDING +{% endcapture %} -## Deleting a constraint +{% capture python_1 %} +result = client.create_constraint('g', 'MANDATORY', 'RELATIONSHIP', 'Visited', ['date']) +print(result) +{% endcapture %} -See [GRAPH.CONSTRAINT DROP](/commands/graph.constraint-drop) +{% capture javascript_1 %} +const result = await client.createConstraint('g', 'MANDATORY', 'RELATIONSHIP', 'Visited', ['date']); +console.log(result); +{% endcapture %} + +{% capture java_1 %} +String result = client.createConstraint("g", "MANDATORY", "RELATIONSHIP", "Visited", Arrays.asList("date")); +System.out.println(result); +{% endcapture %} + +{% capture rust_1 %} +let result = client.create_constraint("g", "MANDATORY", "RELATIONSHIP", "Visited", &["date"])?; +println!("{}", result); +{% endcapture %} -## Listing constraints +{% include code_tabs.html id="mandatory_constraint_tabs" shell=shell_1 python=python_1 javascript=javascript_1 java=java_1 rust=rust_1 %} + +### Listing constraints To list all constraints enforced on a given graph, use the `db.constraints` procedure: -```sql -GRAPH.RO_QUERY "CALL db.constraints()" -``` +{% capture shell_2 %} +redis> GRAPH.RO_QUERY g "call db.constraints()" +# Output: ... +{% endcapture %} -For each constraint the procedure will yield the following fields: +{% capture python_2 %} +result = graph.ro_query("call db.constraints()") +print(result) +{% endcapture %} -| Field | Description | -| ------------ | ------------------------------------------------------ | -| `type` | type of constraint, either `UNIQUE` or `MANDATORY` | -| `label` | label or relationship-type enforced by the constraint | -| `properties` | list of properties enforced by the constraint | -| `entitytype` | type of entity, either `NODE` or `RELATIONSHIP` | -| `status` | either `UNDER CONSTRUCTION`, `OPERATIONAL` or `FAILED` | +{% capture javascript_2 %} +const result = await graph.ro_query("call db.constraints()"); +console.log(result); +{% endcapture %} -Example: +{% capture java_2 %} +ResultSet result = graph.ro_query("call db.constraints()"); +System.out.println(result); +{% endcapture %} -```sh -redis> GRAPH.RO_QUERY g "call db.constraints()" -1) 1) "type" - 2) "label" - 3) "properties" - 4) "entitytype" - 5) "status" -2) 1) 1) "UNIQUE" - 2) "Person" - 3) "[birthdate]" - 4) "NODE" - 5) "UNDER CONSTRUCTION" - 2) 1) "MANDATORY" - 2) "Person" - 3) "[first_name, last_name]" - 4) "NODE" - 5) "OPERATIONAL" -``` +{% capture rust_2 %} +let result = graph.ro_query("call db.constraints()")?; +println!("{:?}", result); +{% endcapture %} + +{% include code_tabs.html id="list_constraints_tabs" shell=shell_2 python=python_2 javascript=javascript_2 java=java_2 rust=rust_2 %} + +## Deleting a constraint + +See [GRAPH.CONSTRAINT DROP](/commands/graph.constraint-drop) diff --git a/commands/graph.constraint-drop.md b/commands/graph.constraint-drop.md index c57720f..1e30b17 100644 --- a/commands/graph.constraint-drop.md +++ b/commands/graph.constraint-drop.md @@ -60,7 +60,35 @@ is a list of `propCount` property names. To delete a unique constraint for all nodes with label `Person` enforcing uniqueness on the combination of values of attributes `first_name` and `last_name`, issue the following command: -``` +{% capture shell_0 %} redis> GRAPH.CONSTRAINT DROP g UNIQUE NODE Person PROPERTIES 2 first_name last_name -OK -``` +# Output: OK +{% endcapture %} + +{% capture python_0 %} +from falkordb import FalkorDB +client = FalkorDB() +result = client.drop_constraint('g', 'UNIQUE', 'NODE', 'Person', ['first_name', 'last_name']) +print(result) +{% endcapture %} + +{% capture javascript_0 %} +import { FalkorDB } from 'falkordb'; +const client = await FalkorDB.connect(); +const result = await client.dropConstraint('g', 'UNIQUE', 'NODE', 'Person', ['first_name', 'last_name']); +console.log(result); +{% endcapture %} + +{% capture java_0 %} +FalkorDB client = new FalkorDB(); +String result = client.dropConstraint("g", "UNIQUE", "NODE", "Person", Arrays.asList("first_name", "last_name")); +System.out.println(result); +{% endcapture %} + +{% capture rust_0 %} +let client = FalkorDB::connect_default(); +let result = client.drop_constraint("g", "UNIQUE", "NODE", "Person", &["first_name", "last_name"])?; +println!("{}", result); +{% endcapture %} + +{% include code_tabs.html id="drop_constraint_tabs" shell=shell_0 python=python_0 javascript=javascript_0 java=java_0 rust=rust_0 %} diff --git a/commands/graph.copy.md b/commands/graph.copy.md index b5f7518..130b622 100644 --- a/commands/graph.copy.md +++ b/commands/graph.copy.md @@ -9,20 +9,24 @@ parent: "Commands" Usage: `GRAPH.COPY ` +<<<<<<< HEAD The GRAPH.COPY command creates a copy of a graph while leaving the source graph fully accessible. +======= +The `GRAPH.COPY` command creates a copy of a graph, while the copy is performed the `src` graph is fully accessible. +>>>>>>> 4ce9d9d (add code examples in different langs) Example: {% capture shell_0 %} +<<<<<<< HEAD 127.0.0.1:6379> GRAPH.LIST (empty array) +======= +# Create a graph and copy it +>>>>>>> 4ce9d9d (add code examples in different langs) 127.0.0.1:6379> GRAPH.QUERY A "CREATE (:Account {number: 516637})" -1) 1) "Labels added: 1" - 2) "Nodes created: 1" - 3) "Properties set: 1" - 4) "Cached execution: 0" - 5) "Query internal execution time: 0.588084 milliseconds" 127.0.0.1:6379> GRAPH.COPY A Z +<<<<<<< HEAD "OK" 127.0.0.1:6379> GRAPH.LIST 1) "Z" @@ -54,3 +58,52 @@ result = graph_z.query('MATCH (a:Account) RETURN a.number') {% endcapture %} {% include code_tabs.html id="tabs_0" shell=shell_0 python=python_0 %} +======= +127.0.0.1:6379> GRAPH.QUERY Z "MATCH (a:Account) RETURN a.number" +# Output: 516637 +{% endcapture %} + +{% capture python_0 %} +from falkordb import FalkorDB +client = FalkorDB() +graph_a = client.select_graph('A') +graph_a.query("CREATE (:Account {number: 516637})") +client.copy_graph('A', 'Z') +graph_z = client.select_graph('Z') +result = graph_z.query("MATCH (a:Account) RETURN a.number") +print(result) +{% endcapture %} + +{% capture javascript_0 %} +import { FalkorDB } from 'falkordb'; +const client = await FalkorDB.connect(); +const graphA = client.selectGraph('A'); +await graphA.query("CREATE (:Account {number: 516637})"); +await client.copyGraph('A', 'Z'); +const graphZ = client.selectGraph('Z'); +const result = await graphZ.query("MATCH (a:Account) RETURN a.number"); +console.log(result); +{% endcapture %} + +{% capture java_0 %} +FalkorDB client = new FalkorDB(); +Graph graphA = client.selectGraph("A"); +graphA.query("CREATE (:Account {number: 516637})"); +client.copyGraph("A", "Z"); +Graph graphZ = client.selectGraph("Z"); +ResultSet result = graphZ.query("MATCH (a:Account) RETURN a.number"); +System.out.println(result); +{% endcapture %} + +{% capture rust_0 %} +let client = FalkorDB::connect_default(); +let graph_a = client.select_graph("A"); +graph_a.query("CREATE (:Account {number: 516637})")?; +client.copy_graph("A", "Z")?; +let graph_z = client.select_graph("Z"); +let result = graph_z.query("MATCH (a:Account) RETURN a.number")?; +println!("{:?}", result); +{% endcapture %} + +{% include code_tabs.html id="copy_tabs" shell=shell_0 python=python_0 javascript=javascript_0 java=java_0 rust=rust_0 %} +>>>>>>> 4ce9d9d (add code examples in different langs) diff --git a/commands/graph.delete.md b/commands/graph.delete.md index 68f6efc..278a2ed 100644 --- a/commands/graph.delete.md +++ b/commands/graph.delete.md @@ -22,7 +22,19 @@ GRAPH.DELETE us_government graph.delete() {% endcapture %} -{% include code_tabs.html id="tabs_0" shell=shell_0 python=python_0 %} +{% capture javascript_0 %} +await graph.delete(); +{% endcapture %} + +{% capture java_0 %} +graph.delete(); +{% endcapture %} + +{% capture rust_0 %} +graph.delete()?; +{% endcapture %} + +{% include code_tabs.html id="tabs_0" shell=shell_0 python=python_0 javascript=javascript_0 java=java_0 rust=rust_0 %} Note: To delete a node from the graph (not the entire graph), execute a `MATCH` query and pass the alias to the `DELETE` clause: @@ -34,7 +46,19 @@ GRAPH.QUERY DEMO_GRAPH "MATCH (x:Y {propname: propvalue}) DELETE x" graph.query("MATCH (x:Y {propname: propvalue}) DELETE x") {% endcapture %} -{% include code_tabs.html id="tabs_1" shell=shell_1 python=python_1 %} +{% capture javascript_1 %} +await graph.query("MATCH (x:Y {propname: propvalue}) DELETE x"); +{% endcapture %} + +{% capture java_1 %} +graph.query("MATCH (x:Y {propname: propvalue}) DELETE x"); +{% endcapture %} + +{% capture rust_1 %} +graph.query("MATCH (x:Y {propname: propvalue}) DELETE x")?; +{% endcapture %} + +{% include code_tabs.html id="tabs_1" shell=shell_1 python=python_1 javascript=javascript_1 java=java_1 rust=rust_1 %} WARNING: When you delete a node, all of the node's incoming/outgoing relationships are also removed. diff --git a/commands/graph.explain.md b/commands/graph.explain.md index 657231b..3ef9d4a 100644 --- a/commands/graph.explain.md +++ b/commands/graph.explain.md @@ -15,6 +15,42 @@ Arguments: `Graph name, Query` Returns: `String representation of a query execution plan` -```sh +{% capture shell_0 %} GRAPH.EXPLAIN us_government "MATCH (p:President)-[:BORN]->(h:State {name:'Hawaii'}) RETURN p" -``` +{% endcapture %} + +{% capture python_0 %} +from falkordb import FalkorDB +client = FalkorDB() +graph = client.select_graph('us_government') +query = "MATCH (p:President)-[:BORN]->(h:State {name:'Hawaii'}) RETURN p" +result = graph.explain(query) +print(result) +{% endcapture %} + +{% capture javascript_0 %} +import { FalkorDB } from 'falkordb'; +const client = await FalkorDB.connect(); +const graph = client.selectGraph('us_government'); +const query = "MATCH (p:President)-[:BORN]->(h:State {name:'Hawaii'}) RETURN p"; +const result = await graph.explain(query); +console.log(result); +{% endcapture %} + +{% capture java_0 %} +FalkorDB client = new FalkorDB(); +Graph graph = client.selectGraph("us_government"); +String query = "MATCH (p:President)-[:BORN]->(h:State {name:'Hawaii'}) RETURN p"; +String result = graph.explain(query); +System.out.println(result); +{% endcapture %} + +{% capture rust_0 %} +let client = FalkorDB::connect_default(); +let graph = client.select_graph("us_government"); +let query = r#"MATCH (p:President)-[:BORN]->(h:State {name:'Hawaii'}) RETURN p"#; +let result = graph.explain(query)?; +println!("{}", result); +{% endcapture %} + +{% include code_tabs.html id="explain_tabs" shell=shell_0 python=python_0 javascript=javascript_0 java=java_0 rust=rust_0 %} diff --git a/commands/graph.profile.md b/commands/graph.profile.md index 5fa5eef..c3115cf 100644 --- a/commands/graph.profile.md +++ b/commands/graph.profile.md @@ -20,8 +20,8 @@ instead returning the operation tree structure alongside the number of records p It is important to note that this blends elements of [GRAPH.QUERY](/commands/graph.query) and [GRAPH.EXPLAIN](/commands/graph.explain). It is not a dry run and will perform all graph modifications expected of the query, but will not output results produced by a `RETURN` clause or query statistics. -```sh -GRAPH.PROFILE imdb +{% capture shell_0 %} +GRAPH.PROFILE imdb \ "MATCH (actor_a:Actor)-[:ACT]->(:Movie)<-[:ACT]-(actor_b:Actor) WHERE actor_a <> actor_b CREATE (actor_a)-[:COSTARRED_WITH]->(actor_b)" @@ -29,5 +29,62 @@ CREATE (actor_a)-[:COSTARRED_WITH]->(actor_b)" 2) " Filter | Records produced: 11208, Execution time: 1.250565 ms" 3) " Conditional Traverse | Records produced: 12506, Execution time: 7.705860 ms" 4) " Node By Label Scan | (actor_a:Actor) | Records produced: 1317, Execution time: 0.104346 ms" -``` +{% endcapture %} + +{% capture python_0 %} +from falkordb import FalkorDB +client = FalkorDB() +graph = client.select_graph('imdb') +query = '''\ +MATCH (actor_a:Actor)-[:ACT]->(:Movie)<-[:ACT]-(actor_b:Actor) +WHERE actor_a <> actor_b +CREATE (actor_a)-[:COSTARRED_WITH]->(actor_b) +''' +result = graph.profile(query) +for line in result: + print(line) +{% endcapture %} + +{% capture javascript_0 %} +import { FalkorDB } from 'falkordb'; +const client = await FalkorDB.connect(); +const graph = client.selectGraph('imdb'); +const query = `\ +MATCH (actor_a:Actor)-[:ACT]->(:Movie)<-[:ACT]-(actor_b:Actor) +WHERE actor_a <> actor_b +CREATE (actor_a)-[:COSTARRED_WITH]->(actor_b) +`; +const result = await graph.profile(query); +result.forEach(line => console.log(line)); +{% endcapture %} + +{% capture java_0 %} +FalkorDB client = new FalkorDB(); +Graph graph = client.selectGraph("imdb"); +String query = """ +MATCH (actor_a:Actor)-[:ACT]->(:Movie)<-[:ACT]-(actor_b:Actor) +WHERE actor_a <> actor_b +CREATE (actor_a)-[:COSTARRED_WITH]->(actor_b) +"""; +ResultSet result = graph.profile(query); +for (String line : result) { + System.out.println(line); +} +{% endcapture %} + +{% capture rust_0 %} +let client = FalkorDB::connect_default(); +let graph = client.select_graph("imdb"); +let query = r#" +MATCH (actor_a:Actor)-[:ACT]->(:Movie)<-[:ACT]-(actor_b:Actor) +WHERE actor_a <> actor_b +CREATE (actor_a)-[:COSTARRED_WITH]->(actor_b) +"#; +let result = graph.profile(query)?; +for line in result { + println!("{}", line); +} +{% endcapture %} + +{% include code_tabs.html id="profile_tabs" shell=shell_0 python=python_0 javascript=javascript_0 java=java_0 rust=rust_0 %} diff --git a/commands/graph.query.md b/commands/graph.query.md index 06f9c0a..53f6654 100644 --- a/commands/graph.query.md +++ b/commands/graph.query.md @@ -34,7 +34,22 @@ GRAPH.QUERY us_government "MATCH (p:president)-[:born]->(:state {name:'Hawaii'}) graph.query("MATCH (p:president)-[:born]->(:state {name:'Hawaii'}) RETURN p") {% endcapture %} -{% include code_tabs.html id="tabs_0" shell=shell_0 python=python_0 %} +{% capture javascript_0 %} +const result = await graph.query("MATCH (p:president)-[:born]->(:state {name:'Hawaii'}) RETURN p"); +console.log(result); +{% endcapture %} + +{% capture java_0 %} +ResultSet result = graph.query("MATCH (p:president)-[:born]->(:state {name:'Hawaii'}) RETURN p"); +System.out.println(result); +{% endcapture %} + +{% capture rust_0 %} +let result = graph.query(r#"MATCH (p:president)-[:born]->(:state {name:'Hawaii'}) RETURN p"#).execute().await?; +println!("{:?}", result); +{% endcapture %} + +{% include code_tabs.html id="tabs_0" shell=shell_0 python=python_0 javascript=javascript_0 java=java_0 rust=rust_0 %} #### Parametrized query structure: @@ -51,7 +66,36 @@ GRAPH.QUERY us_government "CYPHER state_name='Hawaii' MATCH (p:president)-[:born graph.query("MATCH (p:president)-[:born]->(:state {name:$state_name}) RETURN p", {'state_name': 'Hawaii'}) {% endcapture %} -{% include code_tabs.html id="tabs_1" shell=shell_1 python=python_1 %} +{% capture javascript_1 %} +const result = await graph.query( + "MATCH (p:president)-[:born]->(:state {name:$state_name}) RETURN p", + { params: { state_name: "Hawaii" } } +); +console.log(result); +{% endcapture %} + +{% capture java_1 %} +Map params = new HashMap<>(); +params.put("state_name", "Hawaii"); +ResultSet result = graph.query( + "MATCH (p:president)-[:born]->(:state {name:$state_name}) RETURN p", + params +); +System.out.println(result); +{% endcapture %} + +{% capture rust_1 %} +let params = std::collections::HashMap::from([ + ("state_name", "Hawaii") +]); +let result = graph.query_with_params( + r#"MATCH (p:president)-[:born]->(:state {name:$state_name}) RETURN p"#, + ¶ms +).execute().await?; +println!("{:?}", result); +{% endcapture %} + +{% include code_tabs.html id="tabs_1" shell=shell_1 python=python_1 javascript=javascript_1 java=java_1 rust=rust_1 %} ### Query language diff --git a/getting_started.md b/getting_started.md index 7050a28..c2be2e0 100644 --- a/getting_started.md +++ b/getting_started.md @@ -5,7 +5,6 @@ description: > Getting Started with FalkorDB Graph Database. --- - # Getting Started with FalkorDB This guide will walk you through setting up FalkorDB, modeling a social network as a graph, @@ -21,10 +20,12 @@ and accessing it using the [FalkorDB Python client](/clients) with [Cypher](/cyp 2. **Python Installed**: Ensure you have Python 3.8+ installed. 3. **Install FalkorDB Python Client**: - ```bash - pip install falkordb - ``` + {% capture shell_0 %} +pip install falkordb +{% endcapture %} + {% include code_tabs.html id="install_tabs" shell=shell_0 %} + --- ## Step 1: Model a Social Network as a Graph @@ -55,7 +56,7 @@ Here’s how you can model and load the data. ### Cypher Query to Create the Data -```cypher +{% capture cypher_0 %} CREATE (alice:User {id: 1, name: "Alice", email: "alice@example.com"}) CREATE (bob:User {id: 2, name: "Bob", email: "bob@example.com"}) CREATE (charlie:User {id: 3, name: "Charlie", email: "charlie@example.com"}) @@ -67,9 +68,11 @@ CREATE (alice)-[:FRIENDS_WITH {since: 1640995200}]->(bob) CREATE (bob)-[:FRIENDS_WITH {since: 1684108800}]->(charlie) CREATE (alice)-[:CREATED {time: 1701388800}]->(post1) CREATE (bob)-[:CREATED {time: 1701475200}]->(post2) -``` +{% endcapture %} + +{% include code_tabs.html id="cypher_create_tabs" cypher=cypher_0 %} -You can execute these commands using the FalkorDB Python client. +You can execute these commands using the FalkorDB Python client or any supported client. --- @@ -77,19 +80,42 @@ You can execute these commands using the FalkorDB Python client. ### Connect to FalkorDB -```python +{% capture python_0 %} from falkordb import FalkorDB # Connect to FalkorDB -db = FalkorDB(host="localhost", port=6379, password="your-password") -graph = db.select_graph('social') -``` +client = FalkorDB(host="localhost", port=6379, password="your-password") +graph = client.select_graph('social') +{% endcapture %} + +{% capture javascript_0 %} +import { FalkorDB } from 'falkordb'; + +const client = await FalkorDB.connect({ + host: "localhost", + port: 6379, + password: "your-password" +}); +const graph = client.selectGraph('social'); +{% endcapture %} + +{% capture java_0 %} +FalkorDB client = new FalkorDB("localhost", 6379, "your-password"); +Graph graph = client.selectGraph("social"); +{% endcapture %} + +{% capture rust_0 %} +let client = FalkorDB::connect("localhost", 6379, Some("your-password")); +let graph = client.select_graph("social"); +{% endcapture %} + +{% include code_tabs.html id="connect_tabs" python=python_0 javascript=javascript_0 java=java_0 rust=rust_0 %} ### Execute Cypher Queries #### Create the Graph -```python +{% capture python_1 %} create_query = """ CREATE (alice:User {id: 1, name: "Alice", email: "alice@example.com"}) CREATE (bob:User {id: 2, name: "Bob", email: "bob@example.com"}) @@ -106,13 +132,69 @@ CREATE (bob)-[:CREATED {time: 1701475200}]->(post2) graph.query(create_query) print("Graph created successfully!") -``` +{% endcapture %} + +{% capture javascript_1 %} +const createQuery = ` +CREATE (alice:User {id: 1, name: "Alice", email: "alice@example.com"}) +CREATE (bob:User {id: 2, name: "Bob", email: "bob@example.com"}) +CREATE (charlie:User {id: 3, name: "Charlie", email: "charlie@example.com"}) + +CREATE (post1:Post {id: 101, content: "Hello World!", date: 1701388800}) +CREATE (post2:Post {id: 102, content: "Graph Databases are awesome!", date: 1701475200}) + +CREATE (alice)-[:FRIENDS_WITH {since: 1640995200}]->(bob) +CREATE (bob)-[:FRIENDS_WITH {since: 1684108800}]->(charlie) +CREATE (alice)-[:CREATED {time: 1701388800}]->(post1) +CREATE (bob)-[:CREATED {time: 1701475200}]->(post2) +`; +await graph.query(createQuery); +console.log("Graph created successfully!"); +{% endcapture %} + +{% capture java_1 %} +String createQuery = """ +CREATE (alice:User {id: 1, name: \"Alice\", email: \"alice@example.com\"}) +CREATE (bob:User {id: 2, name: \"Bob\", email: \"bob@example.com\"}) +CREATE (charlie:User {id: 3, name: \"Charlie\", email: \"charlie@example.com\"}) + +CREATE (post1:Post {id: 101, content: \"Hello World!\", date: 1701388800}) +CREATE (post2:Post {id: 102, content: \"Graph Databases are awesome!\", date: 1701475200}) + +CREATE (alice)-[:FRIENDS_WITH {since: 1640995200}]->(bob) +CREATE (bob)-[:FRIENDS_WITH {since: 1684108800}]->(charlie) +CREATE (alice)-[:CREATED {time: 1701388800}]->(post1) +CREATE (bob)-[:CREATED {time: 1701475200}]->(post2) +"""; +graph.query(createQuery); +System.out.println("Graph created successfully!"); +{% endcapture %} + +{% capture rust_1 %} +let create_query = r#" +CREATE (alice:User {id: 1, name: \"Alice\", email: \"alice@example.com\"}) +CREATE (bob:User {id: 2, name: \"Bob\", email: \"bob@example.com\"}) +CREATE (charlie:User {id: 3, name: \"Charlie\", email: \"charlie@example.com\"}) + +CREATE (post1:Post {id: 101, content: \"Hello World!\", date: 1701388800}) +CREATE (post2:Post {id: 102, content: \"Graph Databases are awesome!\", date: 1701475200}) + +CREATE (alice)-[:FRIENDS_WITH {since: 1640995200}]->(bob) +CREATE (bob)-[:FRIENDS_WITH {since: 1684108800}]->(charlie) +CREATE (alice)-[:CREATED {time: 1701388800}]->(post1) +CREATE (bob)-[:CREATED {time: 1701475200}]->(post2) +"#; +graph.query(create_query)?; +println!("Graph created successfully!"); +{% endcapture %} + +{% include code_tabs.html id="create_graph_tabs" python=python_1 javascript=javascript_1 java=java_1 rust=rust_1 %} ![image](https://github.com/user-attachments/assets/f67c9a1d-4b80-435d-9038-b7e1f931da74) #### Query the Graph -```python +{% capture python_2 %} # Find all friends of Alice query = """ MATCH (alice:User {name: 'Alice'})-[:FRIENDS_WITH]->(friend) @@ -124,11 +206,49 @@ result = graph.ro_query(query).result_set print("Alice's friends:") for record in result: print(record[0]) -``` +{% endcapture %} + +{% capture javascript_2 %} +const query = ` +MATCH (alice:User {name: "Alice"})-[:FRIENDS_WITH]->(friend) +RETURN friend.name AS Friend +`; +const result = await graph.ro_query(query); +console.log("Alice's friends:"); +for (const record of result) { + console.log(record["Friend"]); +} +{% endcapture %} + +{% capture java_2 %} +String query = """ +MATCH (alice:User {name: \"Alice\"})-[:FRIENDS_WITH]->(friend) +RETURN friend.name AS Friend +"""; +ResultSet result = graph.ro_query(query); +System.out.println("Alice's friends:"); +for (Record record : result) { + System.out.println(record.get("Friend")); +} +{% endcapture %} + +{% capture rust_2 %} +let query = r#" +MATCH (alice:User {name: \"Alice\"})-[:FRIENDS_WITH]->(friend) +RETURN friend.name AS Friend +"#; +let result = graph.ro_query(query)?; +println!("Alice's friends:"); +for record in result { + println!("{}", record["Friend"]); +} +{% endcapture %} + +{% include code_tabs.html id="query_friends_tabs" python=python_2 javascript=javascript_2 java=java_2 rust=rust_2 %} #### Query Relationships -```python +{% capture python_3 %} # Find posts created by Bob query = """ MATCH (bob:User {name: 'Bob'})-[:CREATED]->(post:Post) @@ -140,7 +260,45 @@ result = graph.ro_query(query).result_set print("Posts created by Bob:") for record in result: print(record[0]) -``` +{% endcapture %} + +{% capture javascript_3 %} +const query = ` +MATCH (bob:User {name: "Bob"})-[:CREATED]->(post:Post) +RETURN post.content AS PostContent +`; +const result = await graph.ro_query(query); +console.log("Posts created by Bob:"); +for (const record of result) { + console.log(record["PostContent"]); +} +{% endcapture %} + +{% capture java_3 %} +String query = """ +MATCH (bob:User {name: \"Bob\"})-[:CREATED]->(post:Post) +RETURN post.content AS PostContent +"""; +ResultSet result = graph.ro_query(query); +System.out.println("Posts created by Bob:"); +for (Record record : result) { + System.out.println(record.get("PostContent")); +} +{% endcapture %} + +{% capture rust_3 %} +let query = r#" +MATCH (bob:User {name: \"Bob\"})-[:CREATED]->(post:Post) +RETURN post.content AS PostContent +"#; +let result = graph.ro_query(query)?; +println!("Posts created by Bob:"); +for record in result { + println!("{}", record["PostContent"]); +} +{% endcapture %} + +{% include code_tabs.html id="query_posts_tabs" python=python_3 javascript=javascript_3 java=java_3 rust=rust_3 %} ---