根据三个维度继续过滤

在上一节中我们实现了根据流量信息过滤的代码,但是我们的条件有可能是多条件一起传给我们的检索服务的,本节我们继续实现根据推广单元的三个维度条件的过滤。

  • SearchImpl类中添加过滤方法
  1. public class SearchImpl implements ISearch {
  2. @Override
  3. public SearchResponse fetchAds(SearchRequest request) {
  4. ...
  5. // 根据三个维度过滤
  6. if (featureRelation == FeatureRelation.AND) {
  7. filterKeywordFeature(adUnitIdSet, keywordFeature);
  8. filterHobbyFeature(adUnitIdSet, hobbyFeatrue);
  9. filterDistrictFeature(adUnitIdSet, districtFeature);
  10. targetUnitIdSet = adUnitIdSet;
  11. } else {
  12. getOrRelationUnitIds(adUnitIdSet, keywordFeature, hobbyFeatrue, districtFeature);
  13. }
  14. }
  15. return null;
  16. }
  • 定义三个方法实现过滤
  1. /**
  2. * 获取三个维度各自满足时的广告id
  3. */
  4. private Set<Long> getOrRelationUnitIds(Set<Long> adUnitIdsSet,
  5. KeywordFeature keywordFeature,
  6. HobbyFeatrue hobbyFeatrue,
  7. DistrictFeature districtFeature) {
  8. if (CollectionUtils.isEmpty(adUnitIdsSet)) return Collections.EMPTY_SET;
  9. // 我们在处理的时候,需要对副本进行处理,大家可以考虑一下为什么需要这么做?
  10. Set<Long> keywordUnitIdSet = new HashSet<>(adUnitIdsSet);
  11. Set<Long> hobbyUnitIdSet = new HashSet<>(adUnitIdsSet);
  12. Set<Long> districtUnitIdSet = new HashSet<>(adUnitIdsSet);
  13. filterKeywordFeature(keywordUnitIdSet, keywordFeature);
  14. filterHobbyFeature(hobbyUnitIdSet, hobbyFeatrue);
  15. filterDistrictFeature(districtUnitIdSet, districtFeature);
  16. // 返回它们的并集
  17. return new HashSet<>(
  18. CollectionUtils.union(
  19. CollectionUtils.union(keywordUnitIdSet, hobbyUnitIdSet),
  20. districtUnitIdSet
  21. )
  22. );
  23. }
  24. /**
  25. * 根据传递的关键词过滤
  26. */
  27. private void filterKeywordFeature(Collection<Long> adUnitIds, KeywordFeature keywordFeature) {
  28. if (CollectionUtils.isEmpty(adUnitIds)) return;
  29. if (CollectionUtils.isNotEmpty(keywordFeature.getKeywords())) {
  30. // 如果存在需要过滤的关键词,查找索引实例对象进行过滤处理
  31. CollectionUtils.filter(
  32. adUnitIds,
  33. adUnitId -> IndexDataTableUtils.of(UnitKeywordIndexAwareImpl.class)
  34. .match(adUnitId, keywordFeature.getKeywords())
  35. );
  36. }
  37. }
  38. /**
  39. * 根据传递的兴趣信息过滤
  40. */
  41. private void filterHobbyFeature(Collection<Long> adUnitIds, HobbyFeatrue hobbyFeatrue) {
  42. if (CollectionUtils.isEmpty(adUnitIds)) return;
  43. // 如果存在需要过滤的兴趣,查找索引实例对象进行过滤处理
  44. if (CollectionUtils.isNotEmpty(hobbyFeatrue.getHobbys())) {
  45. CollectionUtils.filter(
  46. adUnitIds,
  47. adUnitId -> IndexDataTableUtils.of(UnitHobbyIndexAwareImpl.class)
  48. .match(adUnitId, hobbyFeatrue.getHobbys())
  49. );
  50. }
  51. }
  52. /**
  53. * 根据传递的地域信息过滤
  54. */
  55. private void filterDistrictFeature(Collection<Long> adUnitIds, DistrictFeature districtFeature) {
  56. if (CollectionUtils.isEmpty(adUnitIds)) return;
  57. // 如果存在需要过滤的地域信息,查找索引实例对象进行过滤处理
  58. if (CollectionUtils.isNotEmpty(districtFeature.getProvinceAndCities())) {
  59. CollectionUtils.filter(
  60. adUnitIds,
  61. adUnitId -> {
  62. return IndexDataTableUtils.of(UnitDistrictIndexAwareImpl.class)
  63. .match(adUnitId, districtFeature.getProvinceAndCities());
  64. }
  65. );
  66. }
  67. }
根据推广单元id获取推广创意

我们知道,推广单元和推广创意的关系是多对多,从上文我们查询到了推广单元ids,接下来我们实现根据推广单元id获取推广创意的代码,let's code.

首先,我们需要在com.sxzhongf.ad.index.creative_relation_unit.CreativeRelationUnitIndexAwareImpl 关联索引中查到推广创意的ids

  1. /**
  2. * 通过推广单元id获取推广创意id
  3. */
  4. public List<Long> selectAdCreativeIds(List<AdUnitIndexObject> unitIndexObjects) {
  5. if (CollectionUtils.isEmpty(unitIndexObjects)) return Collections.emptyList();
  6. //获取要返回的广告创意ids
  7. List<Long> result = new ArrayList<>();
  8. for (AdUnitIndexObject unitIndexObject : unitIndexObjects) {
  9. //根据推广单元id获取推广创意
  10. Set<Long> adCreativeIds = unitRelationCreativeMap.get(unitIndexObject.getUnitId());
  11. if (CollectionUtils.isNotEmpty(adCreativeIds)) result.addAll(adCreativeIds);
  12. }
  13. return result;
  14. }

然后得到了推广创意的id list后,我们在创意索引实现类com.sxzhongf.ad.index.creative.CreativeIndexAwareImpl中定义根据ids查询创意的方法。

  1. /**
  2. * 根据ids获取创意list
  3. */
  4. public List<CreativeIndexObject> findAllByIds(Collection<Long> ids) {
  5. if (CollectionUtils.isEmpty(ids)) return Collections.emptyList();
  6. List<CreativeIndexObject> result = new ArrayList<>();
  7. for (Long id : ids) {
  8. CreativeIndexObject object = get(id);
  9. if (null != object)
  10. result.add(object);
  11. }
  12. return result;
  13. }

自此,我们已经得到了想要的推广单元和推广创意,因为推广单元包含了推广计划,所以我们想要的数据已经全部可以获取到了,接下来,我们还得过滤一次当前我们查询到的数据的状态,因为有的数据,我们可能已经进行过逻辑删除了,因此还需要判断获取的数据是否有效。在SearchImpl类中实现。

  1. /**
  2. * 根据状态信息过滤数据
  3. */
  4. private void filterAdUnitAndPlanStatus(List<AdUnitIndexObject> unitIndexObjects, CommonStatus status) {
  5. if (CollectionUtils.isEmpty(unitIndexObjects)) return;
  6. //同时判断推广单元和推广计划的状态
  7. CollectionUtils.filter(
  8. unitIndexObjects,
  9. unitIndexObject -> unitIndexObject.getUnitStatus().equals(status.getStatus()) &&
  10. unitIndexObject.getAdPlanIndexObject().getPlanStatus().equals(status.getStatus())
  11. );
  12. }

SearchImpl中我们实现广告创意的查询.

  1. ...
  2. //获取 推广计划 对象list
  3. List<AdUnitIndexObject> unitIndexObjects = IndexDataTableUtils.of(AdUnitIndexAwareImpl.class).fetch(adUnitIdSet);
  4. //根据状态过滤数据
  5. filterAdUnitAndPlanStatus(unitIndexObjects, CommonStatus.VALID);
  6. //获取 推广创意 id list
  7. List<Long> creativeIds = IndexDataTableUtils.of(CreativeRelationUnitIndexAwareImpl.class)
  8. .selectAdCreativeIds(unitIndexObjects);
  9. //根据 推广创意ids获取推广创意
  10. List<CreativeIndexObject> creativeIndexObjects = IndexDataTableUtils.of(CreativeIndexAwareImpl.class)
  11. ...
根据广告位adslot 实现对创意数据的过滤

因为我们的广告位是有不同的大小,不同的类型,因此,我们在获取到所有符合我们查询维度以及流量类型的条件后,还需要针对不同的广告位来展示不同的广告创意信息。

  1. /**
  2. * 根据广告位类型以及参数获取展示的合适广告信息
  3. *
  4. * @param creativeIndexObjects 所有广告创意
  5. * @param width 广告位width
  6. * @param height 广告位height
  7. */
  8. private void filterCreativeByAdSlot(List<CreativeIndexObject> creativeIndexObjects,
  9. Integer width,
  10. Integer height,
  11. List<Integer> type) {
  12. if (CollectionUtils.isEmpty(creativeIndexObjects)) return;
  13. CollectionUtils.filter(
  14. creativeIndexObjects,
  15. creative -> {
  16. //审核状态必须是通过
  17. return creative.getAuditStatus().equals(CommonStatus.VALID.getStatus())
  18. && creative.getWidth().equals(width)
  19. && creative.getHeight().equals(height)
  20. && type.contains(creative.getType());
  21. }
  22. );
  23. }
  • 组建搜索返回对象

    正常业务场景中,同一个广告位可以展示多个广告信息,也可以只展示一个广告信息,这个需要根据具体的业务场景来做不同的处理,本次为了演示方便,会从返回的创意列表中随机选择一个创意广告信息进行展示,当然大家也可以根据业务类型,设置不同的优先级或者权重值来进行广告选择。
  1. /**
  2. * 从创意列表中随机获取一条创意广告返回出去
  3. *
  4. * @param creativeIndexObjects 创意广告list
  5. */
  6. private List<SearchResponse.Creative> buildCreativeResponse(List<CreativeIndexObject> creativeIndexObjects) {
  7. if (CollectionUtils.isEmpty(creativeIndexObjects)) return Collections.EMPTY_LIST;
  8. //随机获取一个广告创意,也可以实现优先级排序,也可以根据权重值等等,具体根据业务
  9. CreativeIndexObject randomObject = creativeIndexObjects.get(
  10. Math.abs(new Random().nextInt()) % creativeIndexObjects.size()
  11. );
  12. //List<SearchResponse.Creative> result = new ArrayList<>();
  13. //result.add(SearchResponse.convert(randomObject));
  14. return Collections.singletonList(
  15. SearchResponse.convert(randomObject)
  16. );
  17. }

完整的请求过滤实现方法:

  1. @Service
  2. @Slf4j
  3. public class SearchImpl implements ISearch {
  4. @Override
  5. public SearchResponse fetchAds(SearchRequest request) {
  6. //获取请求广告位信息
  7. List<AdSlot> adSlotList = request.getRequestInfo().getAdSlots();
  8. //获取三个Feature信息
  9. KeywordFeature keywordFeature = request.getFeatureInfo().getKeywordFeature();
  10. HobbyFeatrue hobbyFeatrue = request.getFeatureInfo().getHobbyFeatrue();
  11. DistrictFeature districtFeature = request.getFeatureInfo().getDistrictFeature();
  12. //Feature关系
  13. FeatureRelation featureRelation = request.getFeatureInfo().getRelation();
  14. //构造响应对象
  15. SearchResponse response = new SearchResponse();
  16. Map<String, List<SearchResponse.Creative>> adSlotRelationAds = response.getAdSlotRelationAds();
  17. for (AdSlot adSlot : adSlotList) {
  18. Set<Long> targetUnitIdSet;
  19. //根据流量类型从缓存中获取 初始 广告信息
  20. Set<Long> adUnitIdSet = IndexDataTableUtils.of(
  21. AdUnitIndexAwareImpl.class
  22. ).match(adSlot.getPositionType());
  23. // 根据三个维度过滤
  24. if (featureRelation == FeatureRelation.AND) {
  25. filterKeywordFeature(adUnitIdSet, keywordFeature);
  26. filterHobbyFeature(adUnitIdSet, hobbyFeatrue);
  27. filterDistrictFeature(adUnitIdSet, districtFeature);
  28. targetUnitIdSet = adUnitIdSet;
  29. } else {
  30. targetUnitIdSet = getOrRelationUnitIds(adUnitIdSet, keywordFeature, hobbyFeatrue, districtFeature);
  31. }
  32. //获取 推广计划 对象list
  33. List<AdUnitIndexObject> unitIndexObjects = IndexDataTableUtils.of(AdUnitIndexAwareImpl.class)
  34. .fetch(targetUnitIdSet);
  35. //根据状态过滤数据
  36. filterAdUnitAndPlanStatus(unitIndexObjects, CommonStatus.VALID);
  37. //获取 推广创意 id list
  38. List<Long> creativeIds = IndexDataTableUtils.of(CreativeRelationUnitIndexAwareImpl.class)
  39. .selectAdCreativeIds(unitIndexObjects);
  40. //根据 推广创意ids获取推广创意
  41. List<CreativeIndexObject> creativeIndexObjects = IndexDataTableUtils.of(CreativeIndexAwareImpl.class)
  42. .fetch(creativeIds);
  43. //根据 广告位adslot 实现对创意数据的过滤
  44. filterCreativeByAdSlot(creativeIndexObjects, adSlot.getWidth(), adSlot.getHeight(), adSlot.getType());
  45. //一个广告位可以展示多个广告,也可以仅展示一个广告,具体根据业务来定
  46. adSlotRelationAds.put(
  47. adSlot.getAdSlotCode(),
  48. buildCreativeResponse(creativeIndexObjects)
  49. );
  50. }
  51. return response;
  52. }
  53. ...
检索服务对外提供
  • 暴露API接口

    上文中,我们实现了检索服务的核心逻辑,接下来,我们需要对外暴露我们的广告检索服务接口,在SearchController中提供:

    1. @PostMapping("/fetchAd")
    2. public SearchResponse fetchAdCreative(@RequestBody SearchRequest request) {
    3. log.info("ad-serach: fetchAd ->{}", JSON.toJSONString(request));
    4. return search.fetchAds(request);
    5. }
  • 实现API网关配置

    1. zuul:
    2. routes:
    3. sponsor: #在路由中自定义服务路由名称
    4. path: /ad-sponsor/**
    5. serviceId: mscx-ad-sponsor #微服务name
    6. strip-prefix: false
    7. search: #在路由中自定义服务路由名称
    8. path: /ad-search/**
    9. serviceId: mscx-ad-search #微服务name
    10. strip-prefix: false
    11. prefix: /gateway/api
    12. strip-prefix: true #不对 prefix: /gateway/api 设置的路径进行截取,默认转发会截取掉配置的前缀

[Spring cloud 一步步实现广告系统] 18. 查询返回广告创意的更多相关文章

  1. [Spring cloud 一步步实现广告系统] 19. 监控Hystrix Dashboard

    在之前的18次文章中,我们实现了广告系统的广告投放,广告检索业务功能,中间使用到了 服务发现Eureka,服务调用Feign,网关路由Zuul以及错误熔断Hystrix等Spring Cloud组件. ...

  2. [Spring cloud 一步步实现广告系统] 21. 系统错误汇总

    广告系统学习过程中问题答疑 博客园 Eureka集群启动报错 Answer 因为Eureka在集群启动过程中,会连接集群中其他的机器进行数据同步,在这个过程中,如果别的服务还没有启动完成,就会出现Co ...

  3. [Spring cloud 一步步实现广告系统] 2. 配置&Eureka服务

    父项目管理 首先,我们在创建投放系统之前,先看一下我们的工程结构: mscx-ad-sponsor就是我们的广告投放系统.如上结构,我们需要首先创建一个Parent Project mscx-ad 来 ...

  4. [Spring cloud 一步步实现广告系统] 22. 广告系统回顾总结

    到目前为止,我们整个初级广告检索系统就初步开发完成了,我们来整体回顾一下我们的广告系统. 整个广告系统编码结构如下: mscx-ad 父模块 主要是为了方便我们项目的统一管理 mscx-ad-db 这 ...

  5. [Spring cloud 一步步实现广告系统] 7. 中期总结回顾

    在前面的过程中,我们创建了4个project: 服务发现 我们使用Eureka 作为服务发现组件,学习了Eureka Server,Eureka Client的使用. Eureka Server 加依 ...

  6. [Spring cloud 一步步实现广告系统] 1. 业务架构分析

    什么是广告系统? 主要包含: 广告主投放广告的<广告投放系统> 媒体方(广告展示媒介-)检索广告用的<广告检索系统> 广告计费系统(按次,曝光量等等) 报表系统 Etc. 使用 ...

  7. [Spring cloud 一步步实现广告系统] 13. 索引服务编码实现

    上一节我们分析了广告索引的维护有2种,全量索引加载和增量索引维护.因为广告检索是广告系统中最为重要的环节,大家一定要认真理解我们索引设计的思路,接下来我们来编码实现索引维护功能. 我们来定义一个接口, ...

  8. [Spring cloud 一步步实现广告系统] 12. 广告索引介绍

    索引设计介绍 在我们广告系统中,为了我们能更快的拿到我们想要的广告数据,我们需要对广告数据添加类似于数据库index一样的索引结构,分两大类:正向索引和倒排索引. 正向索引 通过唯一键/主键生成与对象 ...

  9. [Spring cloud 一步步实现广告系统] 11. 使用Feign实现微服务调用

    上一节我们使用了Ribbon(基于Http/Tcp)进行微服务的调用,Ribbon的调用比较简单,通过Ribbon组件对请求的服务进行拦截,通过Eureka Server 获取到服务实例的IP:Por ...

随机推荐

  1. redis可视化客户端

    1. 场景描述 redis安装完成后,使用命令行看还是有点不方便,github上有开源的redis可视化客户端,很好用,介绍下. 2.解决方案 使用redisclient客户端,来操作redis. 2 ...

  2. printf函数的返回值

    先看下面一段程序: 文末会给大家推荐几本好书,希望能够需要的朋友一点帮助! #include <stdio.h> int main() { int i = 123; printf(&quo ...

  3. 关于int和integer

    大家可以看一下下面这个java程序的运行结果 int k = 1; int l = 1; System.out.println(k == l); int a = 128; int b = 128; S ...

  4. NOIP2018普及T4暨洛谷P5018 对称二叉树题解

    题目链接:https://www.luogu.org/problemnew/show/P5018 花絮:这道题真的比历年的t4都简单的多呀,而且本蒟蒻做得出t4做不出t3呜呜呜... 这道题可以是一只 ...

  5. 洛谷P1690 贪婪的Copy 题解

    题目:https://www.luogu.org/problemnew/show/P1690 分析: 这道题就是一道最短路的题目,因为看到数据范围: n≤100n\leq100n≤100 所以考虑使用 ...

  6. Flume框架的学习使用

    Flume框架的学习使用 Flume简介 Flume提供一个分布式的,可靠的,对大数据量的日志进行高效收集.聚集.移动的服务. Flume基于流失架构,容错性强,也很灵活简单 Flume,kafka用 ...

  7. .Net Core2.2 WebApi上传文件

    基于.net core2.2的webapi程序,接收客户端上传的文件.按照以下写法,file的值永远是null [HttpPost] public void Post([FromForm] IForm ...

  8. 2019牛客暑期多校训练营(第三场)H题目

    题意:给你一个N×N的矩阵,求最大的子矩阵 满足子矩阵中最大值和最小值之差小于等于m. 思路:这题是求满足条件的最大子矩阵,毫无疑问要遍历所有矩阵,并判断矩阵是某满足这个条件,那么我们大致只要解决两个 ...

  9. containsObject 总是不含有,你会用吗

    结论:containsObject:是在比较内存地址,即使两个对象内容完全一样,地址不同,那也是不同的.我个人认为这个方法应该叫是否存在同一个对象 (开始不知道这个知识,被坑,至少浪费了3个钟头,数组 ...

  10. [系列] Go gRPC Hello World

    目录 概述 四类服务方法 安装 写个 Hello World 服务 推荐阅读 概述 开始 gRPC 了,这篇文章学习使用 gRPC,输出一个 Hello World. 用 Go 实现 gRPC 的服务 ...