Elasticsearch: How can I get an count of documents by the number of items within a property?
up vote
1
down vote
favorite
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
add a comment |
up vote
1
down vote
favorite
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
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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
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
elasticsearch
edited Nov 26 at 12:43
asked Nov 20 at 13:47
nybblesAndBits
11818
11818
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
You can do it like this:
{
"size": 0,
"aggs": {
"length": {
"terms": {
"script": {
"source": "params._source.vendors.length",
"lang": "painless"
}
}
}
}
}
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 at 16:56
If you tell me which version you have, I can provide a query that works with your version
– Val
Nov 20 at 16:57
Sorry, it's version 2.4. I added that to the original question too.
– nybblesAndBits
Nov 20 at 16:59
Ok, glad you figured it out.
– Val
Nov 20 at 16:59
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
up vote
1
down vote
accepted
You can do it like this:
{
"size": 0,
"aggs": {
"length": {
"terms": {
"script": {
"source": "params._source.vendors.length",
"lang": "painless"
}
}
}
}
}
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 at 16:56
If you tell me which version you have, I can provide a query that works with your version
– Val
Nov 20 at 16:57
Sorry, it's version 2.4. I added that to the original question too.
– nybblesAndBits
Nov 20 at 16:59
Ok, glad you figured it out.
– Val
Nov 20 at 16:59
add a comment |
up vote
1
down vote
accepted
You can do it like this:
{
"size": 0,
"aggs": {
"length": {
"terms": {
"script": {
"source": "params._source.vendors.length",
"lang": "painless"
}
}
}
}
}
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 at 16:56
If you tell me which version you have, I can provide a query that works with your version
– Val
Nov 20 at 16:57
Sorry, it's version 2.4. I added that to the original question too.
– nybblesAndBits
Nov 20 at 16:59
Ok, glad you figured it out.
– Val
Nov 20 at 16:59
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
You can do it like this:
{
"size": 0,
"aggs": {
"length": {
"terms": {
"script": {
"source": "params._source.vendors.length",
"lang": "painless"
}
}
}
}
}
You can do it like this:
{
"size": 0,
"aggs": {
"length": {
"terms": {
"script": {
"source": "params._source.vendors.length",
"lang": "painless"
}
}
}
}
}
answered Nov 20 at 13:54
Val
100k6131168
100k6131168
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 at 16:56
If you tell me which version you have, I can provide a query that works with your version
– Val
Nov 20 at 16:57
Sorry, it's version 2.4. I added that to the original question too.
– nybblesAndBits
Nov 20 at 16:59
Ok, glad you figured it out.
– Val
Nov 20 at 16:59
add a comment |
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 at 16:56
If you tell me which version you have, I can provide a query that works with your version
– Val
Nov 20 at 16:57
Sorry, it's version 2.4. I added that to the original question too.
– nybblesAndBits
Nov 20 at 16:59
Ok, glad you figured it out.
– Val
Nov 20 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 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 at 16:56
If you tell me which version you have, I can provide a query that works with your version
– Val
Nov 20 at 16:57
If you tell me which version you have, I can provide a query that works with your version
– Val
Nov 20 at 16:57
Sorry, it's version 2.4. I added that to the original question too.
– nybblesAndBits
Nov 20 at 16:59
Sorry, it's version 2.4. I added that to the original question too.
– nybblesAndBits
Nov 20 at 16:59
Ok, glad you figured it out.
– Val
Nov 20 at 16:59
Ok, glad you figured it out.
– Val
Nov 20 at 16:59
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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