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
相关产品推荐
相关产品推荐

