在上一节中咱们实现了根据流量信息过滤的代码,可是咱们的条件有多是多条件一块儿传给咱们的检索服务的,本节咱们继续实现根据推广单元的三个维度条件的过滤。java
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()); } ); } }
咱们知道,推广单元和推广创意的关系是多对多,从上文咱们查询到了推广单元ids,接下来咱们实现根据推广单元id获取推广创意的代码,let's code. 首先,咱们须要在com.sxzhongf.ad.index.creative_relation_unit.CreativeRelationUnitIndexAwareImpl
关联索引中查到推广创意的idsapi
/** * 经过推广单元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
类中实现。app
/** * 根据状态信息过滤数据 */ 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
中咱们实现广告创意的查询.dom
... //获取 推广计划 对象list List<adunitindexobject> unitIndexObjects = IndexDataTableUtils.of(AdUnitIndexAwareImpl.class).fetch(adUnitIdSet); //根据状态过滤数据 filterAdUnitAndPlanStatus(unitIndexObjects, CommonStatus.VALID); //获取 推广创意 id list List<long> creativeIds = IndexDataTableUtils.of(CreativeRelationUnitIndexAwareImpl.class) .selectAdCreativeIds(unitIndexObjects); //根据 推广创意ids获取推广创意 List<creativeindexobject> creativeIndexObjects = IndexDataTableUtils.of(CreativeIndexAwareImpl.class) ...
由于咱们的广告位是有不一样的大小,不一样的类型,所以,咱们在获取到全部符合咱们查询维度以及流量类型的条件后,还须要针对不一样的广告位来展现不一样的广告创意信息。ide
/** * 根据广告位类型以及参数获取展现的合适广告信息 * * @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()); } ); }
/** * 从创意列表中随机获取一条创意广告返回出去 * * @param creativeIndexObjects 创意广告list */ private List<searchresponse.creative> buildCreativeResponse(List<creativeindexobject> creativeIndexObjects) { if (CollectionUtils.isEmpty(creativeIndexObjects)) return Collections.EMPTY_LIST; //随机获取一个广告创意,也能够实现优先级排序,也能够根据权重值等等,具体根据业务 CreativeIndexObject randomObject = creativeIndexObjects.get( Math.abs(new Random().nextInt()) % creativeIndexObjects.size() ); //List<searchresponse.creative> result = new ArrayList<>(); //result.add(SearchResponse.convert(randomObject)); return Collections.singletonList( SearchResponse.convert(randomObject) ); }
完整的请求过滤实现方法:微服务
@Service @Slf4j public class SearchImpl implements ISearch { @Override public SearchResponse fetchAds(SearchRequest request) { //获取请求广告位信息 List<adslot> adSlotList = request.getRequestInfo().getAdSlots(); //获取三个Feature信息 KeywordFeature keywordFeature = request.getFeatureInfo().getKeywordFeature(); HobbyFeatrue hobbyFeatrue = request.getFeatureInfo().getHobbyFeatrue(); DistrictFeature districtFeature = request.getFeatureInfo().getDistrictFeature(); //Feature关系 FeatureRelation featureRelation = request.getFeatureInfo().getRelation(); //构造响应对象 SearchResponse response = new SearchResponse(); Map<string, list<searchresponse.creative>> adSlotRelationAds = response.getAdSlotRelationAds(); for (AdSlot adSlot : adSlotList) { Set<long> targetUnitIdSet; //根据流量类型从缓存中获取 初始 广告信息 Set<long> adUnitIdSet = IndexDataTableUtils.of( AdUnitIndexAwareImpl.class ).match(adSlot.getPositionType()); // 根据三个维度过滤 if (featureRelation == FeatureRelation.AND) { filterKeywordFeature(adUnitIdSet, keywordFeature); filterHobbyFeature(adUnitIdSet, hobbyFeatrue); filterDistrictFeature(adUnitIdSet, districtFeature); targetUnitIdSet = adUnitIdSet; } else { targetUnitIdSet = getOrRelationUnitIds(adUnitIdSet, keywordFeature, hobbyFeatrue, districtFeature); } //获取 推广计划 对象list List<adunitindexobject> unitIndexObjects = IndexDataTableUtils.of(AdUnitIndexAwareImpl.class) .fetch(targetUnitIdSet); //根据状态过滤数据 filterAdUnitAndPlanStatus(unitIndexObjects, CommonStatus.VALID); //获取 推广创意 id list List<long> creativeIds = IndexDataTableUtils.of(CreativeRelationUnitIndexAwareImpl.class) .selectAdCreativeIds(unitIndexObjects); //根据 推广创意ids获取推广创意 List<creativeindexobject> creativeIndexObjects = IndexDataTableUtils.of(CreativeIndexAwareImpl.class) .fetch(creativeIds); //根据 广告位adslot 实现对创意数据的过滤 filterCreativeByAdSlot(creativeIndexObjects, adSlot.getWidth(), adSlot.getHeight(), adSlot.getType()); //一个广告位能够展现多个广告,也能够仅展现一个广告,具体根据业务来定 adSlotRelationAds.put( adSlot.getAdSlotCode(), buildCreativeResponse(creativeIndexObjects) ); } return response; } ...
暴露API接口 上文中,咱们实现了检索服务的核心逻辑,接下来,咱们须要对外暴露咱们的广告检索服务接口,在SearchController
中提供:fetch
@PostMapping("/fetchAd") public SearchResponse fetchAdCreative(@RequestBody SearchRequest request) { log.info("ad-serach: fetchAd ->{}", JSON.toJSONString(request)); return search.fetchAds(request); }
实现API网关配置ui
zuul: routes: sponsor: #在路由中自定义服务路由名称 path: /ad-sponsor/** serviceId: mscx-ad-sponsor #微服务name strip-prefix: false search: #在路由中自定义服务路由名称 path: /ad-search/** serviceId: mscx-ad-search #微服务name strip-prefix: false prefix: /gateway/api strip-prefix: true #不对 prefix: /gateway/api 设置的路径进行截取,默认转发会截取掉配置的前缀
</creativeindexobject></long></adunitindexobject></long></long></string,></adslot></searchresponse.creative></creativeindexobject></searchresponse.creative></integer></creativeindexobject></creativeindexobject></long></adunitindexobject></adunitindexobject></creativeindexobject></long></creativeindexobject></long></long></adunitindexobject></long></long></long></long></long></long></long></long></long>