Elasticsearch查询结果(PHP)

时间:2015-08-09 13:34:57

标签: php elasticsearch

鉴于文件的结构

{
    "id":""
    "field1":""
    "field2":""
    "field3":""
    "field4":""
 }

查询时

require 'composer/vendor/autoload.php';
$client = new Elasticsearch\Client(['hosts' => ['127.0.0.1:9200']]);

$value = $_POST['q'];

$query = $client->search(['body' => ['query' => ['bool' => ['should' => [
                    ['match' => ['title' => $value]],
                    ['match' => ['field1' => $value]],
                    ['match' => ['field2' => $value]],
                    ['match' => ['field3' => $value]],
                    ['match' => ['field4' => $value]]
                    ]]]]]);

if ($query['hits']['total'] >= 1) {
    $results = $query['hits']['hits'];
}

$ results返回所有字段。

如何让$ results只返回id,field1和field 4搜索所有文档的字段?

1 个答案:

答案 0 :(得分:0)

您只需在查询中使用_source filtering,就像这样

$query = $client->search(['body' => ['_source' => ['id', 'field1', 'field4'],
              'query' => ['bool' => ['should' => [
                    ['match' => ['title' => $value]],
                    ['match' => ['field1' => $value]],
                    ['match' => ['field2' => $value]],
                    ['match' => ['field3' => $value]],
                    ['match' => ['field4' => $value]]
                    ]]]]]);