我有个问题。假设同一个客户的id '11‘有2个交易,其中一个交易客户购买了’清洁‘产品,第二个交易客户购买了’护肤‘product.Now,我想过滤掉购买产品’清洗‘而不是’护肤‘的客户,但当我试图按客户id’11‘汇总时,我得到了客户,因为在第一个交易中,他没有购买“皮肤护理”产品,如何使客户的整个交易变得有弹性,而没有一个transaction.Please帮助我解决问题。
这些是交易-
{
"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",
}
}
]
}我的问题-
{
"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
}
}
}
}
]
}
}UPDATE-Still没有找到答案
发布于 2019-11-29 07:35:32
在下面的查询中,您可以获得结果。
映射查询:
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"
}
}
}
}
}
}https://stackoverflow.com/questions/59099632
复制相似问题