正确查询elasticsearch php库

时间:2016-06-03 11:58:16

标签: php elasticsearch elasticsearch-2.0

我正在使用php library for elasticsearch并且我正在使用count方法从搜索中获取计数,以避免获取所有结果。但在查询elasticsearch服务器时,我似乎没有使用正确的格式。

以下是我正在做的事情

        $hosts = ['localhost:9200'];
        $client = ClientBuilder::create()
        ->setHosts($hosts)
        ->build();


        $params = [
        'index' => 'logstash-*',
        'type'  => 'INFO',
        'body'  => [
            'query' => [
                'bool' => [
                    "should" => [
                        ["term" => ["tags" => "producer"]],
                        ["term" => ["tags" => "statistics"]],
                        ["term" => ["message" => "view"]],
                        ["term" => ["context.id" => 1]]
                    ]
                ]
            ]
        ]
    ];


$response = $client->search($params); // was returning all the results too, I was wrong
$response = $client->count($params); // return a count of all my documents in elasticsearch instance, that's wrong

固定thanx到@Val我设置最小值应该匹配为4并且它可以工作!

    array (size=4)
  'took' => int 14
  'timed_out' => boolean false
  '_shards' => 
    array (size=3)
      'total' => int 10
      'successful' => int 10
      'failed' => int 0
  'hits' => 
    array (size=3)
      'total' => int 11
      'max_score' => float 7.852423
      'hits' => 
        array (size=10)
          0 => 
            array (size=5)
              ...
          1 => 
            array (size=5)
              ...
          2 => 
            array (size=5)
              ...
          3 => 
            array (size=5)
              ...
          4 => 
            array (size=5)
              ...
          5 => 
            array (size=5)
              ...
          6 => 
            array (size=5)
              ...
          7 => 
            array (size=5)
              ...
          8 => 
            array (size=5)
              ...
          9 => 
            array (size=5)
              ...
array (size=2)
  'count' => int 11
  '_shards' => 
    array (size=3)
      'total' => int 10
      'successful' => int 10
      'failed' => int 0

如何获得查询的正确计数?

1 个答案:

答案 0 :(得分:0)

我用正确的答案编辑了这个问题。 最后的查询是:

$params = [
    'index' => 'logstash-*',
    'type'  => 'INFO',
    'body'  => [
        'query' => [
            'bool' => [
                "should" => [
                    ["term" => ["tags" => "producer"]],
                    ["term" => ["tags" => "statistics"]],
                    ["term" => ["message" => "view"]],
                    ["term" => ["context.id" => 1]]
                ],
               "minimum_should_match" => 4
            ]
        ]
    ]
];