Skip to content

Commit d331f1a

Browse files
committed
added custom analyzers support
1 parent e624657 commit d331f1a

File tree

7 files changed

+540
-23
lines changed

7 files changed

+540
-23
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Added support for per-database default options (`replicationFactor`, `writeConce
1010
Added `maxRuntime` option to `Statement` class for automatically timing out queries on
1111
the server-side.
1212

13+
Added support for custom analyzers for arangosearch.
14+
1315

1416
Release notes for the ArangoDB-PHP driver 3.5.x
1517
===============================================

lib/ArangoDBClient/Analyzer.php

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?php
2+
3+
/**
4+
* ArangoDB PHP client: Analyzer class
5+
*
6+
* @package ArangoDBClient
7+
* @author Jan Steemann
8+
* @copyright Copyright 2019, ArangoDB GmbH, Cologne, Germany
9+
*
10+
* @since 3.6
11+
*/
12+
13+
namespace ArangoDBClient;
14+
15+
/**
16+
* Value object representing an analyzer
17+
*
18+
* <br>
19+
*
20+
* @package ArangoDBClient
21+
* @since 3.6
22+
*/
23+
class Analyzer
24+
{
25+
/**
26+
* The analyzer name
27+
*
28+
* @var string - analyzer name
29+
*/
30+
protected $_name;
31+
32+
/**
33+
* Analyzer name index
34+
*/
35+
const ENTRY_NAME = 'name';
36+
37+
/**
38+
* Analyzer type index
39+
*/
40+
const ENTRY_TYPE = 'type';
41+
42+
/**
43+
* Analyzer properties index
44+
*/
45+
const ENTRY_PROPERTIES = 'properties';
46+
47+
/**
48+
* Analyzer features index
49+
*/
50+
const ENTRY_FEATURES = 'features';
51+
52+
/**
53+
* Constructs an analyzer
54+
*
55+
* @param string $name - analyzer name
56+
* @param string $type - analyzer type
57+
* @param array $properties - analyzer properties
58+
* @param array $features - analyzer features
59+
*
60+
* @since 3.6
61+
*
62+
* @throws \ArangoDBClient\ClientException
63+
*/
64+
public function __construct($name, $type, array $properties = [], array $features = [])
65+
{
66+
$this->_name = $name;
67+
$this->_type = $type;
68+
$this->_properties = $properties;
69+
$this->_features = $features;
70+
}
71+
72+
/**
73+
* Return the analyzer name
74+
*
75+
* @return string - analyzer name
76+
*/
77+
public function getName()
78+
{
79+
return $this->_name;
80+
}
81+
82+
/**
83+
* Return the analyzer type
84+
*
85+
* @return string - analyzer type
86+
*/
87+
public function getType()
88+
{
89+
return $this->_type;
90+
}
91+
92+
/**
93+
* Return the analyzer properties
94+
*
95+
* @return array - analyzer properties
96+
*/
97+
public function getProperties()
98+
{
99+
return $this->_properties;
100+
}
101+
102+
/**
103+
* Return the analyzer features
104+
*
105+
* @return array - analyzer features
106+
*/
107+
public function getFeatures()
108+
{
109+
return $this->_features;
110+
}
111+
112+
/**
113+
* Return the analyzer as an array
114+
*
115+
* @return array - analyzer data as an array
116+
*/
117+
public function getAll()
118+
{
119+
return [
120+
self::ENTRY_NAME => $this->getName(),
121+
self::ENTRY_TYPE => $this->getType(),
122+
self::ENTRY_PROPERTIES => $this->getProperties(),
123+
self::ENTRY_FEATURES => $this->getFeatures(),
124+
];
125+
}
126+
}
127+
128+
class_alias(Analyzer::class, '\triagens\ArangoDb\Analyzer');
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
<?php
2+
3+
/**
4+
* ArangoDB PHP client: analyzer handler
5+
*
6+
* @package ArangoDBClient
7+
* @author Jan Steemann
8+
* @copyright Copyright 2019, ArangoDB GmbH, Cologne, Germany
9+
*
10+
* @since 3.6
11+
*/
12+
13+
namespace ArangoDBClient;
14+
15+
/**
16+
* A handler that manages analyzers.
17+
*
18+
* @package ArangoDBClient
19+
* @since 3.6
20+
*/
21+
class AnalyzerHandler extends Handler
22+
{
23+
/**
24+
* Create an analyzer
25+
*
26+
* This will create an analyzer using the given analyzer object and return an array of the created analyzer object's attributes.<br><br>
27+
*
28+
* @throws Exception
29+
*
30+
* @param Analyzer $analyzer - The analyzer object which holds the information of the analyzer to be created
31+
*
32+
* @return array
33+
* @since 3.6
34+
*/
35+
public function create(Analyzer $analyzer)
36+
{
37+
$params = [
38+
Analyzer::ENTRY_NAME => $analyzer->getName(),
39+
Analyzer::ENTRY_TYPE => $analyzer->getType(),
40+
Analyzer::ENTRY_FEATURES => $analyzer->getFeatures(),
41+
];
42+
43+
$properties = $analyzer->getProperties();
44+
if (count($properties) > 0) {
45+
$params[Analyzer::ENTRY_PROPERTIES] = $properties;
46+
}
47+
48+
$url = Urls::URL_ANALYZER;
49+
$response = $this->getConnection()->post($url, $this->json_encode_wrapper($params));
50+
$json = $response->getJson();
51+
52+
return $analyzer->getAll();
53+
}
54+
55+
/**
56+
* Get an analyzer
57+
*
58+
* This will get an analyzer.<br><br>
59+
*
60+
* @param string $analyzer - The name of the analyzer
61+
*
62+
* @return Analyzer|false
63+
* @throws \ArangoDBClient\ClientException
64+
* @since 3.6
65+
*/
66+
public function get($analyzer)
67+
{
68+
$url = UrlHelper::buildUrl(Urls::URL_ANALYZER, [$analyzer]);
69+
70+
$response = $this->getConnection()->get($url);
71+
$data = $response->getJson();
72+
73+
$result = new Analyzer(
74+
$data[Analyzer::ENTRY_NAME],
75+
$data[Analyzer::ENTRY_TYPE],
76+
$data[Analyzer::ENTRY_PROPERTIES],
77+
$data[Analyzer::ENTRY_FEATURES]
78+
);
79+
80+
return $result;
81+
}
82+
83+
/**
84+
* Get all analyzers<br><br>
85+
*
86+
* @throws Exception
87+
*
88+
* @return array - Returns an array of available analyzers
89+
* @since 3.6
90+
*/
91+
public function getAll()
92+
{
93+
$url = UrlHelper::buildUrl(Urls::URL_ANALYZER, []);
94+
$result = $this->getConnection()->get($url);
95+
96+
return $result->getJson();
97+
}
98+
99+
/**
100+
* Get an analyzer's properties<br><br>
101+
*
102+
* @throws Exception
103+
*
104+
* @param mixed $analyzer - analyzer name as a string or instance of Analyzer
105+
*
106+
* @return array - Returns an array of attributes. Will throw if there is an error
107+
* @since 3.6
108+
*/
109+
public function properties($analyzer)
110+
{
111+
if ($analyzer instanceof Analyzer) {
112+
$analyzer = $analyzer->getName();
113+
}
114+
115+
$url = UrlHelper::buildUrl(Urls::URL_ANALYZER, [$analyzer]);
116+
$result = $this->getConnection()->get($url);
117+
118+
return $result->getJson();
119+
}
120+
121+
/**
122+
* Drop an analyzer<br><br>
123+
*
124+
* @throws Exception
125+
*
126+
* @param mixed $analyzer- analyzer name as a string or instance of Analyzer
127+
*
128+
* @return bool - always true, will throw if there is an error
129+
* @since 3.6
130+
*/
131+
public function drop($analyzer)
132+
{
133+
if ($analyzer instanceof Analyzer) {
134+
$analyzer = $analyzer->getName();
135+
}
136+
137+
$url = UrlHelper::buildUrl(Urls::URL_ANALYZER, [$analyzer]);
138+
$this->getConnection()->delete($url);
139+
140+
return true;
141+
}
142+
143+
}
144+
145+
class_alias(AnalyzerHandler::class, '\triagens\ArangoDb\AnalyzerHandler');

lib/ArangoDBClient/Urls.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ abstract class Urls
4242
* URL base part for all view-related REST calls
4343
*/
4444
const URL_VIEW = '/_api/view';
45+
46+
/**
47+
* URL base part for all analyzer-related REST calls
48+
*/
49+
const URL_ANALYZER = '/_api/analyzer';
4550

4651
/**
4752
* URL part vertex-related graph REST calls

lib/ArangoDBClient/View.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class View
5454
/**
5555
* Constructs an empty view
5656
*
57-
* @param array $name - name for view
57+
* @param string $name - name for view
5858
* @param string $type - view type
5959
*
6060
* @since 3.4

0 commit comments

Comments
 (0)