0

I have to create the following elastic search query in php:

$parameters = [
    'index' => 'myindex',
    'from' => 0,
    'size' => 60,
    'body' => [
        'query' => [
            'bool' => [
                'filter' => [[
                'bool' => [
                    'must' => [
                        $myvariable,
                        ['nested' => [
                                'path' => 'sh',
                                'query' => [
                                    'bool' => [
                                        'filter' => [
                                            ['term' => ['sh.keyword' => 'PK']]
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
                    ]],
            ]
        ],
        "sort" => [
            ["score" => ["mode" => "avg"]]
        ]
    ]
];

Where $myvariable should be:

['bool'=>
  ['should'=>[
      ['term'=> ['id'=> {$id1}]],
      ['term'=> ['id'=> {$id2}]],
          ],
    'minimum_should_match'=> 1
  ]
]

I do not want to hardcode the values as it completely depends on the user whether she/he will choose the mentioned values in drop down or not.

What I tried?

I tried to create the inner part, that is the term part:

$countofids = count($combined_array['ids']);


if ($countofids != 0) {

for ($i = 0; $i < $countofids ; $i++) {

    if ($i == 0) {
        $myvariable= array('term' => array('id' => $combined_array['ids'][$i]));
    } else {
         array_push($myvariable, $combined_array['ids'][$i]);
    }
}

}

But, it does not work as expected. I get:

['term'=> ['id'=> {$id1}]]

But, further, I do not get the required result. Moreover, I don't know how to separate them by comma. Can anyone provide a better solution?

1 Answer 1

1

I've reworked the code slightly to make it easier to read, but the idea is that you were just overwriting $myvariable each time. I've included some test data for my own purposes and show what it's doing, but the main part is to replace the code you already have...

$combined_array = ['ids' => [1,2]];

if (count($combined_array['ids']) != 0) {
    // Define array for terms
    $myvariable = [];
    foreach ( $combined_array['ids'] as $id ) {
        // Add new term to existing list
        $myvariable[]= array('term' => array('id' => $id));
    }
    // Combine data with main structure
    $myvariable = [ 'bool' => ['should' => $myvariable,
        'minimum_should_match'=> 1]
    ];
}

print_r($myvariable);

which gives...

Array
(
    [bool] => Array
        (
            [should] => Array
                (
                    [0] => Array
                        (
                            [term] => Array
                                (
                                    [id] => 1
                                )
                        )
                    [1] => Array
                        (
                            [term] => Array
                                (
                                    [id] => 2
                                )
                        )
                )
        )
)

If I've made any incorrect assumptions then let me know and I will sort them out.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the response! How can I integrate ''minimum_should_match'=> 1'?
I've added in the value, can you check it.
Yes, sorry, I saw the old one. Thanks! I will just check the result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.