Elasticsearchphp中如何进行前缀查询?

How to do prefix query in Elasticsearch php?

我想在 PHP 中使用以下 JSON 查询:

{
    "match_phrase" : {
        "message" : {
            "query" : "this is a test",
            "analyzer" : "my_analyzer"
        }
    }
}

现在我有 PHP 代码:

 $params['body']['query']['match_phrase'] = array(
            "name" => $query
        );

$this->result = $this->client->search($params);

如何根据 elasticsearch php 将 JSON 查询转换为 PHP 数组查询?

一种方法是这样的:

$params['body']['query']['match_phrase'] = array(
    "message" => array(
        "query" => $query,
        "analyzer" => "my_analyzer"
    )
);

$this->result = $this->client->search($params);

另一种在使用 Elasticsearch 时可能更好的方法是使用 json_decode 函数。这样您就可以轻松地在 JSON 中使用 查询 DSL 而无需通过关联数组构造它。

$json = '{
    "match_phrase" : {
        "message" : {
            "query" : "' . $query . '",
            "analyzer" : "my_analyzer"
        }
    }
}';
$params['body']['query'] = json_decode($json);
$this->result = $this->client->search($params);