0

是否可以使用要过滤的多个字段来过滤重要术语聚合的桶列表结果?我正在尝试根据中等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个条件要过滤:

  1. Comic_subscribes.type 只能是“serial”
  2. 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
                    }
                }
            }
        }
    }
}

但是,这两种方法都给了我未经过滤的漫画清单。有没有其他方法可以达到这些要求的条件?我是否应该再创建一个存储预过滤漫画列表的字段以用作源字段重要术语?非常感谢。

4

1 回答 1

1

好的,兄弟们。我认为没有使用不同字段过滤聚合重要术语桶列表结果的选项方法。

基于 elasticsearch 文档的重要术语聚合参数,它指的是术语聚合过滤值除了使用分区表达式过滤和使用精确值过滤值之外没有其他选择(我一直在使用上面的“排除”参数)。

因此,我通过获取要排除的漫画 ID 并将其存储为数组中的 excludeComics 变量来创建其他方式。然后在排除参数中使用 excludeComics var 。和繁荣,你去。过滤的重要术语聚合桶列表结果。

于 2019-10-25T03:11:26.507 回答