简体   繁体   English

Laravel上的Elasticsearch聚合函数错误

[英]Elasticsearch aggregate function error on laravel

Elasticsearch gives error when integrating with laravel using aggregations. 当使用聚合与laravel集成时,Elasticsearch会给出错误。 This is my code: 这是我的代码:

        $laws_y = Law::searchByQuery([
            'multi_match' => [
                'query' => $years,
                'fields' => ["law_year"]
            ],
            "aggs" => [
                "group_by_law_year" => ["terms" => ['field' => ["law_year"]]]
            ]
        ]);

I get following error: 我收到以下错误:

BadRequest400Exception in GuzzleConnection.php line 277: {"error":{"root_cause":[{"type":"parse_exception","reason":"failed to parse search source. expected field name but got [START_OBJECT]"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"default","node":"BcRQOVhkS1SwTlvYPCEfHg","reason":{"type":"parse_exception","reason":"failed to parse search source. expected field name but got [START_OBJECT]"}}]},"status":400} GuzzleConnection.php第277行中的BadRequest400Exception:{“错误”:{“ root_cause”:[{“类型”:“ parse_exception”,“原因”:“无法解析搜索源。期望的字段名称,但是得到了[START_OBJECT]”}] ,“ type”:“ search_phase_execution_exception”,“ reason”:“所有分片均失败”,“ phase”:“查询”,“ grouped”:true,“ failed_shards”:[{“ shard”:0,“ index”:“ default“,” node“:” BcRQOVhkS1SwTlvYPCEfHg“,” reason“:{” type“:” parse_exception“,” reason“:”无法解析搜索源。期望的字段名称,但是得到了[START_OBJECT]“}}]}},”状态“:400}

Does anyone know the solution? 有人知道解决方案吗?

From the Elasticquent documentation , the searchByQuery function takes the following parameters (see source here ): Elasticquent文档中searchByQuery函数采用以下参数(请参见此处的源代码 ):

  • query - Your ElasticSearch Query query -您的ElasticSearch查询
  • aggregations - The Aggregations you wish to return. aggregations -您希望返回的聚合。
  • sourceFields - Limits returned set to the selected fields only sourceFields返回的限制仅设置为所选字段
  • limit - Number of records to return limit -要返回的记录数
  • offset - Sets the record offset (use for paging results) offset设置记录偏移量(用于分页结果)
  • sort - Your sort query sort您的排序查询

In your call, you need to separate the query (first parameter) from the aggregations (second parameter). 在调用中,您需要将查询(第一个参数)与聚合(第二个参数)分开。 Do it like this instead: 改为这样做:

     $laws_y = Law::searchByQuery([
        'multi_match' => [
            'query' => $years,
            'fields' => ["law_year"]
        ]
     ],
     [
        "group_by_law_year" => ["terms" => ['field' => "law_year"]]
     ]);

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

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