Skip to content

Commit

Permalink
Add support of autocomplete endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
donhardman committed Jul 4, 2024
1 parent 0477b5c commit bc22ea5
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 1 deletion.
23 changes: 22 additions & 1 deletion docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Table of Contents

* [Bulk operations with documents](#bulk)

* [Autocomplete](#autocomplete)

* [Percolate searches](percolate.md)

* [Keyword helpers](queryhelpers.md)
Expand Down Expand Up @@ -192,4 +194,23 @@ $doc = [
$response = $client->bulk($doc);
```
<!-- proofread -->

### Autocomplete

For a complete reference of payload and response, see Manticore's [Autocomplete manual](https://manual.manticoresearch.com).

The request requires `query` and `table` defined as required parameters.

```
$request = [
[
'body' => [
'query' => 'hello wo',
'table' => 'testrt',
],
];
$response = $client->autocomplete($request);
```

<!-- proofread -->
12 changes: 12 additions & 0 deletions src/Manticoresearch/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,18 @@ public function keywords(array $params = []) {
return $response->getResponse();
}

/**
* Endpoint: autocomplete
* @param array $params
* @return mixed
*/
public function autocomplete(array $params = []) {
$endpoint = new Endpoints\Autocomplete($params);
$response = $this->request($endpoint);

return $response->getResponse();
}

public function explainQuery(array $params = []) {
$endpoint = new Endpoints\ExplainQuery();
$endpoint->setIndex($params['index']);
Expand Down
50 changes: 50 additions & 0 deletions src/Manticoresearch/Endpoints/Autocomplete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

// Copyright (c) Manticore Software LTD (https://manticoresearch.com)
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.

namespace Manticoresearch\Endpoints;

use Manticoresearch\Request;

/**
* Class Autocomplete
* @package Manticoresearch\Endpoints
*/
class Autocomplete extends Request
{
/**
* @return mixed|string
*/
public function getPath() {
return '/autocomplete';
}

/**
* @return mixed|string
*/
public function getMethod() {
return 'POST';
}

/**
* @return mixed|string
*/
public function getContentType() {
return 'application/json';
}

/**
* @param mixed $body
*/
public function setBody($body = null) {
if (is_array($body)) {
$this->body = json_encode($body, true);
} else {
$this->body = $body;
}
}

}
22 changes: 22 additions & 0 deletions test/Manticoresearch/Endpoints/AutocompleteTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

// Copyright (c) Manticore Software LTD (https://manticoresearch.com)
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.

namespace Manticoresearch\Test\Endpoints;

class AutocompleteTest extends \PHPUnit\Framework\TestCase
{

public function testGetPath() {
$replace = new \Manticoresearch\Endpoints\Autocomplete();
$this->assertEquals('/autocomplete', $replace->getPath());
}

public function testGetMethod() {
$replace = new \Manticoresearch\Endpoints\Autocomplete();
$this->assertEquals('POST', $replace->getMethod());
}
}

0 comments on commit bc22ea5

Please sign in to comment.