Skip to content

Commit 81b48a6

Browse files
thePanzruflin
authored andcommitted
Fix AnalyzeAPI with 'explain' parameter (#1254)
1 parent 035854e commit 81b48a6

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ All notable changes to this project will be documented in this file based on the
1414

1515
### Improvements
1616

17+
- added support for the "explain" flag of AnalyzeAPI [PR-1254](https://github.com/ruflin/Elastica/pull/1254)
18+
1719
### Deprecated
1820

1921
- Deprecated `\Elastica\Exception\ElasticsearchException` which is irrelevant since Elasticsearch now exposes the errors as a structured array instead of a single string.

lib/Elastica/Index.php

+6
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,12 @@ public function analyze($text, $args = [])
536536
{
537537
$data = $this->request('_analyze', Request::POST, $text, $args)->getData();
538538

539+
// Support for "Explain" parameter, that returns a different response structure from Elastic
540+
// @see: https://www.elastic.co/guide/en/elasticsearch/reference/current/_explain_analyze.html
541+
if (isset($args['explain']) && $args['explain']) {
542+
return $data['detail'];
543+
}
544+
539545
return $data['tokens'];
540546
}
541547
}

test/lib/Elastica/Test/IndexTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,18 @@ public function testAnalyze()
914914
$this->assertEquals($tokens, $returnedTokens);
915915
}
916916

917+
/**
918+
* @group functional
919+
*/
920+
public function testAnalyzeExplain()
921+
{
922+
$index = $this->_createIndex();
923+
$index->refresh();
924+
$data = $index->analyze('foo', ['explain' => true]);
925+
926+
$this->assertArrayHasKey('custom_analyzer', $data);
927+
}
928+
917929
/**
918930
* @group unit
919931
* @expectedException \Elastica\Exception\InvalidException

0 commit comments

Comments
 (0)