((column08=Submit | column10=Delivered | column09=Something) & (column08=Delivered | column09=Something)) #### {'query' => {'constant_score' => {'filter' => {'bool' => {'should' => [{'bool' => {'must' => [{'term' => {'column08' => 'Submit'}},{'term' => {'column10' => 'Delivered'}},{'term' => {'column09' => 'Something'}}]}},{'bool' => {'must' => [{'term' => {'column08' => 'Delivered'}},{'term' => {'column09' => 'Something'}}]}}]}}}}} #### { "sheets": { "sheet1": { "Sheet_Name": "Ex-Sheet", "forumle": [ { "0": { "colName": "Ex-Col1", "formula": " #### ", "total": "false" }, "1": { "colName": "Ex-Col1", "formula": " #### ", "total": "false" }, "2": { "colName": "Ex-Col1", "formula": " #### ", "total": "false" } } ] } } } #### foreach my $sheet (keys %{ $doc->{'_source'}{'sheets'} }) { foreach my $columns (@{ $doc->{'_source'}{'sheets'}{$sheet}{'forumle'} }) { foreach my $one (keys %{ $columns }) { $condition = $columns->{$one}{'formula'}; $return_value = function_to_change($condition); #Here I am changing my condition into required output $main_hash{"query"}{"constant_score"}{"filter"} = {$return_value}; #Adding some more data for that $columns->{$one}{'formula'} = \%main_hash #What to replace that in the original doc } } } #### 'bool' => { 'should' => [ $VAR1->{'_source'}{'sheets'}{'sheet1'}{'forumle'}[0]{'1'}{'formula'}{'query'}{'constant_score'}{'filter'}{'bool'}{'should'}[0], $VAR1->{'_source'}{'sheets'}{'sheet1'}{'forumle'}[0]{'1'}{'formula'}{'query'}{'constant_score'}{'filter'}{'bool'}{'should'}[1]]} #### {"query":{"constant_score":{"filter":{"bool":{"should":[{"bool":{"must":[{"term":{"column08":"Submit"}},{"term":{"column10":"Delivered"}},{"term":{"column09":"Something"}}]}},{"bool":{"must":[{"term":{"column08":"Delivered"}},{"term":{"column09":"Something"}}]}},{"bool":{"must":[{"term":{"column08":"Submit"}},{"term":{"column10":"Delivered"}},{"term":{"column09":"Something"}}]}},{"bool":{"must":[{"term":{"column08":"Delivered"}},{"term":{"column09":"Something"}}]}}]}}}}}