在对嵌套对象的弹性搜索复杂布尔查询中出现错误结果



我有一个收件人索引,其中包含一个嵌套的活动对象,该对象存储事件类型和活动ID。

下面是一个示例:

{
email: 'm1@example.com',
userId: 'user-id'
listId: 'list-id'
campaignActivity: [
{ event: 'received', campaignId: 'c1', timestamp: 1 },
{ event: 'received', campaignId: 'c3', timestamp: 3 }
]
}

这是我当前的映射:

{
"recipients_index_test": {
"mappings": {
"recipients": {
"properties": {
"campaignActivity": {
"type": "nested",
"properties": {
"campaignId": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"event": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"timestamp": {
"type": "long"
}
}
},
"email": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"id": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"listId": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"userId": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}

下面是示例数据:

{
email: 'nm1@example.com',
userId: 'user-id',
listId: 'list-id',
campaignActivity: [
{ event: 'received', campaignId: 'c1', timestamp: 1 },
{ event: 'received', campaignId: 'c2', timestamp: 2 },
{ event: 'received', campaignId: 'c3', timestamp: 3 },
{ event: 'received', campaignId: 'c4', timestamp: 4 }
]
},
{
email: 'nm2@example.com',
userId: 'user-id',
listId: 'list-id',
campaignActivity: [
{ event: 'received', campaignId: 'c1', timestamp: 1 }
]
},
{
email: 'm1@example.com',
userId: 'user-id',
listId: 'list-id',
campaignActivity: []
},
{
email: 'm2@example.com',
userId: 'user-id',
listId: 'list-id',
campaignActivity: [
{ event: 'received', campaignId: 'c1', timestamp: 1 },
{ event: 'received', campaignId: 'c3', timestamp: 3 }
]
},
{
email: 'm3@example.com',
userId: 'user-id',
listId: 'list-id',
campaignActivity: [
{ event: 'received', campaignId: 'c2', timestamp: 2 },
{ event: 'received', campaignId: 'c3', timestamp: 3 }
]
}

我想吸引没有收到某些 c2、c3 或 c4 广告系列的收件人(通过排除收到所有广告系列 c2、c3 和 c4 的收件人(这是我一直在尝试的,但没有运气:

{
"query": {
"bool": {
"must_not": [
{
"nested": {
"path": "campaignActivity",
"query": {
"bool": {
"filter": [
{
"term": {
"campaignActivity.campaignId.keyword": "c4"
}
},
{
"term": {
"campaignActivity.event.keyword": "received"
}
}
]
}
}
}
},
{
"nested": {
"path": "campaignActivity",
"query": {
"bool": {
"filter": [
{
"term": {
"campaignActivity.campaignId.keyword": "c3"
}
},
{
"term": {
"campaignActivity.event.keyword": "received"
}
}
]
}
}
}
},
{
"nested": {
"path": "campaignActivity",
"query": {
"bool": {
"filter": [
{
"term": {
"campaignActivity.campaignId.keyword": "c2"
}
},
{
"term": {
"campaignActivity.event.keyword": "received"
}
}
]
}
}
}
}
]
}
},
"from": 0,
"size": 10
}

上面的查询返回 nm2@example.com 和 m1@example.com 但我正在寻找具有 m1@example.com、m2@example.com 和 m3@example.com 的结果

我做错了什么?

更新 1:

这是我迄今为止得到的最接近的:

{
"query": {
"bool": {
"filter": [
{
"nested": {
"path": "campaignActivity",
"query": {
"bool": {
"filter": [
{
"term": {
"campaignActivity.event.keyword": "received"
}
},
{
"terms": {
"campaignActivity.campaignId.keyword": [
"c4",
"c3",
"c2"
]
}
}
]
}
}
}
}
],
"should": [
{
"nested": {
"path": "campaignActivity",
"query": {
"bool": {
"must_not": [
{
"term": {
"campaignActivity.campaignId.keyword": "c4"
}
},
{
"term": {
"campaignActivity.event.keyword": "received"
}
}
]
}
}
}
},
{
"nested": {
"path": "campaignActivity",
"query": {
"bool": {
"must_not": [
{
"term": {
"campaignActivity.campaignId.keyword": "c3"
}
},
{
"term": {
"campaignActivity.event.keyword": "received"
}
}
]
}
}
}
},
{
"nested": {
"path": "campaignActivity",
"query": {
"bool": {
"must_not": [
{
"term": {
"campaignActivity.campaignId.keyword": "c2"
}
},
{
"term": {
"campaignActivity.event.keyword": "received"
}
}
]
}
}
}
}
]
}
},
"from": 0,
"size": 10
}

但它仍然返回不应该成为结果一部分的 nm1@example.com 因为它收到了所有活动(c2,c3,c4(并且缺少 m1@example.com。

我设法让它处理这个查询:

{
"query": {
"bool": {
"must": [
{
"bool": {
"should": [
{
"bool": {
"must_not": [
{
"nested": {
"path": "campaignActivity",
"query": {
"bool": {
"filter": {
"exists": {
"field": "campaignActivity"
}
}
}
}
}
}
]
}
},
{
//match c2
},
{
//match c3
},
{
//match c4
}
]
}
},
{
"bool": {
"must_not": [
{
"bool": {
"must": [
{
//match c2
},
{
//match c3
},
{
//match c4
}
]
}
}
]
}
}
]
}
}
}

我希望将来对某人有所帮助。

最新更新