天天看點

[Spring cloud 一步步實作廣告系統] 18. 查詢傳回廣告創意

根據三個次元繼續過濾

在上一節中我們實作了根據流量資訊過濾的代碼,但是我們的條件有可能是多條件一起傳給我們的檢索服務的,本節我們繼續實作根據推廣單元的三個次元條件的過濾。

  • 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

中我們實作廣告創意的查詢.

...

//擷取 推廣計劃 對象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());
          }
  );
}           
  • 組建搜尋傳回對象

    正常業務場景中,同一個廣告位可以展示多個廣告資訊,也可以隻展示一個廣告資訊,這個需要根據具體的業務場景來做不同的處理,本次為了示範友善,會從傳回的創意清單中随機選擇一個創意廣告資訊進行展示,當然大家也可以根據業務類型,設定不同的優先級或者權重值來進行廣告選擇。

/**
 * 從創意清單中随機擷取一條創意廣告傳回出去
 *
 * @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

    中提供:
@PostMapping("/fetchAd")
    public SearchResponse fetchAdCreative(@RequestBody SearchRequest request) {
        log.info("ad-serach: fetchAd ->{}", JSON.toJSONString(request));
        return search.fetchAds(request);
    }           
  • 實作API網關配置
    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 設定的路徑進行截取,預設轉發會截取掉配置的字首