Elasticsearch: How can I get an count of documents by the number of items within a property?












1















Let's say I have an Elasticsearch documents that look like this



{
"_index": "sample.index",
"_type": "inventory",
"_id": "ebcc7f5c-1697-438f-911f-b8e9ff5554e9",
"_score": 1,
"_source": {
"name": "salt",
"properties": {
"vendors": ["vendor 1", "vendor 2"]
}
}
},
{
"_index": "sample.index",
"_type": "inventory",
"_id": "f5875c5c-9bf1-448b-99d9-47d873d4f016",
"_score": 1,
"_source": {
"name": "pepper",
"properties": {
"vendors": ["vendor 1"]
}
}
}


What I would like to do is get the count of items that can come from one vendor, two vendors, etc... Basically, a query based on the length of the vendors array.



This is the type of output I'm trying to obtain, or something similar.



{
"aggregations": {
"vendor_counts": {
"buckets": [
{
"vendor_array_length": "1",
"doc_count": 1
},
{
"vendor_array_length": "2",
"doc_count": 1
}
]
}
}
}


Thanks in advance for any help :)



EDIT: The below accepted answer appears to work for newer versions of ES, but I'm having to use an older version (I found that out after originally posting this question). The following query works for me on ES 2.4.



{
"size": 0,
"aggs": {
"length": {
"terms": {
"script": "_source.vendors.size()"
}
}
}
}









share|improve this question





























    1















    Let's say I have an Elasticsearch documents that look like this



    {
    "_index": "sample.index",
    "_type": "inventory",
    "_id": "ebcc7f5c-1697-438f-911f-b8e9ff5554e9",
    "_score": 1,
    "_source": {
    "name": "salt",
    "properties": {
    "vendors": ["vendor 1", "vendor 2"]
    }
    }
    },
    {
    "_index": "sample.index",
    "_type": "inventory",
    "_id": "f5875c5c-9bf1-448b-99d9-47d873d4f016",
    "_score": 1,
    "_source": {
    "name": "pepper",
    "properties": {
    "vendors": ["vendor 1"]
    }
    }
    }


    What I would like to do is get the count of items that can come from one vendor, two vendors, etc... Basically, a query based on the length of the vendors array.



    This is the type of output I'm trying to obtain, or something similar.



    {
    "aggregations": {
    "vendor_counts": {
    "buckets": [
    {
    "vendor_array_length": "1",
    "doc_count": 1
    },
    {
    "vendor_array_length": "2",
    "doc_count": 1
    }
    ]
    }
    }
    }


    Thanks in advance for any help :)



    EDIT: The below accepted answer appears to work for newer versions of ES, but I'm having to use an older version (I found that out after originally posting this question). The following query works for me on ES 2.4.



    {
    "size": 0,
    "aggs": {
    "length": {
    "terms": {
    "script": "_source.vendors.size()"
    }
    }
    }
    }









    share|improve this question



























      1












      1








      1








      Let's say I have an Elasticsearch documents that look like this



      {
      "_index": "sample.index",
      "_type": "inventory",
      "_id": "ebcc7f5c-1697-438f-911f-b8e9ff5554e9",
      "_score": 1,
      "_source": {
      "name": "salt",
      "properties": {
      "vendors": ["vendor 1", "vendor 2"]
      }
      }
      },
      {
      "_index": "sample.index",
      "_type": "inventory",
      "_id": "f5875c5c-9bf1-448b-99d9-47d873d4f016",
      "_score": 1,
      "_source": {
      "name": "pepper",
      "properties": {
      "vendors": ["vendor 1"]
      }
      }
      }


      What I would like to do is get the count of items that can come from one vendor, two vendors, etc... Basically, a query based on the length of the vendors array.



      This is the type of output I'm trying to obtain, or something similar.



      {
      "aggregations": {
      "vendor_counts": {
      "buckets": [
      {
      "vendor_array_length": "1",
      "doc_count": 1
      },
      {
      "vendor_array_length": "2",
      "doc_count": 1
      }
      ]
      }
      }
      }


      Thanks in advance for any help :)



      EDIT: The below accepted answer appears to work for newer versions of ES, but I'm having to use an older version (I found that out after originally posting this question). The following query works for me on ES 2.4.



      {
      "size": 0,
      "aggs": {
      "length": {
      "terms": {
      "script": "_source.vendors.size()"
      }
      }
      }
      }









      share|improve this question
















      Let's say I have an Elasticsearch documents that look like this



      {
      "_index": "sample.index",
      "_type": "inventory",
      "_id": "ebcc7f5c-1697-438f-911f-b8e9ff5554e9",
      "_score": 1,
      "_source": {
      "name": "salt",
      "properties": {
      "vendors": ["vendor 1", "vendor 2"]
      }
      }
      },
      {
      "_index": "sample.index",
      "_type": "inventory",
      "_id": "f5875c5c-9bf1-448b-99d9-47d873d4f016",
      "_score": 1,
      "_source": {
      "name": "pepper",
      "properties": {
      "vendors": ["vendor 1"]
      }
      }
      }


      What I would like to do is get the count of items that can come from one vendor, two vendors, etc... Basically, a query based on the length of the vendors array.



      This is the type of output I'm trying to obtain, or something similar.



      {
      "aggregations": {
      "vendor_counts": {
      "buckets": [
      {
      "vendor_array_length": "1",
      "doc_count": 1
      },
      {
      "vendor_array_length": "2",
      "doc_count": 1
      }
      ]
      }
      }
      }


      Thanks in advance for any help :)



      EDIT: The below accepted answer appears to work for newer versions of ES, but I'm having to use an older version (I found that out after originally posting this question). The following query works for me on ES 2.4.



      {
      "size": 0,
      "aggs": {
      "length": {
      "terms": {
      "script": "_source.vendors.size()"
      }
      }
      }
      }






      elasticsearch






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 26 '18 at 12:43







      nybblesAndBits

















      asked Nov 20 '18 at 13:47









      nybblesAndBitsnybblesAndBits

      11818




      11818
























          1 Answer
          1






          active

          oldest

          votes


















          1














          You can do it like this:



          {
          "size": 0,
          "aggs": {
          "length": {
          "terms": {
          "script": {
          "source": "params._source.vendors.length",
          "lang": "painless"
          }
          }
          }
          }
          }





          share|improve this answer
























          • This didn't work for me because I found out that the version of ES I'm having to work with is older. However, it appears your query works for newer versions. You did inspire me and I managed to put together another query. I'll post it below. Thanks!!!

            – nybblesAndBits
            Nov 20 '18 at 16:56











          • If you tell me which version you have, I can provide a query that works with your version

            – Val
            Nov 20 '18 at 16:57











          • Sorry, it's version 2.4. I added that to the original question too.

            – nybblesAndBits
            Nov 20 '18 at 16:59











          • Ok, glad you figured it out.

            – Val
            Nov 20 '18 at 16:59











          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%2f53394455%2felasticsearch-how-can-i-get-an-count-of-documents-by-the-number-of-items-within%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









          1














          You can do it like this:



          {
          "size": 0,
          "aggs": {
          "length": {
          "terms": {
          "script": {
          "source": "params._source.vendors.length",
          "lang": "painless"
          }
          }
          }
          }
          }





          share|improve this answer
























          • This didn't work for me because I found out that the version of ES I'm having to work with is older. However, it appears your query works for newer versions. You did inspire me and I managed to put together another query. I'll post it below. Thanks!!!

            – nybblesAndBits
            Nov 20 '18 at 16:56











          • If you tell me which version you have, I can provide a query that works with your version

            – Val
            Nov 20 '18 at 16:57











          • Sorry, it's version 2.4. I added that to the original question too.

            – nybblesAndBits
            Nov 20 '18 at 16:59











          • Ok, glad you figured it out.

            – Val
            Nov 20 '18 at 16:59
















          1














          You can do it like this:



          {
          "size": 0,
          "aggs": {
          "length": {
          "terms": {
          "script": {
          "source": "params._source.vendors.length",
          "lang": "painless"
          }
          }
          }
          }
          }





          share|improve this answer
























          • This didn't work for me because I found out that the version of ES I'm having to work with is older. However, it appears your query works for newer versions. You did inspire me and I managed to put together another query. I'll post it below. Thanks!!!

            – nybblesAndBits
            Nov 20 '18 at 16:56











          • If you tell me which version you have, I can provide a query that works with your version

            – Val
            Nov 20 '18 at 16:57











          • Sorry, it's version 2.4. I added that to the original question too.

            – nybblesAndBits
            Nov 20 '18 at 16:59











          • Ok, glad you figured it out.

            – Val
            Nov 20 '18 at 16:59














          1












          1








          1







          You can do it like this:



          {
          "size": 0,
          "aggs": {
          "length": {
          "terms": {
          "script": {
          "source": "params._source.vendors.length",
          "lang": "painless"
          }
          }
          }
          }
          }





          share|improve this answer













          You can do it like this:



          {
          "size": 0,
          "aggs": {
          "length": {
          "terms": {
          "script": {
          "source": "params._source.vendors.length",
          "lang": "painless"
          }
          }
          }
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 '18 at 13:54









          ValVal

          103k6139174




          103k6139174













          • This didn't work for me because I found out that the version of ES I'm having to work with is older. However, it appears your query works for newer versions. You did inspire me and I managed to put together another query. I'll post it below. Thanks!!!

            – nybblesAndBits
            Nov 20 '18 at 16:56











          • If you tell me which version you have, I can provide a query that works with your version

            – Val
            Nov 20 '18 at 16:57











          • Sorry, it's version 2.4. I added that to the original question too.

            – nybblesAndBits
            Nov 20 '18 at 16:59











          • Ok, glad you figured it out.

            – Val
            Nov 20 '18 at 16:59



















          • This didn't work for me because I found out that the version of ES I'm having to work with is older. However, it appears your query works for newer versions. You did inspire me and I managed to put together another query. I'll post it below. Thanks!!!

            – nybblesAndBits
            Nov 20 '18 at 16:56











          • If you tell me which version you have, I can provide a query that works with your version

            – Val
            Nov 20 '18 at 16:57











          • Sorry, it's version 2.4. I added that to the original question too.

            – nybblesAndBits
            Nov 20 '18 at 16:59











          • Ok, glad you figured it out.

            – Val
            Nov 20 '18 at 16:59

















          This didn't work for me because I found out that the version of ES I'm having to work with is older. However, it appears your query works for newer versions. You did inspire me and I managed to put together another query. I'll post it below. Thanks!!!

          – nybblesAndBits
          Nov 20 '18 at 16:56





          This didn't work for me because I found out that the version of ES I'm having to work with is older. However, it appears your query works for newer versions. You did inspire me and I managed to put together another query. I'll post it below. Thanks!!!

          – nybblesAndBits
          Nov 20 '18 at 16:56













          If you tell me which version you have, I can provide a query that works with your version

          – Val
          Nov 20 '18 at 16:57





          If you tell me which version you have, I can provide a query that works with your version

          – Val
          Nov 20 '18 at 16:57













          Sorry, it's version 2.4. I added that to the original question too.

          – nybblesAndBits
          Nov 20 '18 at 16:59





          Sorry, it's version 2.4. I added that to the original question too.

          – nybblesAndBits
          Nov 20 '18 at 16:59













          Ok, glad you figured it out.

          – Val
          Nov 20 '18 at 16:59





          Ok, glad you figured it out.

          – Val
          Nov 20 '18 at 16:59


















          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%2f53394455%2felasticsearch-how-can-i-get-an-count-of-documents-by-the-number-of-items-within%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

          MongoDB - Not Authorized To Execute Command

          How to fix TextFormField cause rebuild widget in Flutter

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith