必须对嵌套弹性查询不起作用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了必须对嵌套弹性查询不起作用相关的知识,希望对你有一定的参考价值。

我有一个问题。假设有2个交易使用相同的客户ID'11'。在一次交易中,客户购买了'CLEANSING'产品,在第二次交易中,客户购买了'SKIN CARE'产品。 'SKIN CARE'。但是当我尝试通过客户ID'11'进行汇总时,我得到了客户,因为在第一笔交易中他没有购买产品'SKIN CARE'。如何灵活地寻找客户的整个交易,而不是一笔交易。请帮助我。

这些是交易-

  {
              "transactionId" : "1211",
              "CDID" : "11",
              "transactionDate" : "2019-06-24T09:35:30.2117315Z",
              "lineItems" : [
                {
                  "description" : "BUBBLE BUBBLE MILD FOAMING CLEANSER",
                  "markdownFlag" : "N",
                  "quantity" : 1,
                  "rate" : 14,
                  "value" : 14,
                  "discount" : 0,
                  "amount" : 13.33,
                  "grossAmount" : 14,
                  "itemDetails" : {
                    "itemName" : "BUBBLE BUBBLE MILD FOAMING CLEANSER",
                    "retailDepartmentName" : "CLEANSING",
                  }
                }
              ]
    }

    {
              "transactionId" : "1232",
              "CDID" : "11",
              "transactionDate" : "2019-06-24T09:35:30.2117315Z",
              "lineItems" : [
                {
                  "description" : "BUBBLE BUBBLE MILD FOAMING CLEANSER",
                  "markdownFlag" : "N",
                  "quantity" : 1,
                  "rate" : 14,
                  "value" : 14,
                  "discount" : 0,
                  "amount" : 13.33,
                  "grossAmount" : 14,
                  "itemDetails" : {
                    "itemName" : "BUBBLE BUBBLE MILD FOAMING CLEANSER",
                    "retailDepartmentName" : "SKIN CARE",
                  }
                }
              ]
    }
  • lineItems为嵌套类型

  • 交易是由同一客户进行的

  • [我正试图让购买“清洁”但未购买“护肤”的客户。我应该没有结果。

我的查询-

{
  "aggs": {
    "CDID": {
      "terms": {
        "field": "CDID.keyword",
        "size": 10
      },
      "aggs": {
        "lineItems1": {
          "filter": {
            "nested": {
              "path": "lineItems",
              "query": {
                "bool": {
                  "must": [
                    {
                      "bool": {
                        "must_not": [
                          {
                            "match": {
                              "lineItems.itemDetails.retailDepartmentName.keyword": "SKIN CARE"
                            }
                          }
                        ],
                        "must": [
                          {
                            "match": {
                              "lineItems.itemDetails.retailDepartmentName.keyword": "CLEANSING"
                            }
                          }
                        ]
                      }
                    }
                  ]
                }
              }
            }
          },
          "aggs": {
            "nested_path": {
              "nested": {
                "path": "lineItems"
              },
              "aggs": {
                "sum1": {
                  "sum": {
                    "field": "lineItems.quantity"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

结果-

  "aggregations" : {
    "CDID" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "11",
          "doc_count" : 2,
          "lineItems1" : {
            "doc_count" : 1,
            "nested_path" : {
              "doc_count" : 1,
              "sum1" : {
                "value" : 1.0
              }
            }
          }
        }
      ]
    }
  }
答案

在查询下面,您可以实现结果。

映射查询:

PUT /<index_name>
{
    "mappings" : {
        "properties" : {
            "transactionId": {
              "type": "text"
            },
            "CDID": {
              "type": "text"
            },
             "transactionDate": {
              "type": "text"
            },
            "lineItems" : {
                "type" : "nested"
            }
        }
    }
}

样本数据映射:

POST /<index_name>/_doc
{
  "transactionId": "1211",
  "CDID": "11",
  "transactionDate": "2019-06-24T09:35:30.2117315Z",
  "lineItems": [
    {
      "description": "BUBBLE BUBBLE MILD FOAMING CLEANSER",
      "markdownFlag": "N",
      "quantity": 1,
      "rate": 14,
      "value": 14,
      "discount": 0,
      "amount": 13.33,
      "grossAmount": 14,
      "itemDetails": {
        "itemName": "BUBBLE BUBBLE MILD FOAMING CLEANSER",
        "retailDepartmentName": "CLEANSING"
      }
    }
  ]
}

搜索查询:

GET /<index_name>/_search
{
  "query": {
    "nested": {
      "path": "lineItems",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "lineItems.itemDetails.retailDepartmentName": "SKIN CARE"
              }
            }
          ]
        }
      },
      "score_mode": "avg"
    }
  },
  "aggs": {
    "nested_path": {
      "nested": {
        "path": "lineItems"
      },
      "aggs": {
        "sum1": {
          "sum": {
            "field": "lineItems.quantity"
          }
        }
      }
    }
  }
}

以上是关于必须对嵌套弹性查询不起作用的主要内容,如果未能解决你的问题,请参考以下文章

共享元素转换在父片段和子片段之间不起作用(嵌套片段)

存在嵌套片段时 setRetainInstance(true) 将不起作用

为什么没有嵌套CASE语句不起作用? [重复]

BigQuery - 相关子查询取消嵌套数组不起作用

将嵌套查询从 GraphQL 转换为 Graphene 不起作用

自定义功能用户的角色不起作用,嵌套查询,prisma graphql