Elasticsearch aggregation by field name











up vote
0
down vote

favorite












Imagine two documents:



[
{
"_id": "abc",
"categories": {
"category-id-1": 1,
"category-id-2": 50
}
},
{
"_id": "def",
"categories": {
"category-id-1": 2
}
}
]


As you can see, each document can be associated with a number of categories, by setting a nested field into the categories field.



With this mapping, I should be able to request the documents from a defined category and to order them by the value set as value for this field.



My problem is that I now want to make an aggregation to count for each category the number of documents. That would give the following result for the dataset I provided:



{
"aggregations": {
"categories" : {
"buckets": [
{
"key": "category-id-1",
"doc_count": 2
},
{
"key": "category-id-2",
"doc_count": 1
}
]
}
}
}


I can't find anything in the documentation to solve this problem. I'm completely new to ElasticSearch so I may be doing something wrong either on my documentation research or on my mapping choice.



Is it possible to make this kind of aggregation with my mapping? I'm using ES 6.x



EDIT: Here is the mapping for the index:



{
"test1234": {
"mappings": {
"_doc": {
"properties": {
"categories": {
"properties": {
"category-id-1": {
"type": "long"
},
"category-id-2": {
"type": "long"
}
}
}
}
}
}
}
}









share|improve this question
























  • could you post your mapping?
    – Lupanoide
    Nov 19 at 15:20










  • Hi, thank you for commenting, I added the mapping to the question.
    – Hammerbot
    Nov 19 at 15:23










  • so if I understand well you would like to retrieve not the aggregated values of key category-id-1 , but the number of docs which have the field category-id-1, right?
    – Lupanoide
    Nov 19 at 15:26












  • Exactly, without mentioning category-id-1 nor category-id-2 in my request.
    – Hammerbot
    Nov 19 at 15:27















up vote
0
down vote

favorite












Imagine two documents:



[
{
"_id": "abc",
"categories": {
"category-id-1": 1,
"category-id-2": 50
}
},
{
"_id": "def",
"categories": {
"category-id-1": 2
}
}
]


As you can see, each document can be associated with a number of categories, by setting a nested field into the categories field.



With this mapping, I should be able to request the documents from a defined category and to order them by the value set as value for this field.



My problem is that I now want to make an aggregation to count for each category the number of documents. That would give the following result for the dataset I provided:



{
"aggregations": {
"categories" : {
"buckets": [
{
"key": "category-id-1",
"doc_count": 2
},
{
"key": "category-id-2",
"doc_count": 1
}
]
}
}
}


I can't find anything in the documentation to solve this problem. I'm completely new to ElasticSearch so I may be doing something wrong either on my documentation research or on my mapping choice.



Is it possible to make this kind of aggregation with my mapping? I'm using ES 6.x



EDIT: Here is the mapping for the index:



{
"test1234": {
"mappings": {
"_doc": {
"properties": {
"categories": {
"properties": {
"category-id-1": {
"type": "long"
},
"category-id-2": {
"type": "long"
}
}
}
}
}
}
}
}









share|improve this question
























  • could you post your mapping?
    – Lupanoide
    Nov 19 at 15:20










  • Hi, thank you for commenting, I added the mapping to the question.
    – Hammerbot
    Nov 19 at 15:23










  • so if I understand well you would like to retrieve not the aggregated values of key category-id-1 , but the number of docs which have the field category-id-1, right?
    – Lupanoide
    Nov 19 at 15:26












  • Exactly, without mentioning category-id-1 nor category-id-2 in my request.
    – Hammerbot
    Nov 19 at 15:27













up vote
0
down vote

favorite









up vote
0
down vote

favorite











Imagine two documents:



[
{
"_id": "abc",
"categories": {
"category-id-1": 1,
"category-id-2": 50
}
},
{
"_id": "def",
"categories": {
"category-id-1": 2
}
}
]


As you can see, each document can be associated with a number of categories, by setting a nested field into the categories field.



With this mapping, I should be able to request the documents from a defined category and to order them by the value set as value for this field.



My problem is that I now want to make an aggregation to count for each category the number of documents. That would give the following result for the dataset I provided:



{
"aggregations": {
"categories" : {
"buckets": [
{
"key": "category-id-1",
"doc_count": 2
},
{
"key": "category-id-2",
"doc_count": 1
}
]
}
}
}


I can't find anything in the documentation to solve this problem. I'm completely new to ElasticSearch so I may be doing something wrong either on my documentation research or on my mapping choice.



Is it possible to make this kind of aggregation with my mapping? I'm using ES 6.x



EDIT: Here is the mapping for the index:



{
"test1234": {
"mappings": {
"_doc": {
"properties": {
"categories": {
"properties": {
"category-id-1": {
"type": "long"
},
"category-id-2": {
"type": "long"
}
}
}
}
}
}
}
}









share|improve this question















Imagine two documents:



[
{
"_id": "abc",
"categories": {
"category-id-1": 1,
"category-id-2": 50
}
},
{
"_id": "def",
"categories": {
"category-id-1": 2
}
}
]


As you can see, each document can be associated with a number of categories, by setting a nested field into the categories field.



With this mapping, I should be able to request the documents from a defined category and to order them by the value set as value for this field.



My problem is that I now want to make an aggregation to count for each category the number of documents. That would give the following result for the dataset I provided:



{
"aggregations": {
"categories" : {
"buckets": [
{
"key": "category-id-1",
"doc_count": 2
},
{
"key": "category-id-2",
"doc_count": 1
}
]
}
}
}


I can't find anything in the documentation to solve this problem. I'm completely new to ElasticSearch so I may be doing something wrong either on my documentation research or on my mapping choice.



Is it possible to make this kind of aggregation with my mapping? I'm using ES 6.x



EDIT: Here is the mapping for the index:



{
"test1234": {
"mappings": {
"_doc": {
"properties": {
"categories": {
"properties": {
"category-id-1": {
"type": "long"
},
"category-id-2": {
"type": "long"
}
}
}
}
}
}
}
}






elasticsearch






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 15:23

























asked Nov 19 at 15:12









Hammerbot

5,10031647




5,10031647












  • could you post your mapping?
    – Lupanoide
    Nov 19 at 15:20










  • Hi, thank you for commenting, I added the mapping to the question.
    – Hammerbot
    Nov 19 at 15:23










  • so if I understand well you would like to retrieve not the aggregated values of key category-id-1 , but the number of docs which have the field category-id-1, right?
    – Lupanoide
    Nov 19 at 15:26












  • Exactly, without mentioning category-id-1 nor category-id-2 in my request.
    – Hammerbot
    Nov 19 at 15:27


















  • could you post your mapping?
    – Lupanoide
    Nov 19 at 15:20










  • Hi, thank you for commenting, I added the mapping to the question.
    – Hammerbot
    Nov 19 at 15:23










  • so if I understand well you would like to retrieve not the aggregated values of key category-id-1 , but the number of docs which have the field category-id-1, right?
    – Lupanoide
    Nov 19 at 15:26












  • Exactly, without mentioning category-id-1 nor category-id-2 in my request.
    – Hammerbot
    Nov 19 at 15:27
















could you post your mapping?
– Lupanoide
Nov 19 at 15:20




could you post your mapping?
– Lupanoide
Nov 19 at 15:20












Hi, thank you for commenting, I added the mapping to the question.
– Hammerbot
Nov 19 at 15:23




Hi, thank you for commenting, I added the mapping to the question.
– Hammerbot
Nov 19 at 15:23












so if I understand well you would like to retrieve not the aggregated values of key category-id-1 , but the number of docs which have the field category-id-1, right?
– Lupanoide
Nov 19 at 15:26






so if I understand well you would like to retrieve not the aggregated values of key category-id-1 , but the number of docs which have the field category-id-1, right?
– Lupanoide
Nov 19 at 15:26














Exactly, without mentioning category-id-1 nor category-id-2 in my request.
– Hammerbot
Nov 19 at 15:27




Exactly, without mentioning category-id-1 nor category-id-2 in my request.
– Hammerbot
Nov 19 at 15:27












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










The most straightforward solution is to use a new field that contains all the distinct categories of a document.



If we call this field categories_list here could be a solution :



Change the mapping to



{
"test1234": {
"mappings": {
"_doc": {
"properties": {
"categories": {
"properties": {
"category-id-1": {
"type": "long"
},
"category-id-2": {
"type": "long"
}
}
},
"categories_list": {
"type": "keyword"
}
}
}
}
}
}


Then you need to modify your documents like this :



[
{
"_id": "abc",
"categories": {
"category-id-1": 1,
"category-id-2": 50
},
"categories_list": ["category-id-1", "category-id-2"]
},
{
"_id": "def",
"categories": {
"category-id-1": 2
},
"categories_list": ["category-id-1"]
}
]


then your aggregation request should be



{
"aggs": {
"categories": {
"terms": {
"field": "categories_list",
"size": 10
}
}
}
}


and will return



"aggregations": {
"categories": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "category-id-1",
"doc_count": 2
},
{
"key": "category-id-2",
"doc_count": 1
}
]
}
}





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%2f53377556%2felasticsearch-aggregation-by-field-name%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










    The most straightforward solution is to use a new field that contains all the distinct categories of a document.



    If we call this field categories_list here could be a solution :



    Change the mapping to



    {
    "test1234": {
    "mappings": {
    "_doc": {
    "properties": {
    "categories": {
    "properties": {
    "category-id-1": {
    "type": "long"
    },
    "category-id-2": {
    "type": "long"
    }
    }
    },
    "categories_list": {
    "type": "keyword"
    }
    }
    }
    }
    }
    }


    Then you need to modify your documents like this :



    [
    {
    "_id": "abc",
    "categories": {
    "category-id-1": 1,
    "category-id-2": 50
    },
    "categories_list": ["category-id-1", "category-id-2"]
    },
    {
    "_id": "def",
    "categories": {
    "category-id-1": 2
    },
    "categories_list": ["category-id-1"]
    }
    ]


    then your aggregation request should be



    {
    "aggs": {
    "categories": {
    "terms": {
    "field": "categories_list",
    "size": 10
    }
    }
    }
    }


    and will return



    "aggregations": {
    "categories": {
    "doc_count_error_upper_bound": 0,
    "sum_other_doc_count": 0,
    "buckets": [
    {
    "key": "category-id-1",
    "doc_count": 2
    },
    {
    "key": "category-id-2",
    "doc_count": 1
    }
    ]
    }
    }





    share|improve this answer

























      up vote
      1
      down vote



      accepted










      The most straightforward solution is to use a new field that contains all the distinct categories of a document.



      If we call this field categories_list here could be a solution :



      Change the mapping to



      {
      "test1234": {
      "mappings": {
      "_doc": {
      "properties": {
      "categories": {
      "properties": {
      "category-id-1": {
      "type": "long"
      },
      "category-id-2": {
      "type": "long"
      }
      }
      },
      "categories_list": {
      "type": "keyword"
      }
      }
      }
      }
      }
      }


      Then you need to modify your documents like this :



      [
      {
      "_id": "abc",
      "categories": {
      "category-id-1": 1,
      "category-id-2": 50
      },
      "categories_list": ["category-id-1", "category-id-2"]
      },
      {
      "_id": "def",
      "categories": {
      "category-id-1": 2
      },
      "categories_list": ["category-id-1"]
      }
      ]


      then your aggregation request should be



      {
      "aggs": {
      "categories": {
      "terms": {
      "field": "categories_list",
      "size": 10
      }
      }
      }
      }


      and will return



      "aggregations": {
      "categories": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
      {
      "key": "category-id-1",
      "doc_count": 2
      },
      {
      "key": "category-id-2",
      "doc_count": 1
      }
      ]
      }
      }





      share|improve this answer























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        The most straightforward solution is to use a new field that contains all the distinct categories of a document.



        If we call this field categories_list here could be a solution :



        Change the mapping to



        {
        "test1234": {
        "mappings": {
        "_doc": {
        "properties": {
        "categories": {
        "properties": {
        "category-id-1": {
        "type": "long"
        },
        "category-id-2": {
        "type": "long"
        }
        }
        },
        "categories_list": {
        "type": "keyword"
        }
        }
        }
        }
        }
        }


        Then you need to modify your documents like this :



        [
        {
        "_id": "abc",
        "categories": {
        "category-id-1": 1,
        "category-id-2": 50
        },
        "categories_list": ["category-id-1", "category-id-2"]
        },
        {
        "_id": "def",
        "categories": {
        "category-id-1": 2
        },
        "categories_list": ["category-id-1"]
        }
        ]


        then your aggregation request should be



        {
        "aggs": {
        "categories": {
        "terms": {
        "field": "categories_list",
        "size": 10
        }
        }
        }
        }


        and will return



        "aggregations": {
        "categories": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
        {
        "key": "category-id-1",
        "doc_count": 2
        },
        {
        "key": "category-id-2",
        "doc_count": 1
        }
        ]
        }
        }





        share|improve this answer












        The most straightforward solution is to use a new field that contains all the distinct categories of a document.



        If we call this field categories_list here could be a solution :



        Change the mapping to



        {
        "test1234": {
        "mappings": {
        "_doc": {
        "properties": {
        "categories": {
        "properties": {
        "category-id-1": {
        "type": "long"
        },
        "category-id-2": {
        "type": "long"
        }
        }
        },
        "categories_list": {
        "type": "keyword"
        }
        }
        }
        }
        }
        }


        Then you need to modify your documents like this :



        [
        {
        "_id": "abc",
        "categories": {
        "category-id-1": 1,
        "category-id-2": 50
        },
        "categories_list": ["category-id-1", "category-id-2"]
        },
        {
        "_id": "def",
        "categories": {
        "category-id-1": 2
        },
        "categories_list": ["category-id-1"]
        }
        ]


        then your aggregation request should be



        {
        "aggs": {
        "categories": {
        "terms": {
        "field": "categories_list",
        "size": 10
        }
        }
        }
        }


        and will return



        "aggregations": {
        "categories": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
        {
        "key": "category-id-1",
        "doc_count": 2
        },
        {
        "key": "category-id-2",
        "doc_count": 1
        }
        ]
        }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 at 10:03









        Pierre Mallet

        3,0231416




        3,0231416






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53377556%2felasticsearch-aggregation-by-field-name%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

            To store a contact into the json file from server.js file using a class in NodeJS

            Marschland

            Redirect URL with Chrome Remote Debugging Android Devices