Oihana PHP Arango

levenshteinMatch.php

Table of Contents

Functions

levenshteinMatch()  : string
Match documents within a (Damerau-)Levenshtein distance of a target string.

Functions

levenshteinMatch()

Match documents within a (Damerau-)Levenshtein distance of a target string.

levenshteinMatch(string $path, string $target, int $distance[, bool|null $transpositions = null ][, int|null $maxTerms = null ][, string|null $prefix = null ]) : string

Wraps the ArangoDB AQL function LEVENSHTEIN_MATCH(path, target, distance, transpositions, maxTerms, prefix). By default a Damerau-Levenshtein distance is computed (transpositions count as one operation); pass transpositions: false for a pure Levenshtein distance. The maximum distance is 4 without transpositions and 3 with them.

AQL arguments are positional: when a later option is provided, the helper fills the earlier omitted ones with the official server defaults (transpositions = true, maxTerms = 64) so callers never need to know them. Trailing omitted options are not emitted at all. When using $prefix, the prefix must be removed from $target (the distance is computed on the remainders — see the official documentation).

Example AQL usage:

LEVENSHTEIN_MATCH(doc.text, "quikc", 2, false)        // pure Levenshtein, matches "quick"
LEVENSHTEIN_MATCH(doc.text, "kc", 1, false, 64, "qui") // prefix search
Parameters
$path : string

Attribute path expression to test (kept raw).

$target : string

String to compare against (emitted as a quoted string literal).

$distance : int

Maximum edit distance: 0…4 if $transpositions is false, 0…3 otherwise.

$transpositions : bool|null = null

Optional — false for a pure Levenshtein distance (server default true).

$maxTerms : int|null = null

Optional — number of most relevant terms to consider, 0 for all (server default 64).

$prefix : string|null = null

Optional — known common prefix (emitted as a quoted string literal); improves performance.

Tags
example
use function oihana\arango\db\functions\search\levenshteinMatch;

echo levenshteinMatch( 'doc.text' , 'quikc' , 1 ) ;
// 'LEVENSHTEIN_MATCH(doc.text,"quikc",1)'

echo levenshteinMatch( 'doc.text' , 'quikc' , 2 , false ) ;
// 'LEVENSHTEIN_MATCH(doc.text,"quikc",2,false)'

echo levenshteinMatch( 'doc.text' , 'kc' , 1 , false , prefix: 'qui' ) ;
// 'LEVENSHTEIN_MATCH(doc.text,"kc",1,false,64,"qui")'
see
https://docs.arangodb.com/stable/aql/functions/arangosearch/#levenshtein_match
since
1.2.0
author

Marc Alcaraz

Return values
string

The formatted AQL expression.

On this page

Search results