ElasticSearch查询-嵌套数组的所有对象都应具有特定值

e0bqpujr  于 2022-11-02  发布在  ElasticSearch
关注(0)|答案(1)|浏览(167)

我想写一个Elasticsearch查询,我可以查看嵌套数组,并检查数组中所有元素的关键字,如果所有元素都有该值,则应选择根文档。
这是一个示例数据:

[
{
                "_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": "tempo@tempo.com",
                                        "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": "mag.m@rr.com.au",
                                "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": "mag.m@rr.com.au"
                            },
                            "roleAdjective": "GROUP"
                        },
                        {
                            "id": 3453800,
                            "role": "AGENT",
                            "user": {
                                "id": 20054,
                                "dob": null,
                                "name": "Paul",
                                "email": "p.b@mr.com",
                                "phone": "+61403084232",
                                "surname": "Botti",
                                "salutation": null,
                                "lastInAppActivity": null
                            },
                            "company": null,
                            "roleAdjective": "MANAGING"
                        },
                        {
                            "id": 3453801,
                            "role": "AGENT",
                            "user": {
                                "id": 20054,
                                "dob": null,
                                "name": "Paul",
                                "email": "p.b@mr.com",
                                "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
                ]
            }
]

在这个特定的例子中,租约是nested document,我想选择所有租约都处于取消状态或租约为空的文档,即;要么租约内的所有对象都应具有CANCELLED状态,要么租约键应为null。已经浏览了this question,但没有完全理解,因为这是2015年的旧答案,其中使用的方法已过时。

zzoitvuj

zzoitvuj1#

请尝试以下查询:

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"
                                }
                              }
                            ]
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

相关问题