Search code examples
elasticsearchlucenedsl

Elastic search query - all objects of nested array should have specific value


I want to write Elasticsearch query where I am able to look into a nested array, and check a key for all those elements inside the array, if all of them have that value then the root document should be selected.

this is a sample data :

[
{
                "_index": "allproperties",
                "_type": "_doc",
                "_id": "5241050",
                "_score": 0.0,
                "_source": {
                    "id": 5241050,
                    "type": "HOUSE",
                    "state": "NSW",
                    "agency": {
                        "id": 31,
                        "code": "AU_LNT",
                        "name": "Luthor Properties",
                        "config": "{}"
                    },
                    "branch": {
                        "id": 89,
                        "name": "Luthor Sales Unit Trust",
                        "tradingName": "Luthor Properties",
                        "internalCode": "AU_LNT_G"
                    },
                    "images": null,
                    "leases": [
                        {
                            "id": 26439,
                            "bond": 2000,
                            "leased": true,
                            "source": "PORTAL",
                            "status": "SIGNED",
                            "endDate": "2022-11-06T00:00:00",
                            "tenants": [
                                {
                                    "id": 11106,
                                    "role": "TENANT",
                                    "user": {
                                        "id": 38817,
                                        "dob": "1900-01-01",
                                        "name": "Liam",
                                        "email": "[email protected]",
                                        "phone": "+61400000000",
                                        "surname": "Tempo",
                                        "salutation": "Mr.",
                                        "invitations": null,
                                        "lastInAppActivity": null
                                    },
                                    "address": "45675 Bruce Hwy, Coolbie QLD 4850, Australia",
                                    "extendedData": "{\"rentSplitAmount\":22.62}",
                                    "roleAdjective": "PRIMARY",
                                    "addressComponents": {
                                        "state": "QLD",
                                        "suburb": "Coolbie",
                                        "country": "Australia",
                                        "postcode": "4850",
                                        "streetName": "Bruce Hwy",
                                        "unitNumber": null,
                                        "poboxNumber": null,
                                        "streetNumber": "45675",
                                        "addressComponentsUnavailable": null
                                    }
                                }
                            ],
                            "signDate": "2022-10-05T04:58:08.887",
                            "startDate": "2022-10-06T00:00:00",
                            "moveInDate": "2022-10-06T00:00:00",
                            "appointments": [
                                {
                                    "id": 3506,
                                    "type": "REMOTE",
                                    "date_time": "2022-10-12T04:56:00+00:00",
                                    "createdBy": "Lex Luthor",
                                    "createdDate": "2022-10-05T04:56:52.936",
                                    "lastModifiedBy": "Lex Luthor",
                                    "lastModifiedDate": "2022-10-05T04:56:53.51"
                                }
                            ],
                            "createdDate": "2022-10-05T04:55:42.247",
                            "rentalAmount": 500,
                            "dateAvailable": null,
                            "extendedData": {
                                "last_rent_adjustment_date": 1665014400000
                            },
                            "leaseDuration": 32,
                            "rentalFrequency": "2",
                            "signedManually": false
                        }
                    ],
                    "refId": "b66326eb-a6b2-42b6-b058-b46847e13399",
                    "source": "PT",
                    "status": null,
                    "suburb": "Hurstville",
                    "address": "456 Forest Road Hurstville NSW AUSTRALIA 2220",
                    "country": "Australia",
                    "bedrooms": 3,
                    "category": null,
                    "pmsCode": "84c34d15-a0ab-4791-b9e3-1bdac215b99c",
                    "postcode": "2220",
                    "agencyId": 31,
                    "bathrooms": 3,
                    "branchId": 89,
                    "carspaces": 3,
                    "createdBy": "system",
                    "streetname": "Forest Road",
                    "unitnumber": null,
                    "description": null,
                    "createdDate": "2022-10-05T04:51:12.619",
                    "entitlements": [
                        {
                            "id": 3453799,
                            "role": "LANDLORD",
                            "user": {
                                "id": 22855,
                                "dob": null,
                                "name": "please enter ownership name",
                                "email": "[email protected]",
                                "phone": "0400000000",
                                "surname": "",
                                "salutation": null,
                                "lastInAppActivity": null
                            },
                            "company": {
                                "id": 137,
                                "abn": "",
                                "acn": "",
                                "phone": "0400000000",
                                "address": "1234 Park Avenue New York NY USA 10037-1702",
                                "companyName": "please enter ownership name",
                                "displayName": "please enter ownership name",
                                "email": "[email protected]"
                            },
                            "roleAdjective": "GROUP"
                        },
                        {
                            "id": 3453800,
                            "role": "AGENT",
                            "user": {
                                "id": 20054,
                                "dob": null,
                                "name": "Paul",
                                "email": "[email protected]",
                                "phone": "+61403084232",
                                "surname": "Botti",
                                "salutation": null,
                                "lastInAppActivity": null
                            },
                            "company": null,
                            "roleAdjective": "MANAGING"
                        },
                        {
                            "id": 3453801,
                            "role": "AGENT",
                            "user": {
                                "id": 20054,
                                "dob": null,
                                "name": "Paul",
                                "email": "[email protected]",
                                "phone": "+61403084232",
                                "surname": "Botti",
                                "salutation": null,
                                "lastInAppActivity": null
                            },
                            "company": null,
                            "roleAdjective": "LEASING"
                        }
                    ],
                    "streetnumber": "456",
                    "advertised": false,
                    "commission_type": null,
                    "lastModifiedBy": "system",
                    "lastModifiedDate": "2022-10-05T04:58:09.043",
                    "_meta": {
                        "branches": {
                            "id": [
                                89
                            ]
                        },
                        "agencies": {
                            "id": [
                                31
                            ]
                        },
                        "user_property_entitlement": {
                            "id": [
                                3453799,
                                3453800,
                                3453801
                            ]
                        },
                        "users": {
                            "id": [
                                20054,
                                22855,
                                38817
                            ]
                        },
                        "companies": {
                            "id": [
                                137
                            ]
                        },
                        "leases": {
                            "id": [
                                26439
                            ]
                        },
                        "user_lease_entitlement": {
                            "id": [
                                11106
                            ]
                        },
                        "appointments": {
                            "id": [
                                3506
                            ]
                        }
                    }
                },
                "sort": [
                    1664945472619,
                    0.0
                ]
            }
]

In this particular case, leases is a nested document, I want to select the documents where all the leases are in cancelled state or leases is null, i.e; either all of the objects inside leases should have status as CANCELLED or the leases key should be null. Already went through this question, but did'nt quite get it as its an old answer from 2015 and methods used in this got deprecated.


Solution

  • Try this query:

    GET idx_test/_search?filter_path=hits.hits
    {
      "query": {
        "bool": {
          "must_not": [
            {
              "nested": {
                "path": "leases",
                "query": {
                  "bool": {
                    "must_not": [
                      {
                        "bool": {
                          "should": [
                            {
                              "term": {
                                "leases.status.keyword": "CANCELLED"
                              }
                            },
                            {
                              "bool": {
                                "must_not": [
                                  {
                                    "exists": {
                                      "field": "leases"
                                    }
                                  }
                                ]
                              }
                            }
                          ]
                        }
                      }
                    ]
                  }
                }
              }
            }
          ]
        }
      }
    }