Q: How to search in nested object's array matching all items I have an index with nested objects houses.
My index contains these documents:
{
    "_id": "hello",
    "name": "pippos",
    "houses": [
      {
        "address": "garden square",
        "id1": 1,
        "id2": 5
      },
      {
        "address": "top square",
        "id1": 1,
        "id2": 5
      }
    ]
  },
  {
    "_id": "hellone",
    "name": "pippoone",
    "houses": [
      {
        "address": "central square",
        "id1": 1,
        "id2": 9
      },
      {
        "address": "minimale square",
        "id1": 1,
        "id2": 5
      }
    ]
  }

Using this query I receive both documents:
GET /pippis/_search
{
  "query": {
    "nested": {
      "path": "houses",
      "query": {
        "bool": {
          "must": [
            { "match": { "houses.id1": 1 }},
            { "match": { "houses.id2": 5 }} 
          ]
        }
      }
    }
  }
}

I want only documents having all houses with id1=1 and id2=5 

A: Mapping:
PUT /user
{
  "mappings": {
    "properties": {
      "name": {
        "type": "text"
      },
      "houses": {
        "type": "nested",
        "properties": {
          "address": {
            "type": "text"
          },
          "id1": {
            "type": "integer"
          },
          "id2": {
            "type": "integer"
          }
        }
      }
    }
  }
}

Data:
"hits" : [
      {
        "_index" : "user",
        "_type" : "_doc",
        "_id" : "5kQ6-2wBWSK8eKKSSozQ",
        "_score" : 1.0,
        "_source" : {
          "name" : "pippos",
          "houses" : [
            {
              "address" : "garden square",
              "id1" : 1,
              "id2" : 5
            },
            {
              "address" : "top square",
              "id1" : 1,
              "id2" : 5
            }
          ]
        }
      },
      {
        "_index" : "user",
        "_type" : "_doc",
        "_id" : "50Q9-2wBWSK8eKKStIzf",
        "_score" : 1.0,
        "_source" : {
          "name" : "pippoone",
          "houses" : [
            {
              "address" : "central square",
              "id1" : 1,
              "id2" : 9
            },
            {
              "address" : "minimale square",
              "id1" : 1,
              "id2" : 5
            }
          ]
        }
      },
      {
        "_index" : "user",
        "_type" : "_doc",
        "_id" : "6ERM-2wBWSK8eKKS3IzD",
        "_score" : 1.0,
        "_source" : {
          "name" : "pippoone1",
          "houses" : [
            {
              "address" : "central square",
              "id1" : 2,
              "id2" : 9
            },
            {
              "address" : "minimale square",
              "id1" : 2,
              "id2" : 5
            }
          ]
        }
      }
    ]
  }

Query:
GET /user/_search
{
  "query": {
    "bool": {
      "must_not": [             -----> Not of documents returned in nested query
        {
          "nested": {
            "path": "houses",
            "query": {
              "bool": {
                "should": [      -----> get documents where id1 is not 1 or id2 is not 5
                  {
                    "bool": {
                      "must_not": [   
                        {
                          "match": {
                            "houses.id1": 1
                          }
                        }
                      ]
                    }
                  },
                  {
                    "bool": {
                      "must_not": [
                        {
                          "match": {
                            "houses.id2": 5
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

Result:
 [
      {
        "_index" : "user",
        "_type" : "_doc",
        "_id" : "5kQ6-2wBWSK8eKKSSozQ",
        "_score" : 0.0,
        "_source" : {
          "name" : "pippos",
          "houses" : [
            {
              "address" : "garden square",
              "id1" : 1,
              "id2" : 5
            },
            {
              "address" : "top square",
              "id1" : 1,
              "id2" : 5
            }
          ]
        }
      }
    ]

