How to filter array of nested objects that must have all terms apllied


(Alvin Tanjaya) #1

Hi guys, I really have difficulties in understanding how the query DSL works, I have a object like this :

{
    "name": "Asus Zenphone",
    "brand": "Asus",
    "items": [
      {
        "price": 2000000,
        "options": [
          {
            "name": "Color",
            "value": "Red"
          },
          {
            "name": "Memory Capacity",
            "value": "16GB"
          }
        ]
      },
      {
        "price": 2000000,
        "options": [
          {
            "name": "Color",
            "value": "Black"
          },
          {
            "name": "Memory Capacity",
            "value": "16GB"
          }
        ]
      },
      {
        "price": 2000000,
        "options": [
          {
            "name": "Color",
            "value": "White"
          },
          {
            "name": "Memory Capacity",
            "value": "16GB"
          }
        ]
      },
      {
        "price": 4000000,
        "options": [
          {
            "name": "Color",
            "value": "Black"
          },
          {
            "name": "Memory Capacity",
            "value": "32GB"
          }
        ]
      },
      {
        "price": 4000000,
        "options": [
          {
            "name": "Color",
            "value": "White"
          },
          {
            "name": "Memory Capacity",
            "value": "32GB"
          }
        ]
      }
    ]
  }

How could I get a result empty if I find product with Color Red and Memory Capacity 32GB but get result 1 product if i find Color Red and Memory Capacity 16GB?

I've tried using this query but it give me empty result for both :

{
  "query": {
    "filtered": {
      "filter": {
        "and": [
          {
            "terms": {
              "brand": [
                "Asus",
                "Apple"
              ]
            }
          },
          {
            "nested": {
              "path": "items.options",
              "filter": {
                "bool": {
                  "must": [
                    {
                      "bool": {
                        "must": [
                          {
                            "term": {
                              "items.options.name": "Memory Capacity"
                            }
                          },
                          {
                            "terms": {
                              "items.options.value": [
                                "32GB"
                              ]
                            }
                          }
                        ]
                      }
                    },
                    {
                      "bool": {
                        "must": [
                          {
                            "term": {
                              "items.options.name": "Color"
                            }
                          },
                          {
                            "terms": {
                              "items.options.value": [
                                "Red"
                              ]
                            }
                          }
                        ]
                      }
                    }
                  ]
                }
              }
            }
          }
        ]
      }
    }
  }
}

(system) #2