# How to filter nested objects on a should query?

**URL:** <https://discuss.elastic.co/t/how-to-filter-nested-objects-on-a-should-query/119612>\
**Category:** Elasticsearch\
**Created:** [February 13, 2018, 10:21am UTC](https://discuss.elastic.co/t/how-to-filter-nested-objects-on-a-should-query/119612 "2018-02-13T10:21:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![batmaci](https://avatars.discourse-cdn.com/v4/letter/b/f08c70/32.png) [@batmaci](https://discuss.elastic.co/u/batmaci)\
**Post date:** [February 13, 2018, 10:21am UTC](https://discuss.elastic.co/t/how-to-filter-nested-objects-on-a-should-query/119612/1 "2018-02-13T10:21:20Z")

</div>

I have my mappings as below and I am doing a bool should query on name and other properties as shown below but what I need is that I want to filter CustomerPrices by CustomerId on response.  
Each products have same CustomerIds so for eaxample;

```
product1 -CustomerPrice( CustomerId :1234 -Price:4) 
           CustomerPrice( CustomerId :567-Price:5)
            .
            .
 Product2 - CustomerPrice(CustomerId :1234 -Price:8)
            CustomerPrice(CustomerId :567-Price:10)
           .
           .

```

So according to that when I query Product1, response should have only customerPrice for customerId:1234

```
{
  "Product": {
    "properties": {
      "CustomerPrices": {
        "type": "nested",
        "properties": {
          "Price": {
            "store": true,
            "type": "float"
          },
          "CustomerId": {
            "type": "integer"
          }
        }
      },
      "Name": {
        "index": "not_analyzed",
        "store": true,
        "type": "string"
      }
    }
  }
}

```

I tried following query but this is not filtering nested objects. I guess it filters product objects as it makes sense because all products have customerId:1234

```
   "query":{
        "bool":{
          "should":[
            {
              "multi_match":{
                "type":"best_fields",
                "query":"product 1",
                "fields":[             
                  "Name^7"]
              }
            },
            {
              "multi_match":{
                "type":"best_fields",
                "query":"product 1",
                "operator":"and",
                "fields":[
                  "Code^10",           
                  "ShortDescription^6"]
              }
            },      
            {
              "nested":{
                "query":{
                  "term":{
                    "CustomerPrices.CustomerId":{
                      "value":1234
                    }
                  }
                },
                "path":"CustomerPrices"
              }
            }]
        }
      },
```

---

<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:** [March 13, 2018, 10:21am UTC](https://discuss.elastic.co/t/how-to-filter-nested-objects-on-a-should-query/119612/2 "2018-03-13T10:21:33Z")

</div>

Closed on March 13, 2018, 10:21am UTC

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