I am a beginner and working on elasticsearch php for a simple search. I have to set filters conditionally based on weather the filter is selected or not. Hence I have made the filters for that specific task and placed them in different files. All i do is include these files when the isset returns true. But the trouble is, if more than one filter is selected, then the last filter overwrites the filter part of array and does not provide the required multiple filtered answer. Is there anyway to make this work? (Please pardon me if it is inefficient as I am a beginner and just trying to learn.)
我是初学者,正在使用elasticsearch php进行简单的搜索。我必须根据天气选择或不选择过滤器来有条件地设置过滤器。因此,我为该特定任务制作了过滤器,并将它们放在不同的文件中。我所做的就是当isset返回true时包含这些文件。但问题是,如果选择了多个过滤器,则最后一个过滤器会覆盖数组的过滤器部分,并且不会提供所需的多个过滤答案。反正有没有让这项工作? (请原谅我,因为我是初学者,只是想学习,效率低下。)
My files are something like this
我的文件是这样的
BalFilter.php
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
AgeFilter.php
<?php
$params['body']['query']['filtered']['filter']['term'] = ['age' => $age];
OnlyQuery.php
OnlyQuery.php
<?php
$params['body']['query']['filtered']['query'] = [ 'query_string' => [
'default_field' => '_all',
'query' => $q,
'lenient' => true
]
];
And in the main index
并在主要指数
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";
}
1
What you could do is to create a bool/must
filters array that gets populated by each of your filter in the included file. This can be done either in OnlyQuery.php
or just after including OnlyQuery.php
, basically like this:
你可以做的是创建一个bool / must过滤器数组,该数组由包含文件中的每个过滤器填充。这可以在OnlyQuery.php中完成,也可以在包含OnlyQuery.php之后完成,基本上是这样的:
...
include "OnlyQuery.php";
$params['body']['query']['filtered']['filter'] = array('bool' => [
'must' => []
]);
...
Then AgeFilter.php
can be changed to this:
然后可以将AgeFilter.php更改为:
$term = array('term' => ['age' => $age]);
array_push($params['body']['query']['filtered']['filter']['bool']['must'], $term);
And BalFilter.php
to this
和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);
}
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2016/01/20/ca9fa85a3bde2f4a981418dd66214d62.html。