[Spring cloud 一步步实现广告系统] 18. 查询返回广告创意
- 转载:www.GuoXiongfei.cn
- /
- 时间:2019-08-14 20:55:53
- /
- 浏览:93,942次
- /
- 分类:css , Javascript , Java , DataBase , app
根据三个维度继续过滤在上一节中我们实现了根据流量信息过滤的代码,但是我们的条件有可能是多条件一起传给我们的检索服务的,本节我们继续实现根据推广单元的三个维度条件的过滤。在SearchImpl类中添加过滤方法publicclassSearchImplimplementsISearch{@OverridepublicSearchResponsefetchAds(SearchRequestrequest...
根据三个维度继续过滤
在上一节中我们实现了根据流量信息过滤的代码,但是我们的条件有可能是多条件一起传给我们的检索服务的,本节我们继续实现根据推广单元的三个维度条件的过滤。
- 在
SearchImpl
类中添加过滤方法
public class SearchImpl implements ISearch { @Override public SearchResponse fetchAds(SearchRequest request) { ...// 根据三个维度过滤if (featureRelation == FeatureRelation.AND) { filterKeywordFeature(adUnitIdSet, keywordFeature); filterHobbyFeature(adUnitIdSet, hobbyFeatrue); filterDistrictFeature(adUnitIdSet, districtFeature); targetUnitIdSet = adUnitIdSet;} else { getOrRelationUnitIds(adUnitIdSet, keywordFeature, hobbyFeatrue, districtFeature);} } return null; }
- 定义三个方法实现过滤
/** * 获取三个维度各自满足时的广告id */ private Set<Long> getOrRelationUnitIds(Set<Long> adUnitIdsSet, KeywordFeature keywordFeature, HobbyFeatrue hobbyFeatrue, DistrictFeature districtFeature) { if (CollectionUtils.isEmpty(adUnitIdsSet)) return Collections.EMPTY_SET; // 我们在处理的时候,需要对副本进行处理,大家可以考虑一下为什么需要这么做? Set<Long> keywordUnitIdSet = new HashSet<>(adUnitIdsSet); Set<Long> hobbyUnitIdSet = new HashSet<>(adUnitIdsSet); Set<Long> districtUnitIdSet = new HashSet<>(adUnitIdsSet); filterKeywordFeature(keywordUnitIdSet, keywordFeature); filterHobbyFeature(hobbyUnitIdSet, hobbyFeatrue); filterDistrictFeature(districtUnitIdSet, districtFeature); // 返回它们的并集 return new HashSet<>( CollectionUtils.union(CollectionUtils.union(keywordUnitIdSet, hobbyUnitIdSet),districtUnitIdSet ) ); } /** * 根据传递的关键词过滤 */ private void filterKeywordFeature(Collection<Long> adUnitIds, KeywordFeature keywordFeature) { if (CollectionUtils.isEmpty(adUnitIds)) return; if (CollectionUtils.isNotEmpty(keywordFeature.getKeywords())) {// 如果存在需要过滤的关键词,查找索引实例对象进行过滤处理CollectionUtils.filter( adUnitIds, adUnitId -> IndexDataTableUtils.of(UnitKeywordIndexAwareImpl.class).match(adUnitId, keywordFeature.getKeywords())); } } /** * 根据传递的兴趣信息过滤 */ private void filterHobbyFeature(Collection<Long> adUnitIds, HobbyFeatrue hobbyFeatrue) { if (CollectionUtils.isEmpty(adUnitIds)) return; // 如果存在需要过滤的兴趣,查找索引实例对象进行过滤处理 if (CollectionUtils.isNotEmpty(hobbyFeatrue.getHobbys())) {CollectionUtils.filter( adUnitIds, adUnitId -> IndexDataTableUtils.of(UnitHobbyIndexAwareImpl.class).match(adUnitId, hobbyFeatrue.getHobbys())); } } /** * 根据传递的地域信息过滤 */ private void filterDistrictFeature(Collection<Long> adUnitIds, DistrictFeature districtFeature) { if (CollectionUtils.isEmpty(adUnitIds)) return; // 如果存在需要过滤的地域信息,查找索引实例对象进行过滤处理 if (CollectionUtils.isNotEmpty(districtFeature.getProvinceAndCities())) {CollectionUtils.filter( adUnitIds, adUnitId -> {return IndexDataTableUtils.of(UnitDistrictIndexAwareImpl.class) .match(adUnitId, districtFeature.getProvinceAndCities()); }); } }
根据推广单元id获取推广创意
我们知道,推广单元和推广创意的关系是多对多,从上文我们查询到了推广单元ids,接下来我们实现根据推广单元id获取推广创意的代码,let's code.
首先,我们需要在com.sxzhongf.ad.index.creative_relation_unit.CreativeRelationUnitIndexAwareImpl
关联索引中查到推广创意的ids
/** * 通过推广单元id获取推广创意id */ public List<Long> selectAdCreativeIds(List<AdUnitIndexObject> unitIndexObjects) { if (CollectionUtils.isEmpty(unitIndexObjects)) return Collections.emptyList(); //获取要返回的广告创意ids List<Long> result = new ArrayList<>(); for (AdUnitIndexObject unitIndexObject : unitIndexObjects) {//根据推广单元id获取推广创意Set<Long> adCreativeIds = unitRelationCreativeMap.get(unitIndexObject.getUnitId());if (CollectionUtils.isNotEmpty(adCreativeIds)) result.addAll(adCreativeIds); } return result; }
然后得到了推广创意的id list后,我们在创意索引实现类com.sxzhongf.ad.index.creative.CreativeIndexAwareImpl
中定义根据ids查询创意的方法。
/** * 根据ids获取创意list */public List<CreativeIndexObject> findAllByIds(Collection<Long> ids) { if (CollectionUtils.isEmpty(ids)) return Collections.emptyList(); List<CreativeIndexObject> result = new ArrayList<>(); for (Long id : ids) { CreativeIndexObject object = get(id); if (null != object)result.add(object); } return result;}
自此,我们已经得到了想要的推广单元和推广创意,因为推广单元包含了推广计划,所以我们想要的数据已经全部可以获取到了,接下来,我们还得过滤一次当前我们查询到的数据的状态,因为有的数据,我们可能已经进行过逻辑删除了,因此还需要判断获取的数据是否有效。在SearchImpl
类中实现。
/*** 根据状态信息过滤数据*/ private void filterAdUnitAndPlanStatus(List<AdUnitIndexObject> unitIndexObjects, CommonStatus status) {if (CollectionUtils.isEmpty(unitIndexObjects)) return;//同时判断推广单元和推广计划的状态CollectionUtils.filter( unitIndexObjects, unitIndexObject -> unitIndexObject.getUnitStatus().equals(status.getStatus()) && unitIndexObject.getAdPlanIndexObject().getPlanStatus().equals(status.getStatus())); }
在SearchImpl
中我们实现广告创意的查询.
...//获取 推广计划 对象listList<AdUnitIndexObject> unitIndexObjects = IndexDataTableUtils.of(AdUnitIndexAwareImpl.class).fetch(adUnitIdSet);//根据状态过滤数据filterAdUnitAndPlanStatus(unitIndexObjects, CommonStatus.VALID);//获取 推广创意 id listList<Long> creativeIds = IndexDataTableUtils.of(CreativeRelationUnitIndexAwareImpl.class) .selectAdCreativeIds(unitIndexObjects);//根据 推广创意ids获取推广创意List<CreativeIndexObject> creativeIndexObjects = IndexDataTableUtils.of(CreativeIndexAwareImpl.class)...
根据广告位adslot 实现对创意数据的过滤
因为我们的广告位是有不同的大小,不同的类型,因此,我们在获取到所有符合我们查询维度以及流量类型的条件后,还需要针对不同的广告位来展示不同的广告创意信息。
/*** 根据广告位类型以及参数获取展示的合适广告信息** @param creativeIndexObjects 所有广告创意* @param width 广告位width* @param height广告位height*/private void filterCreativeByAdSlot(List<CreativeIndexObject> creativeIndexObjects, Integer width, Integer height, List<Integer> type) { if (CollectionUtils.isEmpty(creativeIndexObjects)) return; CollectionUtils.filter( creativeIndexObjects, creative -> { //审核状态必须是通过 return creative.getAuditStatus().equals(CommonStatus.VALID.getStatus()) && creative.getWidth().equals(width) && creative.getHeight().equals(height) && type.contains(creative.getType()); } );}
- 组建搜索返回对象
正常业务场景中,同一个广告位可以展示多个广告信息,也可以只展示一个广告信息,这个需要根据具体的业务场景来做不同的处理,本次为了演示方便,会从返回的创意列表中随机选择一个创意广告信息进行展示,当然大家也可以根据业务类型,设置不同的优先级或者权重值来进行广告选择。
/** * 从创意列表中随机获取一条创意广告返回出