0
votes

I'm using the PHP client. I can search no problem.

My problem is getting suggestions. Here is my code:

$json = '
POST /my_search_engine/_suggest
{
  "my_engine":{
    "text":"'.$keyword.'",
    "completion":{
      "field":"title_suggest"
    }
  }
}';

$params ['body'] = $json;
$response = $client->suggest ( $params );

Here is the stack trace:

Fatal error: Uncaught exception 'Elasticsearch\Common\Exceptions\BadRequest400Exception' with message '{"error":{"root_cause":[{"type":"parse_exception","reason":"Failed to derive xcontent"}],"type":"parse_exception","reason":"Failed to derive xcontent"},"status":400}' in /var/www/ats-site-lib/ats-site-thoracic/web/app/es_search/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php:681 Stack trace:
#0 /var/www/ats-site-lib/ats-site-thoracic/web/app/es_search/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php(659): Elasticsearch\Connections\Connection->tryDeserializeError(Array, 'Elasticsearch\C...')
#1 /var/www/ats-site-lib/ats-site-thoracic/web/app/es_search/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php(579): Elasticsearch\Connections\Connection->tryDeserialize400Error(Array)
#2 /var/www/ats-site-lib/ats-site-thoracic/web/app/es_search/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php(261): Elasticsearch in /var/www/ats-site-lib/ats-site-thoracic/web/app/es_search/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php on line 682

Here is how a document's "title_suggest" completion field looks. I am storing an array of keywords. maybe this is the problem? :

"title_suggest": [ "Membership", "Renewal" ]

1
You need to remove the line POST /my_search_engine/_suggest from your $json variable.Val
Thank you! That did the trick. I was hoping is was something so simple.user1098063

1 Answers

0
votes

I had to make the JSON valid by removing POST /my_search_engine/_suggest from the string. Thanks to @Val

This is the end result:

$json = '
{
  "thoracic":{
    "text":"'.$keyword.'",
    "completion":{
      "field":"title_suggest"
    }
  }
}';