gpt4 book ai didi

php - 将多个过滤器添加到ElasticsearchPhp中的现有过滤器

转载 作者:行者123 更新时间:2023-12-02 22:31:51 24 4
gpt4 key购买 nike

我是初学者,正在研究Elasticsearch php,以进行简单的搜索。
我必须根据天气情况有条件地设置过滤器,无论是否选择过滤器。
因此,我为该特定任务创建了过滤器,并将其放置在不同的文件中。我要做的就是在isset返回true时包括这些文件。
但是麻烦在于,如果选择了多个过滤器,那么最后一个过滤器将覆盖数组的过滤器部分,并且无法提供所需的多个过滤后的答案。反正有做这项工作吗?
(如果我是初学者,只是想学习,如果效率低下,请原谅我。)

我的文件是这样的

BalFilter.php

<?php

if ($val == 1) {
$params['body']['query']['filtered']['filter'] = ['range' => [
'balance' => ['gte' => $bal]
]
];
}
if ($val == 2) {
$params['body']['query']['filtered']['filter'] = ['range' => [
'balance' => ['lte' => $bal]
]
];
}

AgeFilter.php
<?php
$params['body']['query']['filtered']['filter']['term'] = ['age' => $age];

OnlyQuery.php
<?php 
$params['body']['query']['filtered']['query'] = [ 'query_string' => [
'default_field' => '_all',
'query' => $q,
'lenient' => true
]
];

而在主要指标
if (isset($_GET['age'])) {
$age = $_GET['age'];
} else {
$age = 0;
}
if (isset($_GET['val'])) {
$val = $_GET['val'];
} else {
$val = 0;
}
if (isset($_GET['bal'])) {
$bal = $_GET['bal'];
} else {
$bal = 0;
}

include "OnlyQuery.php";

if ($age != 0) {
include "AgeFilter.php";
}
if (($val == 0 && $bal != 0) || ($val != 0 && $bal == 0)) {
echo "Please choose the right combination for balance";
}
if ($val != 0 && $bal != 0) {
include "BalFilter.php";
}

最佳答案

您可以做的是创建一个bool/must过滤器数组,该数组将由包含文件中的每个过滤器填充。可以使用OnlyQuery.php或在添加OnlyQuery.php之后完成,基本上是这样的:

...
include "OnlyQuery.php";

$params['body']['query']['filtered']['filter'] = array('bool' => [
'must' => []
]);
...

然后可以将 AgeFilter.php更改为此:
$term = array('term' => ['age' => $age]);
array_push($params['body']['query']['filtered']['filter']['bool']['must'], $term);

BalFilter.php到这
if ($val == 1) {
$range = array('range' => [
'balance' => ['gte' => $bal]
]);
array_push($params['body']['query']['filtered']['filter']['bool']['must'], $range);
}
if ($val == 2) {
$range = array('range' => [
'balance' => ['lte' => $bal]
]);
array_push($params['body']['query']['filtered']['filter']['bool']['must'], $range);
}

关于php - 将多个过滤器添加到ElasticsearchPhp中的现有过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34892963/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com