#$condition = "((column08=Submit & column10=Delivered & column09=Somet +hing) | (column08=Delivered))"; $condition = "(column08=Submit & column10=Delivered & column09=Somethi +ng)"; #$condition = "((column08=Submit & column10=Delivered & column09=Somet +hing) | (column08=Delivered & column09=Something))";

Hope you have idea on elastic search. From the GUI i will get these type of conditions. I have to parse this into elastic search queries. Is there any easiest way to do this.

Example : { 'query' => { 'constant_score' => { 'filter' => { 'bool' => { + 'must' => [ + { + 'term' => { + 'column08' => 'Submit' + } + }, + { + 'term' => { + 'column10' => 'Delivered' + } + }, + { + 'term' => { + 'column09' => 'Something' + } + } + ] } } } } }

Any help will be appreciated.

Upto now I tried this. But missing some where

#!/usr/bin/perl #use strict; #use warnings; use Data::Dumper; use List::MoreUtils qw(any uniq); use JSON; $condition = "((column08=Submit & column10=Delivered & column09=Someth +ing) | (column08=Delivered))"; #$condition = "(column08=Submit & column10=Delivered & column09=Someth +ing)"; #$condition = "((column08=Submit & column10=Delivered & column09=Somet +hing) | (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_in +dex-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_a +rray[(int($condition)-1)]; my $json = encode_json \%main_hash; print $json; print Dumper \%main_hash; #print "In this loop"; } } exit;

In reply to Queries for elastic search using perl by ravi45722

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.