ElasticSearch聚合查询badrequest400exception

7eumitmz  于 2021-06-13  发布在  ElasticSearch
关注(0)|答案(1)|浏览(697)

我试图在php代码中执行ElasticSearch查询,但出现以下错误:

Uncaught PHP Exception Elasticsearch\Common\Exceptions\BadRequest400Exception: 
"{"error":{"root_cause":[{"type":"named_object_not_found_exception","reason":"[1:834] 
unable to parse BaseAggregationBuilder with name [highlight_properties]: parser not found"}]

这是我的elasticsearch查询的aggs代码部分:

'aggs' => [
        'highlight_dealers' => [
            'terms' => [
                'field' => 'sorting.Id',
                'size' => 4
            ]
        ],
        'aggs' => [
            'highlight_properties' => [
                'terms' => [
                    'field' => '_id',
                    'size' => 2,
                ],
                'aggs' => [
                    'property' => [
                        'top_hits' => [
                            'size' => 1
                        ]
                    ],
                    'randomProperty' => [
                        'max' => [
                            'script' => [
                                'source' => 'new Random().nextInt()',
                                'lang' => 'painless'
                            ]
                        ]
                    ]
                ]
            ],
            'randomDealer' => [
                'avg' => [
                    'script' => [
                        'source' => 'Math.sin(109813 * doc["sorting.id"].value)',
                        'lang' => 'painless',
                    ]
                ]
            ]
        ]
    ];

也许我没有正确筑巢?

hxzsmxv2

hxzsmxv21#

尝试删除 aggs 从查询的这部分

'aggs' => [                                        <-- remove this
                'highlight_properties' => [
                    'terms' => [
                        'field' => '_id',
                        'size' => 2,
                    ],

新查询应如下所示:

'aggs' => [
        'highlight_dealers' => [
            'terms' => [
                'field' => 'sorting.Id',
                'size' => 4
            ]
        ],
            'highlight_properties' => [
                'terms' => [
                    'field' => '_id',
                    'size' => 2,
                ],
                'aggs' => [
                    'property' => [
                        'top_hits' => [
                            'size' => 1
                        ]
                    ],
                    'randomProperty' => [
                        'max' => [
                            'script' => [
                                'source' => 'new Random().nextInt()',
                                'lang' => 'painless'
                            ]
                        ]
                    ]
                ]
            ],
            'randomDealer' => [
                'avg' => [
                    'script' => [
                        'source' => 'Math.sin(109813 * doc["sorting.id"].value)',
                        'lang' => 'painless',
                    ]
                ]
        ]
    ];

由于我不了解您的用例,您甚至可以尝试将查询格式化为以下格式:

{
  "aggs": {
    "highlight_dealers": {
      "terms": {
        "field": "sorting.Id",
        "size": 4
      },                              <-- note the brackets here
      "aggs": {
        "highlight_properties": {
          "terms": {
            "field": "_id",
            "size": 2
          },
          "aggs": {
            "property": {
              "top_hits": {
                "size": 1
              }
            },
            "randomProperty": {
              "max": {
                "script": {
                  "source": "new Random().nextInt()",
                  "lang": "painless"
                }
              }
            }
          }
        }
      }
    }
  }
}

相关问题