php - 使用PHP的Elasticsearch多个术语搜索Json Query构建器

标签 php json codeigniter elasticsearch

我正在Codeigniter框架中实现ElasticSearch。
我的托管公司不允许托管服务器中提供ES服务,因此我正在使用Facetflow的免费选项来测试ES系统,以将ES安装在远程服务器上。

我正在使用此处提供的ES / Codeigniter库:
https://github.com/confact/elasticsearch-codeigniter-library

我可以做一个简单的搜索,但是由于不了解如何在不使用ES Client PHP API的情况下使用PHP构建查询,因此无法进行多次搜索。

我执行简单搜索的功能如下:

private function call($path, $method = 'GET', $data = null)
{
    if (!$this -> index) {
        throw new Exception('$this->index needs a value');
    }
    $url = $this -> server . '/' . $this -> index . '/' . $path;
    $headers = array('Accept: application/json', 'Content-Type: application/json', );
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    switch($method) {
        case 'GET' :
            break;
        case 'POST' :
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            break;
        case 'PUT' :
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
            break;
        case 'DELETE' :
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
            break;
    }
    $response = curl_exec($ch);
    $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    return json_decode($response, true);
}
public function query($type, $q)
{
    return $this -> call($type . '/_search?' . http_build_query(array('q' => $q)));
}

调用搜索,我利用
query('','Do My Search');

要得到:
curl -XGET 'http://domain/_search?q=Do+My+Search'

建立多词搜索查询以创建以下内容需要做什么?
curl -XGET https://domain/type/_search -d '{ "query": { "filtered": { "query": { "query_string": 
{ "query": "Car For Sale" } }, 
"filter": { "bool" : { "must" : [ 
{"term" : { "provid" : "5" } }, 
{"term" : { "areaid" : "16" } }, 
{"term" : { "catid" : "3" } } ] } } } } }'

最佳答案

你不远。用JSON编写查询。可以将其作为正文传递到POST请求中。 call()函数已经可以处理POST和主体数据。

$path = 'https://domain/type/_search';
$data = '{ "query": { "match_all": {} } }';
call($path, 'POST', $data);

注意:不需要Content-Type: application/json header 。实际上,您可以完全省略标题,Elasticsearch将使用JSON进行响应。

关于php - 使用PHP的Elasticsearch多个术语搜索Json Query构建器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33721478/

相关文章:

php - 使用 php 检测远程 url 的重定向

javascript - WordPress & suggest.js - Ajax 查询收到 200 响应但内容为空

json - 我们如何在golang中将json文件读取为json对象

带有 MySQL 数据库的 Android Lazy List Loader

php - Codeigniter cookies 没有设置?

php - mysql删除然后更新

PHP mysql charset utf8问题

java - 如何正确地将特定对象的列表转换为 Gson?

php - Codeigniter : Parse error: syntax error, 意外 'const' (T_CONST),Laravel 项目中需要变量 (T_VARIABLE)

php - 如何在 codeigniter 中创建用户时创建空行