You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP Elasticsearch客户端使用function_score出现Unknown key报错求助

问题根因

触发parsing_exception的核心原因是查询结构不符合Elasticsearch DSL规范:function_score是查询类型的一种,必须嵌套在请求体body下的query字段中,不能直接作为body的一级参数。

修复后代码
$client = ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
$params['index'] = 'providers_wow';

$params['body'] = [
    // 新增query层级包裹function_score
    'query' => [
        'function_score' => [
            'query' => [
                'multi_match' => [
                    'query' => $_GET['search'],
                    'fuzziness' => 2,
                    'fields' => ['services', 'specialities', 'full_name', 'primary_speciality']
                ]
            ],
            'score_mode' => 'multiply',
            'functions' => [
                [
                    'filter' => [
                        'term' => [
                            'node_type' => 'S'
                        ]
                    ],
                    'weight' => 4
                ],
                [
                    'filter' => [
                        'term' => [
                            'node_type' => 'SP'
                        ]
                    ],
                    'weight' => 5
                ],
                [
                    'filter' => [
                        'term' => [
                            'node_type' => 'P'
                        ]
                    ],
                    'weight' => 1
                ],
            ]
        ]
    ]
];
$params['from'] = 0;
$params['size'] = 100;
$params['_source'] = ["full_name", "services", "specialities", "node_type", "primary_speciality"];

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

内容的提问来源于stack exchange,提问作者Umer Javed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 09:39:00