Skip to content

Commit 184c6b6

Browse files
authored
Merge pull request #314 from FalkorDB/danshalev7-patch-8-1
Revise UDFs documentation for clarity and consistency
2 parents a380d86 + e1a0e19 commit 184c6b6

File tree

1 file changed

+45
-51
lines changed

1 file changed

+45
-51
lines changed

udfs/index.md

Lines changed: 45 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,18 @@ nav_order: 6
77

88
# UDFs
99

10-
Every databases comes with a set of built-in functions, for example among FalkorDB functions you'll find:
11-
`abs` - computes the absolute value of a number
12-
`pow` - computes v^x
13-
`trim` - removes leading and trailing spaces.
10+
Every database comes with a set of built-in functions. For example, FalkorDB functions include:
11+
- `abs` - computes the absolute value of a number
12+
- `pow` - computes v^x
13+
- `trim` - removes leading and trailing spaces.
1414

15-
These are baked into the DB and are part of its source code, introducing a new function e.g. `UpperCaseOdd`isn't always trivial,
16-
the function needs to be usable to a wide audience for it to be considered, in the past we've rejected requests for adding new functions as these were too specific and we didn't believe they've added a significant value for most of our users.
15+
These functions are built into the database and are part of its source code. Introducing a new function (for example, `UpperCaseOdd`) is not always trivial. The function needs to be usable to a wide audience for it to be considered. In the past, FalkorDB has rejected requests for adding new functions when these were too specific and did not add significant value for most users.
1716

18-
But now, with the support of UDFs, everyone can extend FalkorDB's functionality with their own set of functions. Following is an introduction to UDFs, how to manage & use them within FalkorDB.
17+
However, with the support of UDFs, everyone can extend FalkorDB's functionality with their own set of functions. The following sections introduce UDFs and explain how to manage and use them within FalkorDB.
1918

2019

21-
## Example
22-
In order to introduce UDFs, please review the following, a complete example which loads a new UDF library "StringUtils" that includes a single function "UpperCaseOdd", once loaded the script puts it to use.
20+
## Practical Example
21+
To introduce UDFs, review the following complete example, which loads a new UDF library called "StringUtils" that includes a single function called "UpperCaseOdd". Once loaded, the script puts the function to use.
2322

2423
```python
2524
from falkordb import FalkorDB
@@ -57,13 +56,13 @@ print(f"s: {s}") # prints 'AbCdEf'
5756
```
5857
## Commands Specification
5958

60-
Although conveniently available through `FalkorDB-PY` Python client, FalkorDB exposes its UDF functionality via a set of new `GRAPH.UDF <sub_cmd>` commands.
59+
The FalkorDB-PY Python client provides convenient access to UDF functionality, but FalkorDB also exposes this functionality via a set of GRAPH.UDF <sub_cmd> commands.
6160

6261
### GRAPH.UDF LOAD [REPLACE] <Lib> <script>
6362

64-
Adding a UDF is done by calling `GRAPH.UDF LOAD` followed by an optional `REPLACE` keyword which, if specified, replaces an already registered UDF library. The command then takes the library name and the library script written in JavaScript arguments.
63+
To add a UDF, call `GRAPH.UDF LOAD` followed by an optional `REPLACE` keyword. When specified, the REPLACE keyword replaces an already registered UDF library. The command then takes two arguments: the library name and the library script (written in JavaScript).
6564

66-
A UDF library can expose multiple UDFs, here's an example of a script which includes both non-exposed utility functions and a number of callable functions:
65+
A UDF library can expose multiple UDFs. The following example shows a script that includes both non-exposed utility functions and a number of callable functions:
6766

6867
```javascript
6968
function ShapeType(shape) {
@@ -100,10 +99,9 @@ falkor.register('Perimeter', Perimeter);
10099
falkor.register('RandomShape', RandomShape);
101100
```
102101

103-
For each UDF script FalkorDB exposes the `falkor` object, through which you register UDFs.
104-
To register a function call `falkor.register` and provide the name you wish to expose your function under, followed by either an anonymous function or the actual function.
102+
For each UDF script, FalkorDB exposes the falkor object, through which you register UDFs. To register a function, call `falkor.register` and provide the name you wish to expose your function under, followed by either an anonymous function or the actual function.
105103

106-
e.g.
104+
For example:
107105
```javascript
108106
falkor.register('Area', Area);
109107
falkor.register('Perimeter', function(s) {return s.a + s.b + s.c});
@@ -146,7 +144,7 @@ Calling the command: `GRAPH.UDF LIST WITHCODE` will generate the following outpu
146144
147145
To remove a UDF library use either the `udf_delete` FalkorDB-PY function, or send a `GRAPH.UDF DELETE <library>` command via a direct connection to the database.
148146
149-
e.g.
147+
For example:
150148
```python
151149
from falkordb import FalkorDB
152150
@@ -158,7 +156,7 @@ db.udf_delete("Shapes")
158156
```
159157
160158
### GRAPH.UDF FLUSH
161-
Similar to delete `GRAPH.UDF FLUSH` removes **all** UDF libraries from the DB.
159+
Similar to delete `GRAPH.UDF FLUSH` removes **all** UDF libraries from the database.
162160
163161
```python
164162
from falkordb import FalkorDB
@@ -176,9 +174,9 @@ Scalar, Node, Edge & Path objects.
176174
177175
### Node
178176
In a UDF, a node object exposes its `ID`, `labels` and `attributes` via the corresponding properties:
179-
`id` - node internal ID
180-
`labels` - node's labels
181-
`attributes` - node's attributes
177+
- `id` - node internal ID
178+
- `labels` - node's labels
179+
- `attributes` - node's attributes
182180
183181
For example:
184182
```javascript
@@ -187,8 +185,7 @@ function stringify_node(n) {
187185
}
188186
```
189187
190-
It's also possible to collect a node's neighbors by calling the node's `getNeighbors` function.
191-
`getNeighbors` accept an optional config map:
188+
You can also collect a node's neighbors by calling the node's `getNeighbors` function. The getNeighbors function accepts an optional config map:
192189
193190
| config name | type | description | example |
194191
|------------|------|--------------------------------|------------------------------|
@@ -200,11 +197,11 @@ It's also possible to collect a node's neighbors by calling the node's `getNeigh
200197
### Edge
201198
In a UDF, an edge object exposes its `ID`, `type`, `startNode`,`endNode` and `attributes` via the corresponding properties:
202199
203-
`id` - edge internal ID
204-
`type` - edge's relationship type
205-
`startNode` - edge's start node
206-
`endNode` - edge's end node
207-
`attributes` - edge's attributes
200+
- `id` - edge internal ID
201+
- `type` - edge's relationship type
202+
- `startNode` - edge's start node
203+
- `endNode` - edge's end node
204+
- `attributes` - edge's attributes
208205
209206
For example:
210207
```javascript
@@ -220,9 +217,9 @@ function stringify_edge(e) {
220217
### Path
221218
In a UDF, a path object exposes its `nodes`, `length` and `relationships` via the corresponding properties:
222219
223-
`nodes` - path's nodes
224-
`length` - path's length
225-
`relationships` - path's edges
220+
- `nodes` - path's nodes
221+
- `length` - path's length
222+
- `relationships` - path's edges
226223
227224
For example:
228225
```javascript
@@ -234,12 +231,12 @@ function stringify_path(p) {
234231
```
235232
236233
## Advanced examples
237-
In this example we'll implement Jaccard similarity for nodes.
238-
Jaccard's formula J(A,B) = |A ∩ B| / |A ∪ B| = |A ∩ B| / (|A| + |B| - |A ∩ B|)
234+
In this example, we'll implement Jaccard similarity for nodes.
235+
Jaccard's formula is: J(A,B) = |A ∩ B| / |A ∪ B| = |A ∩ B| / (|A| + |B| - |A ∩ B|)
239236
240-
In simple words: to compute Jaccard similarity for two nodes A and B we'll compute the number of shared neighbors between them and divide it by the total number of neighbors. such that if A and B have the same neighbors then their similarity value would be 1 and in case they have no shared neighbors their similarity value is 0.
237+
In simple terms, to compute Jaccard similarity for two nodes A and B, compute the number of shared neighbors between them and divide it by the total number of neighbors. If A and B have the same neighbors, their similarity value is 1. If they have no shared neighbors, their similarity value is 0.
241238
242-
To start with let's define two UDFs: `union` and `intersection`, in a `collection.js` file:
239+
To start, define two UDFs (union and intersection) in a collection.js file:
243240
244241
```javascript
245242
function union (a, b) {
@@ -255,8 +252,7 @@ falkor.register('union', union);
255252
falkor.register('intersection', intersection);
256253
```
257254
258-
With these functions defined we can proceed implementing `Jaccard similarity`
259-
Create `similarity.js` as follows:
255+
With these functions defined, proceed to implement Jaccard similarity. Create `similarity.js` as follows:
260256
261257
```javascript
262258
function jaccard(a, b) {
@@ -272,9 +268,9 @@ function jaccard(a, b) {
272268
falkor.register('jaccard', jaccard);
273269
```
274270
275-
As you'll notice `jaccard` uses both `union` and `intersection` from `collection.js` but it also collects A's and B's neighbors via a call to `getNeighbors`
271+
Notice that jaccard uses both `union` and `intersection` from `collection.js`, and also collects A's and B's neighbors via a call to `getNeighbors`.
276272
277-
We're almost done, what's left is to load these UDFs libraries into FalkorDB and use them.
273+
The remaining step is to load these UDF libraries into FalkorDB and use them:
278274
279275
```python
280276
from falkordb import FalkorDB
@@ -344,10 +340,9 @@ Jaccard similarity between Alice and Alice is: 1
344340
```
345341
346342
### Custom Traversals
347-
In some situations where you want to have fine control over the way graph traversals are made, Cypher might not be flexible enough.
348-
Let's consider the following requirement, we would like to collect all reachable nodes from a given start node, a neighbor node is added to the expanded path if its `amount` value is greater than the accumulated sum of amounts on the current path.
343+
In some situations where you want fine control over the way graph traversals are made, Cypher might not be flexible enough. Consider the following requirement: collect all reachable nodes from a given start node, where a neighbor node is added to the expanded path if its amount value is greater than the accumulated sum of amounts on the current path.
349344
350-
Here's a UDF that accomplishes this traversal. It performs a DFS and only expands to neighbors whose `amount` value is greater than the accumulated sum of amounts along the current path:
345+
The following UDF accomplishes this traversal. It performs a DFS and only expands to neighbors whose `amount` value is greater than the accumulated sum of amounts along the current path:
351346
352347
353348
```javascript
@@ -388,7 +383,8 @@ function CollectIncreasingAmounts(n) {
388383
falkor.register('CollectIncreasingAmounts', CollectIncreasingAmounts);
389384
```
390385
391-
All that's left is to load this UDF:
386+
The remaining step is to load this UDF:
387+
392388
```python
393389
from falkordb import FalkorDB
394390
@@ -412,17 +408,15 @@ for node in reachables:
412408
413409
## FLEX
414410
415-
FLEX is FalkorDB's open source community UDF package, available at [github.com/FalkorDB/flex](https://github.com/FalkorDB/flex).
416-
It contains a variety of useful functionality, including:
411+
FLEX (FalkorDB Library of Extensions) is FalkorDB's open source community UDF package, available at [github.com/FalkorDB/flex](https://github.com/FalkorDB/flex).
417412
418-
1. String and set similarity metrics for fuzzy matching and comparison.
419-
2. Date and time manipulation, formatting, and parsing.
420-
3. Low-level bitwise operations on integers.
413+
It contains a variety of useful functionality, including:
414+
- String and set similarity metrics for fuzzy matching and comparison
415+
- Date and time manipulation, formatting, and parsing
416+
- Low-level bitwise operations on integers
421417
422-
We welcome contributions to extend this library with additional functionality.
418+
Contributions to extend this library with additional functionality are welcome.
423419
424420
## Limitations
425-
426-
Currently UDFs are not allowed to modify the graph in any way.
427-
You can't update graph entities within a UDF, nor can you add or delete entities.
421+
> Currently, UDFs are not allowed to modify the graph in any way. You cannot update graph entities within a UDF, nor can you add or delete entities.
428422

0 commit comments

Comments
 (0)