[Spring cloud 一步步实现广告系统] 18. 查询返回广告创意
根據三個維度繼續過濾
在上一節中我們實現了根據流量信息過濾的代碼,但是我們的條件有可能是多條件一起傳給我們的檢索服務的,本節我們繼續實現根據推廣單元的三個維度條件的過濾。
- 在SearchImpl類中添加過濾方法
- 定義三個方法實現過濾
根據推廣單元id獲取推廣創意
我們知道,推廣單元和推廣創意的關系是多對多,從上文我們查詢到了推廣單元ids,接下來我們實現根據推廣單元id獲取推廣創意的代碼,let's code.
首先,我們需要在com.sxzhongf.ad.index.creative_relation_unit.CreativeRelationUnitIndexAwareImpl 關聯索引中查到推廣創意的ids
然后得到了推廣創意的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中我們實現廣告創意的查詢.
...//獲取 推廣計劃 對象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) ...根據廣告位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());}); }- 組建搜索返回對象
正常業務場景中,同一個廣告位可以展示多個廣告信息,也可以只展示一個廣告信息,這個需要根據具體的業務場景來做不同的處理,本次為了演示方便,會從返回的創意列表中隨機選擇一個創意廣告信息進行展示,當然大家也可以根據業務類型,設置不同的優先級或者權重值來進行廣告選擇。
完整的請求過濾實現方法:
@Service @Slf4j public class SearchImpl implements ISearch {@Overridepublic 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);}//獲取 推廣計劃 對象listList<AdUnitIndexObject> unitIndexObjects = IndexDataTableUtils.of(AdUnitIndexAwareImpl.class).fetch(targetUnitIdSet);//根據狀態過濾數據filterAdUnitAndPlanStatus(unitIndexObjects, CommonStatus.VALID);//獲取 推廣創意 id listList<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接口
@PostMapping("/fetchAd")public SearchResponse fetchAdCreative(@RequestBody SearchRequest request) {log.info("ad-serach: fetchAd ->{}", JSON.toJSONString(request));return search.fetchAds(request);}
上文中,我們實現了檢索服務的核心邏輯,接下來,我們需要對外暴露我們的廣告檢索服務接口,在SearchController中提供:實現API網關配置
zuul: routes:sponsor: #在路由中自定義服務路由名稱path: /ad-sponsor/**serviceId: mscx-ad-sponsor #微服務namestrip-prefix: falsesearch: #在路由中自定義服務路由名稱path: /ad-search/**serviceId: mscx-ad-search #微服務namestrip-prefix: false prefix: /gateway/api strip-prefix: true #不對 prefix: /gateway/api 設置的路徑進行截取,默認轉發會截取掉配置的前綴
轉載于:https://www.cnblogs.com/zhangpan1244/p/11349029.html
總結
以上是生活随笔為你收集整理的[Spring cloud 一步步实现广告系统] 18. 查询返回广告创意的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring @CrossOrigin
- 下一篇: CentOS系统如何搭建离线yum源