You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: udfs/index.md
+45-51Lines changed: 45 additions & 51 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,19 +7,18 @@ nav_order: 6
7
7
8
8
# UDFs
9
9
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.
14
14
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.
17
16
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.
19
18
20
19
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 followingcomplete 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.
23
22
24
23
```python
25
24
from falkordb import FalkorDB
@@ -57,13 +56,13 @@ print(f"s: {s}") # prints 'AbCdEf'
57
56
```
58
57
## Commands Specification
59
58
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.
61
60
62
61
### GRAPH.UDF LOAD [REPLACE] <Lib> <script>
63
62
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).
65
64
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:
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.
@@ -146,7 +144,7 @@ Calling the command: `GRAPH.UDF LIST WITHCODE` will generate the following outpu
146
144
147
145
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.
148
146
149
-
e.g.
147
+
For example:
150
148
```python
151
149
from falkordb import FalkorDB
152
150
@@ -158,7 +156,7 @@ db.udf_delete("Shapes")
158
156
```
159
157
160
158
### 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.
In simple words: to compute Jaccard similarity fortwo 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 andincasethey 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.
241
238
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:
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:
260
256
261
257
```javascript
262
258
function jaccard(a, b) {
@@ -272,9 +268,9 @@ function jaccard(a, b) {
272
268
falkor.register('jaccard', jaccard);
273
269
```
274
270
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`.
276
272
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:
278
274
279
275
```python
280
276
from falkordb import FalkorDB
@@ -344,10 +340,9 @@ Jaccard similarity between Alice and Alice is: 1
344
340
```
345
341
346
342
### 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.
349
344
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:
351
346
352
347
353
348
```javascript
@@ -388,7 +383,8 @@ function CollectIncreasingAmounts(n) {
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).
417
412
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
421
417
422
-
We welcome contributions to extend this library with additional functionality.
418
+
Contributions to extend this library with additional functionality are welcome.
423
419
424
420
## 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.
0 commit comments