must_not不适用于嵌套弹性查询

时间:2019-11-29 06:17:58

标签: elasticsearch

我有一个问题。 假设有2个交易使用相同的客户ID'11'。在一次交易中,客户购买了'CLEANSING'产品,在第二次交易中,客户购买了'SKIN CARE'产品。现在我想过滤掉购买了'CLEANSING'产品的客户'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是嵌套类型

  • 交易由同一位客户进行

  • 我试图吸引那些购买了'CLEANSING'但没有购买'SKIN CARE'的客户。我应该没有结果。

我的查询-

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

更新-仍然找不到答案

1 个答案:

答案 0 :(得分: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 /test_trans/_search
{
  "query": {
    "nested": {
      "path": "lineItems",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "lineItems.itemDetails.retailDepartmentName": "CLEANSING"
              }
            }
          ],
          "must_not": [
            {
              "match": {
                "lineItems.itemDetails.retailDepartmentName": "SKIN CARE"
              }
            }
          ]
        }
      },
      "score_mode": "avg"
    }
  },
  "aggs": {
    "nested_path": {
      "nested": {
        "path": "lineItems"
      },
      "aggs": {
        "sum1": {
          "sum": {
            "field": "lineItems.quantity"
          }
        }
      }
    }
  }
}
相关问题