Koddial
Koddial

Reputation: 85

Elasticsearch completion suggester query in PHP

I was not able to find a working example on how to query Elasticsearch using the completion suggester in PHP (elasticsearch-php).

Querying via CURL, e.g.

curl -X POST 'localhost:9200/tstidx/_suggest?pretty' -d '{
"try" : {
    "text" : "a",
    "completion" : {
        "field" : "suggest"
    }
}
}'

works, so the only problem is the query part in PHP.

How do I use the API to query Elasticsearch using the completion suggester?

Upvotes: 1

Views: 3622

Answers (2)

Gregory Neely
Gregory Neely

Reputation: 111

Using the PHP elasticsearch API

    <?Php 
       include 'autoload.php';
       $elastic_search = new ElasticApiService();
       $search_params = array(
           'index' => 'my_index',
           'search_type' =>  'match',
           'search_key' => 'citynamefield',
           'search_value' => 'orlando'
       );
       $search_results = $elastic_search->searchElastic($search_params);
       echo '<pre>';
print_r($search_results);

Upvotes: 0

Val
Val

Reputation: 217464

The PHP ES client has a method called suggest that you can use for that purpose:

$params = [
    'index' => 'tstidx',
    'body' => [
        'try' => [
            'text' => 'a',
            'completion' => [ 'field' => 'suggest' ]
        ]
    ]
];

$client = ClientBuilder::create()->build();
$response = $client->suggest($params);

Upvotes: 2

Related Questions