drjtua5953 2017-11-09 20:50
浏览 66
已采纳

PHP期望[END_OBJECT]但得到[FIELD_NAME],可能有太多查询子句

Elasticsearch 2.4.5 PHP 7.0

I have the following aggregation query that works via curl but fails when I convert to PHP. I feel like I'm missing something stupid/easy and just looking for another set of eyes

curl -XPOST "http://localhost:9200/_search" -d '
{
  "size": 1,
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": [
            {"type": { "value": "web_logs" }},
            {"range": {
              "@timestamp": {
                "gte": "2017-10-01T00:00:00.000" ,
                "lt": "2017-11-01T00:00:00.000"
              }
            }}
          ]
        }
      }
    }
  },
  "aggs": {
    "by_company": {
      "terms": {
        "field": "company.raw"
      },
      "aggs": {
        "total_bytes": {
          "sum": {
            "field": "sc_bytes"
          }
        }
      }
    }
  }
}' 

But when I try to convert it to PHP I get an error

<?php
ini_set('display_errors', 0);

use Elasticsearch\ClientBuilder;

require 'vendor/autoload.php';

$hosts = [
  'localhost:9200'
];


$client = ClientBuilder::create()
  ->setHosts($hosts)
  ->build();

$params['body'] = [
  'size' => 1,
  'query' => [
    'filtered' => [
      'filter' => [
        'bool' => [
          'must' => [
            'type' => [
              'value' => 'web_logs'
            ],
            'range' => [
              '@timestamp' => [
                'gte' => '2017-10-01T00:00:00.000',
                'lt' => '2017-11-01T00:00:00.000'
              ]
            ]
          ],
        ]
      ]
    ]
  ],
  'aggs' => [
    'by_company' => [
      'terms' => [
        'field' => 'company.raw'
      ],
      'aggs' => [
        'total_bytes' => [
          'sum' => [
            'field' => 'sc_bytes'
          ]
        ]
      ]
    ]
  ]
];

$results = $client->search($params);

Here is the error

$ php report2.php
PHP Fatal error:  Uncaught Elasticsearch\Common\Exceptions\BadRequest400Exception: {"error":{"root_cause":[{"type":"query_parsing_exception","reason":"expected [END_OBJECT] but got [FIELD_NAME], possibly too many query clauses","index":"logstash-2017.09.11","line":1,"col":92},{"type":"query_parsing_exception","reason":"expected [END_OBJECT] but got [FIELD_NAME], possibly too many query clauses","index":"logstash-2017.09.12","line":1,"col":92},{"type":"query_parsing_exception","reason":"expected [END_OBJECT] but got [FIELD_NAME], possibly too many query clauses","index":"logstash-2017.09.13","line":1,"col":92},{"type":"query_parsing_exception","reason":"expected [END_OBJECT] but got [FIELD_NAME], possibly too many query clauses","index":"logstash-2017.09.14","line":1,"col":92},{"type":"query_parsing_exception","reason":"expected [END_OBJECT] but got [FIELD_NAME], possibly too many query clauses","index":"logstash-2017.09.15","line":1,"col":92},{"type":"query_parsing_exception","reason":"expected [END_OBJECT] but got [FIELD_NAME in /mnt/c/Users/Chris/code/logstash/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php on line 610
  • 写回答

1条回答 默认 最新

  • dongxu7408 2017-11-10 04:50
    关注

    Your bool/must clause needs to be an array, so you're missing a few angle brackets:

        'bool' => [
          'must' => [
       ->   [
              'type' => [
                'value' => 'web_logs'
              ]
       ->   ],
       ->   [
              'range' => [
                '@timestamp' => [
                  'gte' => '2017-10-01T00:00:00.000',
                  'lt' => '2017-11-01T00:00:00.000'
                ]
              ]
       ->   ]
          ],
        ]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算