Skip to content

Commit 1e6d96f

Browse files
feat[indexes]: add inverted index implementation
1 parent ca1561c commit 1e6d96f

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

src/Collection/Index/Factory.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ public static function factory(array $attributes): IndexInterface
6262
return new TTLIndex($attributes['fields'], $attributes['expireAfter'], $attributes);
6363
}
6464

65+
if ($attributes['type'] === 'inverted') {
66+
return new InvertedIndex($attributes['fields'], $attributes);
67+
}
68+
6569
$class = $indexes[$attributes['type']];
6670
return new $class($attributes['fields'], $attributes);
6771
}

src/Collection/Index/Index.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class Index implements IndexInterface
8585
/**
8686
* Index constructor.
8787
*
88-
* @param string $type The index type. Must be one of following values: 'fulltext', 'general', 'geo', 'hash', 'persistent', 'skiplist' or 'ttl'
88+
* @param string $type The index type. Must be one of following values: 'fulltext', 'general', 'geo', 'hash', 'persistent', 'skiplist', 'inverted' or 'ttl'
8989
* @param array $fields An array of attribute names. Normally with just one attribute.
9090
*
9191
* @param array $attributes
@@ -97,10 +97,20 @@ public function __construct(string $type, array $fields, array $attributes = [])
9797
throw new InvalidParameterException("type", $type);
9898
}
9999

100+
$fieldNames = [];
101+
100102
foreach ($fields as $key => $field) {
101-
if (!is_string($field)) {
102-
throw new InvalidParameterException("fields[$key]", $field);
103+
if (is_string($field)) {
104+
array_push($fieldNames, $field);
105+
continue;
106+
}
107+
108+
if (is_array($field)) {
109+
array_push($fieldNames, $field['name']);
110+
continue;
103111
}
112+
113+
throw new InvalidParameterException("fields[$key]", $field);
104114
}
105115

106116
$this->type = $type;

src/Collection/Index/InvertedIndex.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,21 @@
1010
* @package ArangoDB\Collection\Index
1111
* @author Lucas S. Vieira
1212
*/
13-
class InvertedIndex extends Index
13+
final class InvertedIndex extends Index
1414
{
15+
/**
16+
* Default options for inverted index
17+
*
18+
* @var array
19+
*/
20+
protected array $defaultOptions = [
21+
'unique' => true,
22+
'sparse' => true,
23+
'analyzer' => "identity",
24+
'inBackground' => true, // Keep the collection available for writes during the index creation
25+
'parallelism' => 2,
26+
];
27+
1528
/**
1629
* InvertedIndex constructor
1730
*

0 commit comments

Comments
 (0)