painless: check if an individual document contains a key












1















I'm using painless to filter documents with Elastic 5.5



Problem



Using "painless", find documents with strings field.



Expected Results



Only documents with strings field are returned



Actual Results



All documents are returned.



Observation



All documents are returned, as long as there's a document with strings field. This could be a caching issue of some sort.



TestCase



Fixtures



PUT /test_idx

POST /test_idx/t/1
{
"strings": ["hello", "world"]
}

POST /test_idx/t/2
{
"numbers": [1, 2, 3]
}


Query



GET /test_idx/_search
{
"query": {
"bool": {
"filter": [
{
"script": {
"script": {
"lang": "painless",
"inline": "return doc.containsKey(params.keypath)",
"params": {"keypath": "strings"}
}
}
}
]
}
}
}


Actual Response



{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0,
"hits": [
{
"_index": "test_idx",
"_type": "t",
"_id": "2",
"_score": 0,
"_source": {
"numbers": [
1,
2,
3
]
}
},
{
"_index": "test_idx",
"_type": "t",
"_id": "1",
"_score": 0,
"_source": {
"strings": [
"hello",
"world"
]
}
}
]
}
}


Expected Response



{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0,
"hits": [
{
"_index": "test_idx",
"_type": "t",
"_id": "1",
"_score": 0,
"_source": {
"strings": [
"hello",
"world"
]
}
}
]
}
}









share|improve this question





























    1















    I'm using painless to filter documents with Elastic 5.5



    Problem



    Using "painless", find documents with strings field.



    Expected Results



    Only documents with strings field are returned



    Actual Results



    All documents are returned.



    Observation



    All documents are returned, as long as there's a document with strings field. This could be a caching issue of some sort.



    TestCase



    Fixtures



    PUT /test_idx

    POST /test_idx/t/1
    {
    "strings": ["hello", "world"]
    }

    POST /test_idx/t/2
    {
    "numbers": [1, 2, 3]
    }


    Query



    GET /test_idx/_search
    {
    "query": {
    "bool": {
    "filter": [
    {
    "script": {
    "script": {
    "lang": "painless",
    "inline": "return doc.containsKey(params.keypath)",
    "params": {"keypath": "strings"}
    }
    }
    }
    ]
    }
    }
    }


    Actual Response



    {
    "took": 5,
    "timed_out": false,
    "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
    },
    "hits": {
    "total": 2,
    "max_score": 0,
    "hits": [
    {
    "_index": "test_idx",
    "_type": "t",
    "_id": "2",
    "_score": 0,
    "_source": {
    "numbers": [
    1,
    2,
    3
    ]
    }
    },
    {
    "_index": "test_idx",
    "_type": "t",
    "_id": "1",
    "_score": 0,
    "_source": {
    "strings": [
    "hello",
    "world"
    ]
    }
    }
    ]
    }
    }


    Expected Response



    {
    "took": 5,
    "timed_out": false,
    "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
    },
    "hits": {
    "total": 2,
    "max_score": 0,
    "hits": [
    {
    "_index": "test_idx",
    "_type": "t",
    "_id": "1",
    "_score": 0,
    "_source": {
    "strings": [
    "hello",
    "world"
    ]
    }
    }
    ]
    }
    }









    share|improve this question



























      1












      1








      1








      I'm using painless to filter documents with Elastic 5.5



      Problem



      Using "painless", find documents with strings field.



      Expected Results



      Only documents with strings field are returned



      Actual Results



      All documents are returned.



      Observation



      All documents are returned, as long as there's a document with strings field. This could be a caching issue of some sort.



      TestCase



      Fixtures



      PUT /test_idx

      POST /test_idx/t/1
      {
      "strings": ["hello", "world"]
      }

      POST /test_idx/t/2
      {
      "numbers": [1, 2, 3]
      }


      Query



      GET /test_idx/_search
      {
      "query": {
      "bool": {
      "filter": [
      {
      "script": {
      "script": {
      "lang": "painless",
      "inline": "return doc.containsKey(params.keypath)",
      "params": {"keypath": "strings"}
      }
      }
      }
      ]
      }
      }
      }


      Actual Response



      {
      "took": 5,
      "timed_out": false,
      "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
      },
      "hits": {
      "total": 2,
      "max_score": 0,
      "hits": [
      {
      "_index": "test_idx",
      "_type": "t",
      "_id": "2",
      "_score": 0,
      "_source": {
      "numbers": [
      1,
      2,
      3
      ]
      }
      },
      {
      "_index": "test_idx",
      "_type": "t",
      "_id": "1",
      "_score": 0,
      "_source": {
      "strings": [
      "hello",
      "world"
      ]
      }
      }
      ]
      }
      }


      Expected Response



      {
      "took": 5,
      "timed_out": false,
      "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
      },
      "hits": {
      "total": 2,
      "max_score": 0,
      "hits": [
      {
      "_index": "test_idx",
      "_type": "t",
      "_id": "1",
      "_score": 0,
      "_source": {
      "strings": [
      "hello",
      "world"
      ]
      }
      }
      ]
      }
      }









      share|improve this question
















      I'm using painless to filter documents with Elastic 5.5



      Problem



      Using "painless", find documents with strings field.



      Expected Results



      Only documents with strings field are returned



      Actual Results



      All documents are returned.



      Observation



      All documents are returned, as long as there's a document with strings field. This could be a caching issue of some sort.



      TestCase



      Fixtures



      PUT /test_idx

      POST /test_idx/t/1
      {
      "strings": ["hello", "world"]
      }

      POST /test_idx/t/2
      {
      "numbers": [1, 2, 3]
      }


      Query



      GET /test_idx/_search
      {
      "query": {
      "bool": {
      "filter": [
      {
      "script": {
      "script": {
      "lang": "painless",
      "inline": "return doc.containsKey(params.keypath)",
      "params": {"keypath": "strings"}
      }
      }
      }
      ]
      }
      }
      }


      Actual Response



      {
      "took": 5,
      "timed_out": false,
      "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
      },
      "hits": {
      "total": 2,
      "max_score": 0,
      "hits": [
      {
      "_index": "test_idx",
      "_type": "t",
      "_id": "2",
      "_score": 0,
      "_source": {
      "numbers": [
      1,
      2,
      3
      ]
      }
      },
      {
      "_index": "test_idx",
      "_type": "t",
      "_id": "1",
      "_score": 0,
      "_source": {
      "strings": [
      "hello",
      "world"
      ]
      }
      }
      ]
      }
      }


      Expected Response



      {
      "took": 5,
      "timed_out": false,
      "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
      },
      "hits": {
      "total": 2,
      "max_score": 0,
      "hits": [
      {
      "_index": "test_idx",
      "_type": "t",
      "_id": "1",
      "_score": 0,
      "_source": {
      "strings": [
      "hello",
      "world"
      ]
      }
      }
      ]
      }
      }






      elasticsearch elasticsearch-painless






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 29 at 7:36









      Justin Wrobel

      1,47021730




      1,47021730










      asked Nov 23 '18 at 23:17









      MaryanMaryan

      134




      134
























          2 Answers
          2






          active

          oldest

          votes


















          0














          You might want to try this, even though it is strongly discouraged to overuse painless for performance reasons



          GET /test_idx/_search
          {
          "query": {
          "bool": {
          "filter": [
          {
          "script": {
          "script": {
          "lang": "painless",
          "inline": "return doc[params.keypath].value != null",
          "params": {
          "keypath": "strings.keyword"
          }
          }
          }
          }
          ]
          }
          }
          }





          share|improve this answer
























          • Thanks! this works, yet null could be a valid field value. which may lead to incorrect results… Yes, we're observing the performance, so far it's acceptable…

            – Maryan
            Nov 24 '18 at 5:16













          • when a field has a null value it is not at all indexed.

            – Val
            Nov 24 '18 at 5:17



















          0














          Why do you require painless to do so? This can be easily done by exists query



          {
          "query": {
          "exists": {
          "field": "strings"
          }
          }
          }





          share|improve this answer
























          • Using painless to do more complex checks, ie: field strings exactly equals ["hello", "world"]. Yes, exists works, but still wondering why isn't the containsKey functioning...

            – Maryan
            Nov 24 '18 at 3:35











          • If you have something complex I would suggest you to change the way you are storing data. For example instead of storing values as array, consider nested object. Try to avoid the use of script as relying more and more on script will degrade the performance.

            – Nishant Saini
            Nov 24 '18 at 4:52













          • We're benchmarking and will see whether it's acceptable. Also don't see why painless can't be compiled into the same underlying AST JSON pseudo AST gets compiled to.

            – Maryan
            Nov 24 '18 at 5:20






          • 1





            @Maryan the problem when using scripting is that the script (painless = java) has to be executed against each document and can't leverage all the benefits that the inverted index provides.

            – Val
            Nov 24 '18 at 7:25











          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%2f53453792%2fpainless-check-if-an-individual-document-contains-a-key%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You might want to try this, even though it is strongly discouraged to overuse painless for performance reasons



          GET /test_idx/_search
          {
          "query": {
          "bool": {
          "filter": [
          {
          "script": {
          "script": {
          "lang": "painless",
          "inline": "return doc[params.keypath].value != null",
          "params": {
          "keypath": "strings.keyword"
          }
          }
          }
          }
          ]
          }
          }
          }





          share|improve this answer
























          • Thanks! this works, yet null could be a valid field value. which may lead to incorrect results… Yes, we're observing the performance, so far it's acceptable…

            – Maryan
            Nov 24 '18 at 5:16













          • when a field has a null value it is not at all indexed.

            – Val
            Nov 24 '18 at 5:17
















          0














          You might want to try this, even though it is strongly discouraged to overuse painless for performance reasons



          GET /test_idx/_search
          {
          "query": {
          "bool": {
          "filter": [
          {
          "script": {
          "script": {
          "lang": "painless",
          "inline": "return doc[params.keypath].value != null",
          "params": {
          "keypath": "strings.keyword"
          }
          }
          }
          }
          ]
          }
          }
          }





          share|improve this answer
























          • Thanks! this works, yet null could be a valid field value. which may lead to incorrect results… Yes, we're observing the performance, so far it's acceptable…

            – Maryan
            Nov 24 '18 at 5:16













          • when a field has a null value it is not at all indexed.

            – Val
            Nov 24 '18 at 5:17














          0












          0








          0







          You might want to try this, even though it is strongly discouraged to overuse painless for performance reasons



          GET /test_idx/_search
          {
          "query": {
          "bool": {
          "filter": [
          {
          "script": {
          "script": {
          "lang": "painless",
          "inline": "return doc[params.keypath].value != null",
          "params": {
          "keypath": "strings.keyword"
          }
          }
          }
          }
          ]
          }
          }
          }





          share|improve this answer













          You might want to try this, even though it is strongly discouraged to overuse painless for performance reasons



          GET /test_idx/_search
          {
          "query": {
          "bool": {
          "filter": [
          {
          "script": {
          "script": {
          "lang": "painless",
          "inline": "return doc[params.keypath].value != null",
          "params": {
          "keypath": "strings.keyword"
          }
          }
          }
          }
          ]
          }
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 24 '18 at 4:21









          ValVal

          106k6143178




          106k6143178













          • Thanks! this works, yet null could be a valid field value. which may lead to incorrect results… Yes, we're observing the performance, so far it's acceptable…

            – Maryan
            Nov 24 '18 at 5:16













          • when a field has a null value it is not at all indexed.

            – Val
            Nov 24 '18 at 5:17



















          • Thanks! this works, yet null could be a valid field value. which may lead to incorrect results… Yes, we're observing the performance, so far it's acceptable…

            – Maryan
            Nov 24 '18 at 5:16













          • when a field has a null value it is not at all indexed.

            – Val
            Nov 24 '18 at 5:17

















          Thanks! this works, yet null could be a valid field value. which may lead to incorrect results… Yes, we're observing the performance, so far it's acceptable…

          – Maryan
          Nov 24 '18 at 5:16







          Thanks! this works, yet null could be a valid field value. which may lead to incorrect results… Yes, we're observing the performance, so far it's acceptable…

          – Maryan
          Nov 24 '18 at 5:16















          when a field has a null value it is not at all indexed.

          – Val
          Nov 24 '18 at 5:17





          when a field has a null value it is not at all indexed.

          – Val
          Nov 24 '18 at 5:17













          0














          Why do you require painless to do so? This can be easily done by exists query



          {
          "query": {
          "exists": {
          "field": "strings"
          }
          }
          }





          share|improve this answer
























          • Using painless to do more complex checks, ie: field strings exactly equals ["hello", "world"]. Yes, exists works, but still wondering why isn't the containsKey functioning...

            – Maryan
            Nov 24 '18 at 3:35











          • If you have something complex I would suggest you to change the way you are storing data. For example instead of storing values as array, consider nested object. Try to avoid the use of script as relying more and more on script will degrade the performance.

            – Nishant Saini
            Nov 24 '18 at 4:52













          • We're benchmarking and will see whether it's acceptable. Also don't see why painless can't be compiled into the same underlying AST JSON pseudo AST gets compiled to.

            – Maryan
            Nov 24 '18 at 5:20






          • 1





            @Maryan the problem when using scripting is that the script (painless = java) has to be executed against each document and can't leverage all the benefits that the inverted index provides.

            – Val
            Nov 24 '18 at 7:25
















          0














          Why do you require painless to do so? This can be easily done by exists query



          {
          "query": {
          "exists": {
          "field": "strings"
          }
          }
          }





          share|improve this answer
























          • Using painless to do more complex checks, ie: field strings exactly equals ["hello", "world"]. Yes, exists works, but still wondering why isn't the containsKey functioning...

            – Maryan
            Nov 24 '18 at 3:35











          • If you have something complex I would suggest you to change the way you are storing data. For example instead of storing values as array, consider nested object. Try to avoid the use of script as relying more and more on script will degrade the performance.

            – Nishant Saini
            Nov 24 '18 at 4:52













          • We're benchmarking and will see whether it's acceptable. Also don't see why painless can't be compiled into the same underlying AST JSON pseudo AST gets compiled to.

            – Maryan
            Nov 24 '18 at 5:20






          • 1





            @Maryan the problem when using scripting is that the script (painless = java) has to be executed against each document and can't leverage all the benefits that the inverted index provides.

            – Val
            Nov 24 '18 at 7:25














          0












          0








          0







          Why do you require painless to do so? This can be easily done by exists query



          {
          "query": {
          "exists": {
          "field": "strings"
          }
          }
          }





          share|improve this answer













          Why do you require painless to do so? This can be easily done by exists query



          {
          "query": {
          "exists": {
          "field": "strings"
          }
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 24 '18 at 2:26









          Nishant SainiNishant Saini

          1,7591018




          1,7591018













          • Using painless to do more complex checks, ie: field strings exactly equals ["hello", "world"]. Yes, exists works, but still wondering why isn't the containsKey functioning...

            – Maryan
            Nov 24 '18 at 3:35











          • If you have something complex I would suggest you to change the way you are storing data. For example instead of storing values as array, consider nested object. Try to avoid the use of script as relying more and more on script will degrade the performance.

            – Nishant Saini
            Nov 24 '18 at 4:52













          • We're benchmarking and will see whether it's acceptable. Also don't see why painless can't be compiled into the same underlying AST JSON pseudo AST gets compiled to.

            – Maryan
            Nov 24 '18 at 5:20






          • 1





            @Maryan the problem when using scripting is that the script (painless = java) has to be executed against each document and can't leverage all the benefits that the inverted index provides.

            – Val
            Nov 24 '18 at 7:25



















          • Using painless to do more complex checks, ie: field strings exactly equals ["hello", "world"]. Yes, exists works, but still wondering why isn't the containsKey functioning...

            – Maryan
            Nov 24 '18 at 3:35











          • If you have something complex I would suggest you to change the way you are storing data. For example instead of storing values as array, consider nested object. Try to avoid the use of script as relying more and more on script will degrade the performance.

            – Nishant Saini
            Nov 24 '18 at 4:52













          • We're benchmarking and will see whether it's acceptable. Also don't see why painless can't be compiled into the same underlying AST JSON pseudo AST gets compiled to.

            – Maryan
            Nov 24 '18 at 5:20






          • 1





            @Maryan the problem when using scripting is that the script (painless = java) has to be executed against each document and can't leverage all the benefits that the inverted index provides.

            – Val
            Nov 24 '18 at 7:25

















          Using painless to do more complex checks, ie: field strings exactly equals ["hello", "world"]. Yes, exists works, but still wondering why isn't the containsKey functioning...

          – Maryan
          Nov 24 '18 at 3:35





          Using painless to do more complex checks, ie: field strings exactly equals ["hello", "world"]. Yes, exists works, but still wondering why isn't the containsKey functioning...

          – Maryan
          Nov 24 '18 at 3:35













          If you have something complex I would suggest you to change the way you are storing data. For example instead of storing values as array, consider nested object. Try to avoid the use of script as relying more and more on script will degrade the performance.

          – Nishant Saini
          Nov 24 '18 at 4:52







          If you have something complex I would suggest you to change the way you are storing data. For example instead of storing values as array, consider nested object. Try to avoid the use of script as relying more and more on script will degrade the performance.

          – Nishant Saini
          Nov 24 '18 at 4:52















          We're benchmarking and will see whether it's acceptable. Also don't see why painless can't be compiled into the same underlying AST JSON pseudo AST gets compiled to.

          – Maryan
          Nov 24 '18 at 5:20





          We're benchmarking and will see whether it's acceptable. Also don't see why painless can't be compiled into the same underlying AST JSON pseudo AST gets compiled to.

          – Maryan
          Nov 24 '18 at 5:20




          1




          1





          @Maryan the problem when using scripting is that the script (painless = java) has to be executed against each document and can't leverage all the benefits that the inverted index provides.

          – Val
          Nov 24 '18 at 7:25





          @Maryan the problem when using scripting is that the script (painless = java) has to be executed against each document and can't leverage all the benefits that the inverted index provides.

          – Val
          Nov 24 '18 at 7:25


















          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%2f53453792%2fpainless-check-if-an-individual-document-contains-a-key%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

          Costa Masnaga

          Fotorealismo

          Sidney Franklin