# App Search - Listing out Categories with Image and Item counts

**URL:** https://discuss.elastic.co/t/app-search-listing-out-categories-with-image-and-item-counts/281358
**Category:** Elastic Search
**Tags:** elastic-app-search
**Created:** [August 13, 2021, 1:06pm UTC](https://discuss.elastic.co/t/app-search-listing-out-categories-with-image-and-item-counts/281358 "2021-08-13T13:06:28Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![selectedpixel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/selectedpixel/32/92792_2.png) [@selectedpixel](https://discuss.elastic.co/u/selectedpixel)
#### Post date: [August 13, 2021, 1:06pm UTC](https://discuss.elastic.co/t/app-search-listing-out-categories-with-image-and-item-counts/281358/1 "2021-08-13T13:06:28Z")

</div>

I have an Elasticsearch Index setup with the following document structure.  
**NOTE: THIS IS BEING DONE USING APP SEARCH SO I'm UNABLE TO USE NESTED FIELDS**

```auto
    [
        {
          "id":id,
          "title: "Sample Title",
          "created_at":timestamp
          "categories:[
              {
                  "name": "Category 1"
                  "image": "image.png",
              },
              {
                  "name": "Category 2"
                  "image": "image.png",
              },
              {
                  "name": "Category 3"
                  "image": "image.png",
              },
              {
                  "name": "Category 4"
                  "image": "image.png",
              },
              ....
          ]
        },
        ....
    ]

```

This index will contain hundreds of thousands of records in this format.  
I'm trying to figure out how to optimally setup a query and structure this data so I can setup a query that will allow me to display a list of categories to the end user in this fashion.

- [image] Category 1 (20)
- [image] Category 2 (30)
- [image] Category 3 (10)

This query would be able to be filtered by the created\_at timestamp so that the counts would adjust based on the number of items like such.

- [image] Category 1 (10)
- [image] Category 2 (5)
- [image] Category 3 (25)

Using Aggregations to accomplish this gets me very close, but I'm unable to figure out what the category image would be because the aggregations don't bring back anything but a single value and the count.

Thank you!

---

<div class="post-metadata">

### Author: ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)
#### Post date: [August 15, 2021, 5:52pm UTC](https://discuss.elastic.co/t/app-search-listing-out-categories-with-image-and-item-counts/281358/2 "2021-08-15T17:52:54Z")

</div>

Hi @selectedpixel

I updated your title and tags so hopefully an App Search person may take a look.

---

<div class="post-metadata">

### Author: ![selectedpixel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/selectedpixel/32/92792_2.png) [@selectedpixel](https://discuss.elastic.co/u/selectedpixel)
#### Post date: [August 16, 2021, 12:20pm UTC](https://discuss.elastic.co/t/app-search-listing-out-categories-with-image-and-item-counts/281358/3 "2021-08-16T12:20:10Z")

</div>

I ended up using an explicit mapping on the category property with a type of "nested". Past that I used a composite aggregation to return a list of buckets with values that contained a key that have the category name, id and image reference.

You can see this SO post for a few more details.

> <https://stackoverflow.com/questions/68641499/elasticsearch-query-and-filter-on-index-with-array-of-nested-objects>

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [September 14, 2021, 7:00am UTC](https://discuss.elastic.co/t/app-search-listing-out-categories-with-image-and-item-counts/281358/5 "2021-09-14T07:00:26Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
