简体   繁体   English

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

[英]Elasticsearch multiple term search Json Query builder with PHP

I am implementing ElasticSearch in the Codeigniter Framework. 我正在Codeigniter框架中实现ElasticSearch。 My hosting Company do not allow ES service in my managed server so I am testing the ES system by making use of the free option of Facetflow, to have ES on a remote server. 我的托管公司不允许在托管服务器中提供ES服务,因此我正在使用Facetflow的免费选项测试ES系统,以将ES安装在远程服务器上。

I am making use of a ES / Codeigniter library available here: https://github.com/confact/elasticsearch-codeigniter-library 我在这里使用ES / Codeigniter库: https : //github.com/confact/elasticsearch-codeigniter-library

I can do a simple search but am stuck to do a multiple Term search as I don't understand how to build the Query with PHP without making use of the ES Client PHP API. 我可以做一个简单的搜索,但是由于不了解如何在不使用ES Client PHP API的情况下使用PHP构建查询,因此只能进行多个术语搜索。

My functions to do the simple search is as follow: 我执行简单搜索的功能如下:

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)));
}

To call the search I make use of 调用搜索,我利用

query('','Do My Search');

to get: 要得到:

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

What needs to be done to build the multiple Term search query that creates the following? 建立多词搜索查询以创建以下内容需要做什么?

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" } } ] } } } } }'

You're not far off. 你不远。 Compose your query in JSON. 用JSON编写查询。 This can be passed as the body in a POST request. 可以在POST请求中将其作为正文传递。 The call() function already handles POST and body data. call()函数已经可以处理POST和主体数据。

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

Note: the Content-Type: application/json header is not required. 注意:不需要Content-Type: application/json标头。 You can actually omit the headers entirely and Elasticsearch will respond with JSON. 您实际上可以完全省略标题,Elasticsearch将使用JSON进行响应。

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

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