Skip to content

Commit

Permalink
Merge pull request #41 from AlexandreToyer/3.0
Browse files Browse the repository at this point in the history
Add support for Analyzers API
  • Loading branch information
AlexandreToyer committed Oct 23, 2014
2 parents 0f7a30b + 0213300 commit 91aba73
Show file tree
Hide file tree
Showing 7 changed files with 301 additions and 1 deletion.
84 changes: 84 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ foreach($results as $key => $result) {
* [Get details of a specific field](#get-details-of-a-specific-field)
* [Delete a field](#delete-a-field)
* [Set default and unique field for an index](#set-default-and-unique-field-for-an-index)
* **[Analyzers](#analyzers)**
* [Create an analyzer](#create-an-analyzer)
* [Get list of analyzers](#get-list-of-analyzers)
* [Get details of a specific analyzer](#get-details-of-a-specific-analyzer)
* **[Web crawler](#web-crawler)**
* [Patterns](#patterns)
* _[Insert inclusion patterns](#insert-inclusion-patterns)_
Expand Down Expand Up @@ -460,6 +464,7 @@ foreach($response as $key => $item) {
* OpenSearchServer\Field\GetList
* OpenSearchServer\SearchTemplate\GetList
* OpenSearchServer\MoreLikeThis\GetList
* OpenSearchServer\Analyzer\GetList
* OpenSearchServer\Synonyms\GetList
* OpenSearchServer\Crawler\Rest\GetList
* OpenSearchServer\Crawler\Web\Patterns\Exclusion\GetList
Expand Down Expand Up @@ -835,6 +840,85 @@ Available methods:
* **defaultField(string $name)**: name of field that must be used as default field. Default field is used for search queries when no particular field is configured in the query. Empty value removes default field setting.
* **uniqueField(string $name)**: name of field that must be used as unique field. Unique field is used as a primary key. Empty value removes unique field setting.

## Analyzers

### Create an analyzer

[Go to API documentation for this method](http://www.opensearchserver.com/documentation/api_v2/analyzers/create_update.html)

Analyzer can be created or updated using some JSON Text or JSON array of values with object of type `OpenSearchServer\Analyzer\Create`.

```php
$json = <<<JSON
{
"queryTokenizer":{"name":"KeywordTokenizer"},
"indexTokenizer":{"name":"KeywordTokenizer"},
"filters":[
{
"name":"ShingleFilter",
"properties":{
"max_shingle_size":"5",
"token_separator":" ",
"min_shingle_size":"1"
},
"scope":"QUERY_INDEX"
},
{
"name":"PrefixSuffixStopFilter",
"properties":{
"prefixList":"English stop words",
"ignore_case":"true",
"token_separator":" ",
"suffixList":"English stop words"
},
"scope":"QUERY_INDEX"
}
]
}
JSON;
$request = new OpenSearchServer\Analyzer\Create(null, $json);
$request->index('index_name')
->name('TestAnalyzer');
$response = $oss_api->submit($request);
var_dump($response->isSuccess());
```

Available methods:

* **name(string $name)**: name of the analyzer to create or update.
* **lang(string $lang)**: lang of the analyzer to create or update.

### Get list of analyzers

[Go to API documentation for this method](http://www.opensearchserver.com/documentation/api_v2/analyzers/list.html)

```php
$request = new OpenSearchServer\Analyzer\GetList();
$request->index('index_name');
$response = $oss_api->submit($request);
foreach($response as $key => $analyzer) {
echo $analyzer->name . ' - ' . $analyzer->lang. '<br/>';
}
```

### Get details of a specific analyzer

[Go to API documentation for this method](http://www.opensearchserver.com/documentation/api_v2/analyzers/get.html)

```php
$request = new OpenSearchServer\Analyzer\Get();
$request->index('index_name')
->name('TextAnalyzer')
->lang(OpenSearchServer\Request::LANG_FR);
$response = $oss_api->submit($request);
```

Available methods:

* **name(string $name)**: name of the analyzer to get information for.
* **lang(string $lang)**: lang of the analyzer to get information for.


## Web Crawler

### Patterns
Expand Down
73 changes: 72 additions & 1 deletion examples/oss_examples.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
}
echo '</ul>';


/**
* ## Index\Create
* Create index
Expand Down Expand Up @@ -653,6 +652,78 @@
print_r($item);
}



/**
* ## Analyzer\GetList
* Get list of analyzers
*/
echo '<hr/><h2>Analyzer\GetList</h2>';
$request = new OpenSearchServer\Analyzer\GetList();
$request->index('web_index');
$response = $oss_api->submit($request);
var_dump($response->isSuccess());
foreach($response as $analyzer) {
echo $analyzer->name . ' - ' . $analyzer->lang. '<br/>';
}

/**
* ## Analyzer\Get
* Get an analyzer
*/
echo '<hr/><h2>Analyzer\Get</h2>';
$request = new OpenSearchServer\Analyzer\Get();
$request->index('web_index')
->name('SortingAnalyzer');
$response = $oss_api->submit($request);
var_dump($response->isSuccess());
echo '<h3>IndexTokenizer</h3>';
var_dump($response->getJsonValues()->analyzer->indexTokenizer);
echo '<h3>QueryTokenizer</h3>';
var_dump($response->getJsonValues()->analyzer->queryTokenizer);
echo '<h3>Filters</h3>';
foreach($response->getJsonValues()->analyzer->filters as $filter) {
var_dump($filter);
}

/**
* ## Analyzer\Get
* Get an analyzer
*/
echo '<hr/><h2>Analyzer\Create</h2>';
$json = <<<JSON
{
"queryTokenizer":{"name":"KeywordTokenizer"},
"indexTokenizer":{"name":"KeywordTokenizer"},
"filters":[
{
"name":"ShingleFilter",
"properties":{
"max_shingle_size":"5",
"token_separator":" ",
"min_shingle_size":"1"
},
"scope":"QUERY_INDEX"
},
{
"name":"PrefixSuffixStopFilter",
"properties":{
"prefixList":"English stop words",
"ignore_case":"true",
"token_separator":" ",
"suffixList":"English stop words"
},
"scope":"QUERY_INDEX"
}
]
}
JSON;
$request = new OpenSearchServer\Analyzer\Create(null, $json);
$request->index('web_index')
->name('TestAnalyzer');
$response = $oss_api->submit($request);
var_dump($response->isSuccess());

/**
* ## MoreLikeThis\Create
* Create a more like this template
Expand Down
57 changes: 57 additions & 0 deletions src/OpenSearchServer/Analyzer/Create.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
namespace OpenSearchServer\Analyzer;

use OpenSearchServer\RequestJson;
use OpenSearchServer\Request;

class Create extends RequestJson
{
public function __construct(array $jsonValues = null, $jsonText = null) {
$this->lang(Request::LANG_UNDEFINED);
parent::__construct($jsonValues, $jsonText);
}

/**
* Specify the name of analyzer
* @param string $name
* @return OpenSearchServer\Analyzer\Create
*/
public function name($name) {
$this->options['name'] = $name;
return $this;
}

/**
* Specify the lang of analyzer
* @param string $lang
* @return OpenSearchServer\Analyzer\Create
*/
public function lang($lang) {
$this->options['lang'] = $lang;
return $this;
}


/******************************
* INHERITED METHODS OVERRIDDEN
******************************/
/**
* {@inheritdoc}
*/
public function getMethod()
{
return self::METHOD_PUT;
}

/**
* {@inheritdoc}
*/
public function getPath()
{
$this->checkPathIndexNeeded();
if(empty($this->options['name'])) {
throw new \Exception('Method "name($name)" must be called before submitting request.');
}
return rawurlencode($this->options['index']).'/analyzer/'.rawurlencode($this->options['name']).'/lang/'.rawurlencode($this->options['lang']);
}
}
55 changes: 55 additions & 0 deletions src/OpenSearchServer/Analyzer/Get.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
namespace OpenSearchServer\Analyzer;

use OpenSearchServer\Request;

class Get extends Request
{
public function __construct(array $jsonValues = null, $jsonText = null) {
$this->lang(Request::LANG_UNDEFINED);
parent::__construct($jsonValues, $jsonText);
}

/**
* Specify the name of analyzer
* @param string $name
* @return OpenSearchServer\Analyzer\Create
*/
public function name($name) {
$this->options['name'] = $name;
return $this;
}

/**
* Specify the lang of analyzer
* @param string $lang
* @return OpenSearchServer\Analyzer\Create
*/
public function lang($lang) {
$this->options['lang'] = $lang;
return $this;
}

/******************************
* INHERITED METHODS OVERRIDDEN
******************************/
/**
* {@inheritdoc}
*/
public function getMethod()
{
return self::METHOD_GET;
}

/**
* {@inheritdoc}
*/
public function getPath()
{
$this->checkPathIndexNeeded();
if(empty($this->options['name'])) {
throw new \Exception('Method "name($name)" must be called before submitting request.');
}
return rawurlencode($this->options['index']).'/analyzer/'.rawurlencode($this->options['name']).'/lang/'.rawurlencode($this->options['lang']);
}
}
25 changes: 25 additions & 0 deletions src/OpenSearchServer/Analyzer/GetList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
namespace OpenSearchServer\Analyzer;

use OpenSearchServer\Request;

//List being a PHP reserved word this class is named GetList
class GetList extends Request
{
/**
* {@inheritdoc}
*/
public function getMethod()
{
return self::METHOD_GET;
}

/**
* {@inheritdoc}
*/
public function getPath()
{
$this->checkPathIndexNeeded();
return rawurlencode($this->options['index']).'/analyzer';
}
}
1 change: 1 addition & 0 deletions src/OpenSearchServer/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Request
/**
* Languages
*/
const LANG_UNDEFINED = 'UNDEFINED';
const LANG_FR = 'FRENCH';
const LANG_EN = 'ENGLISH';
const LANG_DE = 'GERMAN';
Expand Down
7 changes: 7 additions & 0 deletions src/OpenSearchServer/Response/ResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ public static function createResponse(BuzzResponse $response, \OpenSearchServer\
}
return $response;
break;
case 'OpenSearchServer\Analyzer\GetList':
$response = new ResponseIterable($response, $request);
if(!empty($response->getJsonValues()->analyzers)) {
$response->setValues($response->getJsonValues()->analyzers);
}
return $response;
break;
case 'OpenSearchServer\Crawler\Web\Patterns\Exclusion\GetList':
case 'OpenSearchServer\Crawler\Web\Patterns\Inclusion\GetList':
case 'OpenSearchServer\Autocompletion\GetList':
Expand Down

0 comments on commit 91aba73

Please sign in to comment.