#$condition = "((column08=Submit & column10=Delivered & column09=Something) | (column08=Delivered))";
$condition = "(column08=Submit & column10=Delivered & column09=Something)";
#$condition = "((column08=Submit & column10=Delivered & column09=Something) | (column08=Delivered & column09=Something))";
####
Example : {
'query' => {
'constant_score' => {
'filter' => {
'bool' => {
'must' => [
{
'term' => {
'column08' => 'Submit'
}
},
{
'term' => {
'column10' => 'Delivered'
}
},
{
'term' => {
'column09' => 'Something'
}
}
]
}
}
}
}
}
####
#!/usr/bin/perl
#use strict;
#use warnings;
use Data::Dumper;
use List::MoreUtils qw(any uniq);
use JSON;
$condition = "((column08=Submit & column10=Delivered & column09=Something) | (column08=Delivered))";
#$condition = "(column08=Submit & column10=Delivered & column09=Something)";
#$condition = "((column08=Submit & column10=Delivered & column09=Something) | (column08=Delivered & column09=Something))";
my $char_index = -1;
my $stack = 0;
my @condition_array = ();
sub parse_function
{
$start_pos = $_[0];
$end_pos = ($_[1]);
my @return_array = ();
my $end_pos = ($end_pos - $start_pos)+1;
my $string = substr($condition,$start_pos,$end_pos);
print $string,$/;
if (($string =~ m/(=|!=)/))
{
my @matches = grep defined, split / ([|&]) /, $string;
if ((scalar @matches) > 1)
{
foreach my $one (@matches)
{
if (($one =~ tr/=//) == 1)
{
my ($key,$value)=(split /=/,$one);
push @return_array, { term => { $key => $value} };
}
elsif (($one =~ tr/!=//) == 1)
{
my ($key,$value)=(split /!=/,$one);
push @return_array, { term => { $key => $value} };
}
else
{
push (@symbol_array,$one);
}
}
@symbol_array = uniq (@symbol_array);
$symbol_count = scalar @symbol_array;
if ($symbol_count > 1)
{
print "Check the condition once again... Getting multi symbol in one brace";
exit;
}
if (@symbol_array[0] == '&')
{
$return_hash{'bool'}{'must'} = [@return_array];
}
elsif (@symbol_array[0] == '|')
{
$return_hash{'bool'}{'should'} = [@return_array];
}
push @condition_array, %return_hash;
print Dumper \@condition_array;
exit;
$count = @condition_array;
return $count;
}
else
{
my ($key,$value)=(split /=/,$string);
push @condition_array, { term => { $key => $value} };
$count = @condition_array;
return $count;
}
}
else
{
#print "In final else loop";
my @matches = grep defined, split / ([|&]) /, $string;
foreach my $one (@matches)
{
if ($one == '&' or $one == '|')
{
push (@array_symbol,$one);
}
else
{
push (@return_array,$one);
}
}
@array_symbol = uniq (@array_symbol);
$count_array_symbol = @array_symbol;
if ($count_array_symbol > 1)
{
print "In final loop : Got two different operators";
exit;
}
else
{
if ($array_symbol[0] == '|')
{
foreach my $instance (@return_array)
{
$return_hash{'bool'}{'should'} = [@condition_array[int($instance)]];
}
}
if ($array_symbol[0] == '&')
{
foreach my $instance (@return_array)
{
$return_hash{'bool'}{'match'} = [@condition_array[int($instance)]];
}
}
push @condition_array, \%return_hash;
print Dumper \%return_hash;
$count = @condition_array;
return $count;
}
}
}
HERE:
$char_index = -1;
foreach my $char (split //, $condition)
{
$char_index++;
if ( (index($condition,'(') != -1))
{
if ($char eq "(")
{
$open_brace_index = $char_index;
}
elsif ($char eq ")")
{
$returned = parse_function(($open_brace_index+1),($char_index-1));
$first_string = substr($condition,0,$open_brace_index);
$last_string = substr($condition,($char_index+1),(length($condition)-$char_index));
#print $last_string,$/;
$condition = $first_string.$returned.$last_string;
print $condition,$/;
goto HERE;
}
}
else
{
$main_hash{"query"}{"constant_score"}{"filter"} = @condition_array[(int($condition)-1)];
my $json = encode_json \%main_hash;
print $json;
print Dumper \%main_hash;
#print "In this loop";
}
}
exit;