Querying a multiple level nested object

Hi,
I tried to recreate your mapping (not 100% exactly probably, but more or less) and your query worked for me, just returning one user with that id. Here's what I did, you can compare that to your own mapping to spot possible differences:

PUT /nested_test
{
  "mappings": {
    "t": {
      "properties": {
        "categories": {
          "type": "nested",
          "properties": {
            "name": {
              "type": "string"
            },
            "list": {
              "type": "nested",
              "properties": {
                "url_site": {
                  "type": "string"
                },
                "persons": {
                  "type": "nested",
                  "properties": {
                    "total_customers": {
                      "type": "integer"
                    },
                    "total_subscribers": {
                      "type": "integer"
                    },
                    "details": {
                      "type": "nested",
                      "properties": {
                        "person_id": {
                          "type": "string"
                        },
                        "person_date_registration": {
                          "type": "date"
                        },
                        "person_date_subscription": {
                          "type": "date"
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

PUT /nested_test/t/1
{
  "categories" : {
    "name" : "cat1",
    "list" : {
      "url_site" : "www.bla.org",
      "persons" : {
        "total_customers" : 10,
        "total_subscribers" : 10,
        "details" : {
          "person_id" : 1
        }
      }
    }
  }
}

PUT /nested_test/t/2
{
  "categories" : {
    "name" : "cat2",
    "list" : {
      "url_site" : "www.bleep.org",
      "persons" : {
        "total_customers" : 10,
        "total_subscribers" : 10,
        "details" : {
          "person_id" : 2
        }
      }
    }
  }
}

PUT /nested_test/t/3
{
  "categories" : {
    "name" : "cat3",
    "list" : {
      "url_site" : "www.blubb.org",
      "persons" : {
        "total_customers" : 10,
        "total_subscribers" : 10,
        "details" : {
          "person_id" : 3
        }
      }
    }
  }
}

POST /nested_test/t/_search
{
  "query": {
    "nested": {
      "path": "categories",
      "query": {
        "nested": {
          "path": "categories.list",
          "query": {
            "nested": {
              "path": "categories.list.persons.details",
              "query": {
                "bool": {
                  "must": {
                    "match": {
                      "categories.list.persons.details.person_id": 1
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

returns ==>

"hits": {
    "total": 1,
    "max_score": 1.9162908,
    "hits": [
      {
        "_index": "nested_test",
        "_type": "t",
        "_id": "1",
        "_score": 1.9162908,
        "_source": {
          "categories": {
            "name": "cat1",
            "list": {
              "url_site": "www.bla.org",
              "persons": {
                "total_customers": 10,
                "total_subscribers": 10,
                "details": {
                  "person_id": 1
                }
              }
            }
          }
        }
      }
    ]
  }

btw. if you are just interested in the document without scoring, you can use the filter section in the matchquery instead of must.

1 Like