我有Customer( parent )和PromotionCustomer(Child)的父子关系。
客户资料:

{
    "id": "b7818d4d-566e-24f5-89d2-3995bb97fd5e",
    **"externalId": "9200191",**
    "name": "LOBLAW NFLD",
    "fullName": "LOBLAW NFLD",
    "businessSystem": "EBJ/001"
}
PromotionCustomer数据:
{
        "id" : "31f2e065-a046-9c3a-808b-83545ddb07d1",
        "externalId" : "T-000195542",
        "businessSystem" : "EBJ/001",
        "promotionDescription" : "PM-RT-LOBLAW NFLD-BB",
        "promotionType" : "Bill Back",
        "promotionStatus" : "Approved",
        **"promotionCustomer" : "9200191",**
        "validFrom" : "02/28/2019",
        "validTo" : "03/20/2019",
        "promotionDateTypeCode" : "1"
      }
这个映射的细节(schema)
{
  "promotionsearch" : {
    "mappings" : {
      "properties" : {
        "_class" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "customer" : {
          "type" : "nested",
          "include_in_parent" : true,
          "properties" : {
            "businessSystem" : {
              "type" : "keyword"
            },
            "externalId" : {
              "type" : "keyword"
            },
            "fullName" : {
              "type" : "text"
            },
            "id" : {
              "type" : "text"
            },
            "name" : {
              "type" : "text"
            }
          }
        },
        "id" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "promotioncustomer" : {
          "type" : "nested",
          "include_in_parent" : true,
          "properties" : {
            "businessSystem" : {
              "type" : "keyword"
            },
            "externalId" : {
              "type" : "keyword"
            },
            "id" : {
              "type" : "text"
            },
            "promotionCustomer" : {
              "type" : "text"
            },
            "promotionDateTypeCode" : {
              "type" : "keyword"
            },
            "promotionDescription" : {
              "type" : "text"
            },
            "promotionProducts" : {
              "type" : "text"
            },
            "promotionStatus" : {
              "type" : "keyword"
            },
            "promotionType" : {
              "type" : "keyword"
            },
            "validFrom" : {
              "type" : "date",
              "format" : "MM/dd/yyyy"
            },
            "validTo" : {
              "type" : "date",
              "format" : "MM/dd/yyyy"
            }
          }
        },
        "promotionjoin" : {
          "type" : "join",
          "eager_global_ordinals" : true,
          "relations" : {
            "customer" : "promotioncustomer"
          }
        },

        "routing" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}
我需要获取基于externalId的promotionCustomer数据,该数据是与日期范围一起映射到promotionCustomer的customerId。
我写了一个查询,如下所示。
GET /promotionsearch/_search
{
  "query": {
    "has_parent": {
      "parent_type": "customer",
      "inner_hits": {},
      "query": {
        "has_child": {
          "type": "promotioncustomer",
          "query": {
            "bool": {
              "must": [
                {
                  "match": {
                    "customer.externalId": "9200191"
                  }
                },
                {
                  "range": {
                    "promotioncustomer.validFrom": {
                      "gte": "02/28/2019"
                    }
                  }
                },
                {
                  "range": {
                    "promotioncustomer.validTo": {
                      "lte": "03/20/2019"
                    }
                  }
                }
              ]
            }
          }
        }
      }
    }
  }
}
但这并没有产生结果。我也知道原因。在has_child子句中,我使用了父字段,即“customer.externalId” 。有没有一种方法可以在parent_type查询中包含/添加此条件,然后根据结果在has_child中应用范围条件

最佳答案

我能够找到解决方案。希望它对其他人也有帮助。无需对ES进行多次查询调用即可获得所需的结果。

GET /promotionsearch/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "promotioncustomer.promotionDescription": "PM-RT"
          }
        },
        {
          "range": {
            "promotioncustomer.validFrom": {
              "gte": "02/28/2019"
            }
          }
        },
        {
          "range": {
            "promotioncustomer.validTo": {
              "lte": "03/28/2019"
            }
          }
        },
        {
          "has_parent": {
            "parent_type": "customer",
            "query": {
              "match": {
                "customer.fullName": "sdjhfb"
              }
            }
          }
        }
      ]
    }
  }
}

10-06 12:56