Is there any way how can I create Elasticsearch Search Template from BoolQueryBuilder directly?









up vote
0
down vote

favorite












How can I easily create Search Template from BoolQueryBuilder in Java ?



Right now I am trying to do it like this, but this solution seems a little bit dirty to me. Is there any better way ?



BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
boolQueryBuilder.must(new MoreLikeThisQueryBuilder("myField", "myTextToBeReplaced", null));
SearchResponse response = new SearchTemplateRequestBuilder(client)
.setRequest(new SearchRequest())
.setScriptType(ScriptType.INLINE)
.setScript(boolQueryBuilder.toString())
.setScriptParams(templateParams)
.get();


Thank you for any help.










share|improve this question

























    up vote
    0
    down vote

    favorite












    How can I easily create Search Template from BoolQueryBuilder in Java ?



    Right now I am trying to do it like this, but this solution seems a little bit dirty to me. Is there any better way ?



    BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
    boolQueryBuilder.must(new MoreLikeThisQueryBuilder("myField", "myTextToBeReplaced", null));
    SearchResponse response = new SearchTemplateRequestBuilder(client)
    .setRequest(new SearchRequest())
    .setScriptType(ScriptType.INLINE)
    .setScript(boolQueryBuilder.toString())
    .setScriptParams(templateParams)
    .get();


    Thank you for any help.










    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      How can I easily create Search Template from BoolQueryBuilder in Java ?



      Right now I am trying to do it like this, but this solution seems a little bit dirty to me. Is there any better way ?



      BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
      boolQueryBuilder.must(new MoreLikeThisQueryBuilder("myField", "myTextToBeReplaced", null));
      SearchResponse response = new SearchTemplateRequestBuilder(client)
      .setRequest(new SearchRequest())
      .setScriptType(ScriptType.INLINE)
      .setScript(boolQueryBuilder.toString())
      .setScriptParams(templateParams)
      .get();


      Thank you for any help.










      share|improve this question













      How can I easily create Search Template from BoolQueryBuilder in Java ?



      Right now I am trying to do it like this, but this solution seems a little bit dirty to me. Is there any better way ?



      BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
      boolQueryBuilder.must(new MoreLikeThisQueryBuilder("myField", "myTextToBeReplaced", null));
      SearchResponse response = new SearchTemplateRequestBuilder(client)
      .setRequest(new SearchRequest())
      .setScriptType(ScriptType.INLINE)
      .setScript(boolQueryBuilder.toString())
      .setScriptParams(templateParams)
      .get();


      Thank you for any help.







      java templates elasticsearch






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 10 at 12:34









      Druudik

      299




      299






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          You are executing inline templates, and you´re even using boolQueryBuilder.toString() to render the json instead of messy string concatenation. Good work so far @Druudik.



          But have you tried stored templates at search time (https://www.elastic.co/guide/en/elasticsearch/reference/current/search-template.html) ?
          This will move the templating completely out of your java code! Especially for larger queries this approach reduces the code complexity.



          This will also make your appliaction more flexible, as you'll be able to change the query/template, without recompiling your code (as long your parameters are stable).



          Here are some examples using java api: https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-search-template.html






          share|improve this answer




















            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',
            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%2f53238995%2fis-there-any-way-how-can-i-create-elasticsearch-search-template-from-boolquerybu%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            You are executing inline templates, and you´re even using boolQueryBuilder.toString() to render the json instead of messy string concatenation. Good work so far @Druudik.



            But have you tried stored templates at search time (https://www.elastic.co/guide/en/elasticsearch/reference/current/search-template.html) ?
            This will move the templating completely out of your java code! Especially for larger queries this approach reduces the code complexity.



            This will also make your appliaction more flexible, as you'll be able to change the query/template, without recompiling your code (as long your parameters are stable).



            Here are some examples using java api: https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-search-template.html






            share|improve this answer
























              up vote
              0
              down vote













              You are executing inline templates, and you´re even using boolQueryBuilder.toString() to render the json instead of messy string concatenation. Good work so far @Druudik.



              But have you tried stored templates at search time (https://www.elastic.co/guide/en/elasticsearch/reference/current/search-template.html) ?
              This will move the templating completely out of your java code! Especially for larger queries this approach reduces the code complexity.



              This will also make your appliaction more flexible, as you'll be able to change the query/template, without recompiling your code (as long your parameters are stable).



              Here are some examples using java api: https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-search-template.html






              share|improve this answer






















                up vote
                0
                down vote










                up vote
                0
                down vote









                You are executing inline templates, and you´re even using boolQueryBuilder.toString() to render the json instead of messy string concatenation. Good work so far @Druudik.



                But have you tried stored templates at search time (https://www.elastic.co/guide/en/elasticsearch/reference/current/search-template.html) ?
                This will move the templating completely out of your java code! Especially for larger queries this approach reduces the code complexity.



                This will also make your appliaction more flexible, as you'll be able to change the query/template, without recompiling your code (as long your parameters are stable).



                Here are some examples using java api: https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-search-template.html






                share|improve this answer












                You are executing inline templates, and you´re even using boolQueryBuilder.toString() to render the json instead of messy string concatenation. Good work so far @Druudik.



                But have you tried stored templates at search time (https://www.elastic.co/guide/en/elasticsearch/reference/current/search-template.html) ?
                This will move the templating completely out of your java code! Especially for larger queries this approach reduces the code complexity.



                This will also make your appliaction more flexible, as you'll be able to change the query/template, without recompiling your code (as long your parameters are stable).



                Here are some examples using java api: https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-search-template.html







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 10 at 22:06









                ibexit

                581112




                581112



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238995%2fis-there-any-way-how-can-i-create-elasticsearch-search-template-from-boolquerybu%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







                    這個網誌中的熱門文章

                    Barbados

                    How to read a connectionString WITH PROVIDER in .NET Core?

                    Node.js Script on GitHub Pages or Amazon S3