是否可以使用要过滤的多个字段来过滤重要术语聚合的桶列表结果?我正在尝试根据中等https://towardsdatascience.com/how-to-build-a-recommendation-engine-quick-and-simple-aec8c71a823e上的这篇文章使用 ES 创建推荐功能。
我将搜索数据存储为对象数组而不是字符串数组,因为我需要过滤其他字段以获得正确的存储桶列表结果。这是索引映射:
{
"mapping": {
"properties": {
"user": {
"type": "keyword",
"ignore_above": 256
},
"comic_subscribes": {
"properties": {
"genres": {
"type": "keyword",
"ignore_above": 256
},
"id": {
"type": "keyword",
"ignore_above": 256
},
"type": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
我有2个条件要过滤:
- Comic_subscribes.type 只能是“serial”
- Comic_subscribes.genre 不得在“hentai”或“echii”中
我已经尝试了两种方法来应用条件。首先,我尝试使用这样的 bool 查询对其进行过滤:
{
"size": 0,
"query": {
"bool": {
"should": [
{
"term": {
"comic_subscribes.id": "1"
}
}
],
"minimum_should_match": 1,
"filter": {
"term": {
"comic_subscribes.type": "serial"
}
},
"must_not": [
{
"bool": {
"should": [
{
"term": {
"comic_subscribes.genres": "hentai"
}
},
{
"term": {
"comic_subscribes.genres": "echii"
}
}
],
"minimum_should_match": 1
}
}
]
}
},
"aggs": {
"recommendations": {
"significant_terms": {
"field": "comic_subscribes.id",
"exclude": ["1"],
"min_doc_count": 1,
"size": 10
}
}
}
}
和过滤聚合方法:
{
"size": 0,
"query": {
"bool": {
"should": [
{
"term": {
"comic_subscribes.id": "1"
}
}
],
"minimum_should_match": 1
}
},
"aggs": {
"filtered": {
"filter": {
"bool": {
"filter": {
"term": {
"comic_subscribes.type": "serial"
}
},
"must_not": [
{
"bool": {
"should": [
{
"term": {
"comic_subscribes.genres": "hentai"
}
},
{
"term": {
"comic_subscribes.genres": "echii"
}
}
],
"minimum_should_match": 1
}
}
]
}
},
"aggs": {
"recommendations": {
"significant_terms": {
"field": "comic_subscribes.id",
"exclude": ["1"],
"min_doc_count": 1,
"size": 10
}
}
}
}
}
}
但是,这两种方法都给了我未经过滤的漫画清单。有没有其他方法可以达到这些要求的条件?我是否应该再创建一个存储预过滤漫画列表的字段以用作源字段重要术语?非常感谢。