如果没有应用所有过滤器怎么办?在 DOC 中使用 null FIELD VALUE 而非 NULL 值进行过滤

What if all filter not applied? filter with null FIELD VALUE not NULL value in DOC

我有搜索主题。教师可以添加多个科目,学生可以搜索这些科目并可以对结果数据应用过滤器。学生可以按级别、课程、地点进行筛选。

我的问题是如果学生没有应用 LOCATION 过滤器怎么办?

我在过滤器中传递位置字符串对我来说一切正常..但是当我不应用位置过滤器时..它没有为文本查询指定文本。

这是我的查询:

$client = ClientBuilder::create()->build();
$location = $request->location;
$level = $request->level;
$curriculum = $request->curriculum;
$params = [
    'index' => 'tutors',
    'body'  => 
    [
        "query" => [
            "bool" => [
                "must" => [
                    [ "term" => [ "approved" => 1 ]],
                    [ "match" => [ "country_en_name" =>  $location ]],
                    [ "match" => [ "local_rates.levels" =>  $level ]],
                    [ "match" => [ "local_rates.curriculums" =>  $curriculum ]],
                ]
            ],
        ]
    ]
];
$search = $client->search($params);

我也尝试过这样的 NULL 或 EMPTY 值

$client = ClientBuilder::create()->build();
$location = $request->location;
$level = $request->level;
$curriculum = $request->curriculum;
$params = [
    'index' => 'tutors',
    'body'  => 
    [
        "query" => [
            "bool" => [
                "must" => [
                    [ "term" => [ "approved" => 1 ]],
                    $location && $location != null ? [ "match" => [ "country_en_name" =>  $location ]] : '',
                    $level && $level != null ? [ "match" => [ "local_rates.levels" =>  $level ]] : '',
                    $curriculum && $curriculum != null ? [ "match" => [ "local_rates.curriculums" =>  $curriculum ]] : '',
                ]
            ],
        ]
    ]
];
$search = $client->search($params);

但是没有成功,谁能帮帮我?

您不能在 bool/must 查询中添加空查询。您需要逐步构建查询:

$params = [
    'index' => 'tutors',
    'body'  => 
    [
        "query" => [
            "bool" => [
                "must" => [
                    [ "term" => [ "approved" => 1 ]],
                ]
            ],
        ]
    ]
];

if ($location && $location != null) {
   $params['body']['query']['bool']['must'][] = [ "match" => [ "country_en_name" =>  $location ]];
}
if ($level && $level != null) {
   $params['body']['query']['bool']['must'][] = [ "match" => [ "local_rates.levels" =>  $level ]];
}
if ($curriculum && $curriculum != null) {
   $params['body']['query']['bool']['must'][] = [ "match" => [ "local_rates.curriculums" =>  $curriculum ]];
}


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