简体   繁体   English

PHP 中的 Elasticsearch 完成建议查询

[英]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).我无法找到有关如何使用 PHP 中的完成建议器 (elasticsearch-php) 查询 Elasticsearch 的工作示例。

Querying via CURL, eg通过 CURL 查询,例如

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.有效,所以唯一的问题是 PHP 中的查询部分。

How do I use the API to query Elasticsearch using the completion suggester?如何使用 API 使用完成建议器查询 Elasticsearch?

The PHP ES client has a method called suggest that you can use for that purpose: PHP ES 客户端有一个名为suggest的方法,您可以为此目的使用它:

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

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

Using the PHP elasticsearch API使用 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM