# Faceted Search Query

**URL:** <https://discuss.elastic.co/t/faceted-search-query/295351>\
**Category:** Elasticsearch\
**Created:** [January 25, 2022, 1:31pm UTC](https://discuss.elastic.co/t/faceted-search-query/295351 "2022-01-25T13:31:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrew\_1555](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@andrew\_1555](https://discuss.elastic.co/u/andrew_1555)\
**Post date:** [January 25, 2022, 1:31pm UTC](https://discuss.elastic.co/t/faceted-search-query/295351/1 "2022-01-25T13:31:38Z")

</div>

So I am trying to write a query which returns aggregations of facets. So for example (Red: 12, Green: 8, XL : 4 etc) where the numbers are the document counts.

I have been following this article which explains what I am trying to achieve

> **[Faceted navigation for e-commerce with Elasticsearch](https://medium.com/swlh/faceted-navigation-for-e-commerce-with-elasticsearch-dbf82bdeb7d4)**
>
> My colleague and I (the credit has to go to Dimitry Apter who’s done most of the actual tangible work) have recently been commissioned…

The difference for me, is I am using nested documents. So instead of having Size, Colour, Fabric as separate objects in the documents, I have a nested facet type (facet name, facet value) so (Colour Red, Size XS) etc.

When a customer filters by Colour & Size. So Colour Red and Size 16 for example. If we use the results aggregations we only get one colour now and one size (Red & 16) as the results are only for Red & 16. So what we need to do is in the aggs, we get all sizes which are red, and we get all colours which are in size 16.

This is my query so far, but when I try and apply a filter to the aggs. To say get me all colours which are size 10, the only results I get back is size 10.

```auto
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "pageRanks",
            "query": {
              "term": {
                "pageRanks.pageId": {
                  "value": 934,
                  "_name": "query-pageId"
                }
              }
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "resellers": {
      "nested": {
        "path": "facets"
      },
      "aggs": {
        "Color": {
          "aggs": {
            "Color": {
              "terms": {
                "field": "facets.keyword",
                "size": 10
              }
            }
          },
          "filter": {
            "bool": {
              "filter": [
                {
                  "terms": {
                    "facets.keywords": [
                      "Black"
                    ]
                  }
                },
                {
                  "terms": {
                    "facets.keywords": [
                      "10"
                    ]
                  }
                }
              ]
            }
          }
        }
      }
    }
  }
}

```

Not sure if we going down the wrong route at all, a fresh pair of eyes would be great.

Thanks

---

<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:** [February 22, 2022, 1:32pm UTC](https://discuss.elastic.co/t/faceted-search-query/295351/2 "2022-02-22T13:32:27Z")

</div>

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