Convert an infix expression to elastic search query












1















How can I convert an infix expression to an elastic search query

my operators are ! + *

and user may make any expression using those operators, something like:
(((A*B*(!C))*(D*E))+F)*G

and I wish to convert it to a bool query in elastic search



Edit

I don't know why I didn't say this earlier but I have already written a code to convert infix to postfix expression and then I call a very dirty recursive method to create should (+), must (*) and must_not (!) but what i'm seeking is an optimized way to do the trick for me.



My query at the end is something like this which I think is very very nasty:



{
"from": 0,
"size": 10,
"_source": [
"*"
],
"index": "insta_userpost_new2",
"body": {
"query": {
"bool": {
"must": [
{
"match_phrase": {
"caption.text": "G"
}
},
{
"bool": {
"should": [
{
"match_phrase": {
"caption.text": "F"
}
},
{
"bool": {
"must": [
{
"bool": {
"must": [
{
"match_phrase": {
"caption.text": "E"
}
},
{
"match_phrase": {
"caption.text": "D"
}
}
]
}
},
{
"bool": {
"must": [
{
"bool": {
"must_not": [
{
"match_phrase": {
"caption.text": "C"
}
},
{
"bool": {
"must": [
{
"match_phrase": {
"caption.text": "B"
}
},
{
"match_phrase": {
"caption.text": "A"
}
}
]
}
}
]
}
}
]
}
}
]
}
}
]
}
}
]
}
}
}
}









share|improve this question

























  • Does * mean AND and + mean OR? or do those symbols have any other meaning?

    – Val
    Jan 4 at 21:32











  • @Val yes they do mean OR and AND

    – AH.Pooladvand
    Jan 5 at 8:50











  • Ok thanks, I'll provide an answer later today

    – Val
    Jan 5 at 10:41
















1















How can I convert an infix expression to an elastic search query

my operators are ! + *

and user may make any expression using those operators, something like:
(((A*B*(!C))*(D*E))+F)*G

and I wish to convert it to a bool query in elastic search



Edit

I don't know why I didn't say this earlier but I have already written a code to convert infix to postfix expression and then I call a very dirty recursive method to create should (+), must (*) and must_not (!) but what i'm seeking is an optimized way to do the trick for me.



My query at the end is something like this which I think is very very nasty:



{
"from": 0,
"size": 10,
"_source": [
"*"
],
"index": "insta_userpost_new2",
"body": {
"query": {
"bool": {
"must": [
{
"match_phrase": {
"caption.text": "G"
}
},
{
"bool": {
"should": [
{
"match_phrase": {
"caption.text": "F"
}
},
{
"bool": {
"must": [
{
"bool": {
"must": [
{
"match_phrase": {
"caption.text": "E"
}
},
{
"match_phrase": {
"caption.text": "D"
}
}
]
}
},
{
"bool": {
"must": [
{
"bool": {
"must_not": [
{
"match_phrase": {
"caption.text": "C"
}
},
{
"bool": {
"must": [
{
"match_phrase": {
"caption.text": "B"
}
},
{
"match_phrase": {
"caption.text": "A"
}
}
]
}
}
]
}
}
]
}
}
]
}
}
]
}
}
]
}
}
}
}









share|improve this question

























  • Does * mean AND and + mean OR? or do those symbols have any other meaning?

    – Val
    Jan 4 at 21:32











  • @Val yes they do mean OR and AND

    – AH.Pooladvand
    Jan 5 at 8:50











  • Ok thanks, I'll provide an answer later today

    – Val
    Jan 5 at 10:41














1












1








1


0






How can I convert an infix expression to an elastic search query

my operators are ! + *

and user may make any expression using those operators, something like:
(((A*B*(!C))*(D*E))+F)*G

and I wish to convert it to a bool query in elastic search



Edit

I don't know why I didn't say this earlier but I have already written a code to convert infix to postfix expression and then I call a very dirty recursive method to create should (+), must (*) and must_not (!) but what i'm seeking is an optimized way to do the trick for me.



My query at the end is something like this which I think is very very nasty:



{
"from": 0,
"size": 10,
"_source": [
"*"
],
"index": "insta_userpost_new2",
"body": {
"query": {
"bool": {
"must": [
{
"match_phrase": {
"caption.text": "G"
}
},
{
"bool": {
"should": [
{
"match_phrase": {
"caption.text": "F"
}
},
{
"bool": {
"must": [
{
"bool": {
"must": [
{
"match_phrase": {
"caption.text": "E"
}
},
{
"match_phrase": {
"caption.text": "D"
}
}
]
}
},
{
"bool": {
"must": [
{
"bool": {
"must_not": [
{
"match_phrase": {
"caption.text": "C"
}
},
{
"bool": {
"must": [
{
"match_phrase": {
"caption.text": "B"
}
},
{
"match_phrase": {
"caption.text": "A"
}
}
]
}
}
]
}
}
]
}
}
]
}
}
]
}
}
]
}
}
}
}









share|improve this question
















How can I convert an infix expression to an elastic search query

my operators are ! + *

and user may make any expression using those operators, something like:
(((A*B*(!C))*(D*E))+F)*G

and I wish to convert it to a bool query in elastic search



Edit

I don't know why I didn't say this earlier but I have already written a code to convert infix to postfix expression and then I call a very dirty recursive method to create should (+), must (*) and must_not (!) but what i'm seeking is an optimized way to do the trick for me.



My query at the end is something like this which I think is very very nasty:



{
"from": 0,
"size": 10,
"_source": [
"*"
],
"index": "insta_userpost_new2",
"body": {
"query": {
"bool": {
"must": [
{
"match_phrase": {
"caption.text": "G"
}
},
{
"bool": {
"should": [
{
"match_phrase": {
"caption.text": "F"
}
},
{
"bool": {
"must": [
{
"bool": {
"must": [
{
"match_phrase": {
"caption.text": "E"
}
},
{
"match_phrase": {
"caption.text": "D"
}
}
]
}
},
{
"bool": {
"must": [
{
"bool": {
"must_not": [
{
"match_phrase": {
"caption.text": "C"
}
},
{
"bool": {
"must": [
{
"match_phrase": {
"caption.text": "B"
}
},
{
"match_phrase": {
"caption.text": "A"
}
}
]
}
}
]
}
}
]
}
}
]
}
}
]
}
}
]
}
}
}
}






php algorithm elasticsearch






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 5 at 20:13







AH.Pooladvand

















asked Dec 31 '18 at 8:18









AH.PooladvandAH.Pooladvand

14212




14212













  • Does * mean AND and + mean OR? or do those symbols have any other meaning?

    – Val
    Jan 4 at 21:32











  • @Val yes they do mean OR and AND

    – AH.Pooladvand
    Jan 5 at 8:50











  • Ok thanks, I'll provide an answer later today

    – Val
    Jan 5 at 10:41



















  • Does * mean AND and + mean OR? or do those symbols have any other meaning?

    – Val
    Jan 4 at 21:32











  • @Val yes they do mean OR and AND

    – AH.Pooladvand
    Jan 5 at 8:50











  • Ok thanks, I'll provide an answer later today

    – Val
    Jan 5 at 10:41

















Does * mean AND and + mean OR? or do those symbols have any other meaning?

– Val
Jan 4 at 21:32





Does * mean AND and + mean OR? or do those symbols have any other meaning?

– Val
Jan 4 at 21:32













@Val yes they do mean OR and AND

– AH.Pooladvand
Jan 5 at 8:50





@Val yes they do mean OR and AND

– AH.Pooladvand
Jan 5 at 8:50













Ok thanks, I'll provide an answer later today

– Val
Jan 5 at 10:41





Ok thanks, I'll provide an answer later today

– Val
Jan 5 at 10:41












3 Answers
3






active

oldest

votes


















1





+300









I would maybe try to leverage simply_query_string. For that, you'd have to:




  • replace + by | (for the OR)

  • then replace * by + (for the AND)

  • finally replace ! by - (for the NOT)


So if a user inputs this:



(((A*B*(!C))*(D*E))+F)*G


You'd end up with this



(((A+B+(-C))+(D+E))|F)+G


Which is a boolean expression that you can directly use in a simply_query_string query.



GET /_search
{
"query": {
"simple_query_string" : {
"fields" : ["content"],
"query" : "(((A+B+(-C))+(D+E))|F)+G"
}
}
}





share|improve this answer
























  • hmmm, I don't have access to my pc right now but if that works I own you a lot

    – AH.Pooladvand
    Jan 5 at 20:17











  • Shame on me for not fully reading the documentation

    – AH.Pooladvand
    Jan 6 at 6:46











  • Awesome, I'm glad it helped ;-)

    – Val
    Jan 6 at 6:46



















0














You can use Elasticsearch scripts in queries, like this:
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-script-query.html



There are few scripting options, with the most simple and strait forward is 'Painless' scripts. From elastic documentation:




When you define a scripted field in Kibana, you have a choice of scripting languages. Starting with 5.0, the default options are Lucene expressions and Painless




Also you can return the result of the calculation using Scripted Fields:
https://www.elastic.co/guide/en/kibana/current/scripted-fields.html






share|improve this answer































    0














    You can run an infix expression evaluation[1] and replace the standard eval operations with DSL bool query composers.



    We actually do something akin to this for https://opensource.appbase.io/mirage/ (you can try it live), where we map GUI blocks to a composable bool query. The source code is viewable at https://github.com/appbaseio/mirage.



    [1] Ref: https://www.geeksforgeeks.org/expression-evaluation/






    share|improve this answer
























    • Very nice query composer, thanks for sharing, I'll give it a thorough look.

      – Val
      Jan 5 at 20:19











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53985156%2fconvert-an-infix-expression-to-elastic-search-query%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1





    +300









    I would maybe try to leverage simply_query_string. For that, you'd have to:




    • replace + by | (for the OR)

    • then replace * by + (for the AND)

    • finally replace ! by - (for the NOT)


    So if a user inputs this:



    (((A*B*(!C))*(D*E))+F)*G


    You'd end up with this



    (((A+B+(-C))+(D+E))|F)+G


    Which is a boolean expression that you can directly use in a simply_query_string query.



    GET /_search
    {
    "query": {
    "simple_query_string" : {
    "fields" : ["content"],
    "query" : "(((A+B+(-C))+(D+E))|F)+G"
    }
    }
    }





    share|improve this answer
























    • hmmm, I don't have access to my pc right now but if that works I own you a lot

      – AH.Pooladvand
      Jan 5 at 20:17











    • Shame on me for not fully reading the documentation

      – AH.Pooladvand
      Jan 6 at 6:46











    • Awesome, I'm glad it helped ;-)

      – Val
      Jan 6 at 6:46
















    1





    +300









    I would maybe try to leverage simply_query_string. For that, you'd have to:




    • replace + by | (for the OR)

    • then replace * by + (for the AND)

    • finally replace ! by - (for the NOT)


    So if a user inputs this:



    (((A*B*(!C))*(D*E))+F)*G


    You'd end up with this



    (((A+B+(-C))+(D+E))|F)+G


    Which is a boolean expression that you can directly use in a simply_query_string query.



    GET /_search
    {
    "query": {
    "simple_query_string" : {
    "fields" : ["content"],
    "query" : "(((A+B+(-C))+(D+E))|F)+G"
    }
    }
    }





    share|improve this answer
























    • hmmm, I don't have access to my pc right now but if that works I own you a lot

      – AH.Pooladvand
      Jan 5 at 20:17











    • Shame on me for not fully reading the documentation

      – AH.Pooladvand
      Jan 6 at 6:46











    • Awesome, I'm glad it helped ;-)

      – Val
      Jan 6 at 6:46














    1





    +300







    1





    +300



    1




    +300





    I would maybe try to leverage simply_query_string. For that, you'd have to:




    • replace + by | (for the OR)

    • then replace * by + (for the AND)

    • finally replace ! by - (for the NOT)


    So if a user inputs this:



    (((A*B*(!C))*(D*E))+F)*G


    You'd end up with this



    (((A+B+(-C))+(D+E))|F)+G


    Which is a boolean expression that you can directly use in a simply_query_string query.



    GET /_search
    {
    "query": {
    "simple_query_string" : {
    "fields" : ["content"],
    "query" : "(((A+B+(-C))+(D+E))|F)+G"
    }
    }
    }





    share|improve this answer













    I would maybe try to leverage simply_query_string. For that, you'd have to:




    • replace + by | (for the OR)

    • then replace * by + (for the AND)

    • finally replace ! by - (for the NOT)


    So if a user inputs this:



    (((A*B*(!C))*(D*E))+F)*G


    You'd end up with this



    (((A+B+(-C))+(D+E))|F)+G


    Which is a boolean expression that you can directly use in a simply_query_string query.



    GET /_search
    {
    "query": {
    "simple_query_string" : {
    "fields" : ["content"],
    "query" : "(((A+B+(-C))+(D+E))|F)+G"
    }
    }
    }






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 5 at 20:08









    ValVal

    104k6142176




    104k6142176













    • hmmm, I don't have access to my pc right now but if that works I own you a lot

      – AH.Pooladvand
      Jan 5 at 20:17











    • Shame on me for not fully reading the documentation

      – AH.Pooladvand
      Jan 6 at 6:46











    • Awesome, I'm glad it helped ;-)

      – Val
      Jan 6 at 6:46



















    • hmmm, I don't have access to my pc right now but if that works I own you a lot

      – AH.Pooladvand
      Jan 5 at 20:17











    • Shame on me for not fully reading the documentation

      – AH.Pooladvand
      Jan 6 at 6:46











    • Awesome, I'm glad it helped ;-)

      – Val
      Jan 6 at 6:46

















    hmmm, I don't have access to my pc right now but if that works I own you a lot

    – AH.Pooladvand
    Jan 5 at 20:17





    hmmm, I don't have access to my pc right now but if that works I own you a lot

    – AH.Pooladvand
    Jan 5 at 20:17













    Shame on me for not fully reading the documentation

    – AH.Pooladvand
    Jan 6 at 6:46





    Shame on me for not fully reading the documentation

    – AH.Pooladvand
    Jan 6 at 6:46













    Awesome, I'm glad it helped ;-)

    – Val
    Jan 6 at 6:46





    Awesome, I'm glad it helped ;-)

    – Val
    Jan 6 at 6:46













    0














    You can use Elasticsearch scripts in queries, like this:
    https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-script-query.html



    There are few scripting options, with the most simple and strait forward is 'Painless' scripts. From elastic documentation:




    When you define a scripted field in Kibana, you have a choice of scripting languages. Starting with 5.0, the default options are Lucene expressions and Painless




    Also you can return the result of the calculation using Scripted Fields:
    https://www.elastic.co/guide/en/kibana/current/scripted-fields.html






    share|improve this answer




























      0














      You can use Elasticsearch scripts in queries, like this:
      https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-script-query.html



      There are few scripting options, with the most simple and strait forward is 'Painless' scripts. From elastic documentation:




      When you define a scripted field in Kibana, you have a choice of scripting languages. Starting with 5.0, the default options are Lucene expressions and Painless




      Also you can return the result of the calculation using Scripted Fields:
      https://www.elastic.co/guide/en/kibana/current/scripted-fields.html






      share|improve this answer


























        0












        0








        0







        You can use Elasticsearch scripts in queries, like this:
        https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-script-query.html



        There are few scripting options, with the most simple and strait forward is 'Painless' scripts. From elastic documentation:




        When you define a scripted field in Kibana, you have a choice of scripting languages. Starting with 5.0, the default options are Lucene expressions and Painless




        Also you can return the result of the calculation using Scripted Fields:
        https://www.elastic.co/guide/en/kibana/current/scripted-fields.html






        share|improve this answer













        You can use Elasticsearch scripts in queries, like this:
        https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-script-query.html



        There are few scripting options, with the most simple and strait forward is 'Painless' scripts. From elastic documentation:




        When you define a scripted field in Kibana, you have a choice of scripting languages. Starting with 5.0, the default options are Lucene expressions and Painless




        Also you can return the result of the calculation using Scripted Fields:
        https://www.elastic.co/guide/en/kibana/current/scripted-fields.html







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 31 '18 at 9:00









        zivziv

        762714




        762714























            0














            You can run an infix expression evaluation[1] and replace the standard eval operations with DSL bool query composers.



            We actually do something akin to this for https://opensource.appbase.io/mirage/ (you can try it live), where we map GUI blocks to a composable bool query. The source code is viewable at https://github.com/appbaseio/mirage.



            [1] Ref: https://www.geeksforgeeks.org/expression-evaluation/






            share|improve this answer
























            • Very nice query composer, thanks for sharing, I'll give it a thorough look.

              – Val
              Jan 5 at 20:19
















            0














            You can run an infix expression evaluation[1] and replace the standard eval operations with DSL bool query composers.



            We actually do something akin to this for https://opensource.appbase.io/mirage/ (you can try it live), where we map GUI blocks to a composable bool query. The source code is viewable at https://github.com/appbaseio/mirage.



            [1] Ref: https://www.geeksforgeeks.org/expression-evaluation/






            share|improve this answer
























            • Very nice query composer, thanks for sharing, I'll give it a thorough look.

              – Val
              Jan 5 at 20:19














            0












            0








            0







            You can run an infix expression evaluation[1] and replace the standard eval operations with DSL bool query composers.



            We actually do something akin to this for https://opensource.appbase.io/mirage/ (you can try it live), where we map GUI blocks to a composable bool query. The source code is viewable at https://github.com/appbaseio/mirage.



            [1] Ref: https://www.geeksforgeeks.org/expression-evaluation/






            share|improve this answer













            You can run an infix expression evaluation[1] and replace the standard eval operations with DSL bool query composers.



            We actually do something akin to this for https://opensource.appbase.io/mirage/ (you can try it live), where we map GUI blocks to a composable bool query. The source code is viewable at https://github.com/appbaseio/mirage.



            [1] Ref: https://www.geeksforgeeks.org/expression-evaluation/







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 5 at 17:22









            sidisidi

            1,6151020




            1,6151020













            • Very nice query composer, thanks for sharing, I'll give it a thorough look.

              – Val
              Jan 5 at 20:19



















            • Very nice query composer, thanks for sharing, I'll give it a thorough look.

              – Val
              Jan 5 at 20:19

















            Very nice query composer, thanks for sharing, I'll give it a thorough look.

            – Val
            Jan 5 at 20:19





            Very nice query composer, thanks for sharing, I'll give it a thorough look.

            – Val
            Jan 5 at 20:19


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53985156%2fconvert-an-infix-expression-to-elastic-search-query%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas