Tejas Sateesh
Tejas Sateesh

Reputation: 74

Adding Multiple Filters to an existing Filter in ElasticsearchPhp

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.)

My files are something like this

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
                                                                ]
                                               ];

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";
}

Upvotes: 2

Views: 387

Answers (1)

Val
Val

Reputation: 217344

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:

...
include "OnlyQuery.php";

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

Then AgeFilter.php can be changed to this:

$term = array('term' => ['age' => $age]);
array_push($params['body']['query']['filtered']['filter']['bool']['must'], $term);

And BalFilter.php to this

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);
}

Upvotes: 2

Related Questions