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

根據三個維度繼續過濾

在上一節中咱們實現了根據流量信息過濾的代碼,可是咱們的條件有多是多條件一塊兒傳給咱們的檢索服務的,本節咱們繼續實現根據推廣單元的三個維度條件的過濾。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&lt;&gt;(adUnitIdsSet);
        Set<long> hobbyUnitIdSet = new HashSet&lt;&gt;(adUnitIdsSet);
        Set<long> districtUnitIdSet = new HashSet&lt;&gt;(adUnitIdsSet);

        filterKeywordFeature(keywordUnitIdSet, keywordFeature);
        filterHobbyFeature(hobbyUnitIdSet, hobbyFeatrue);
        filterDistrictFeature(districtUnitIdSet, districtFeature);

        // 返回它們的並集
        return new HashSet&lt;&gt;(
                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 -&gt; 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 -&gt; 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 -&gt; {
                        return IndexDataTableUtils.of(UnitDistrictIndexAwareImpl.class)
                                                  .match(adUnitId, districtFeature.getProvinceAndCities());
                    }
            );
        }
    }
根據推廣單元id獲取推廣創意

咱們知道,推廣單元和推廣創意的關係是多對多,從上文咱們查詢到了推廣單元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&lt;&gt;();
        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&lt;&gt;();

    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 -&gt; unitIndexObject.getUnitStatus().equals(status.getStatus()) &amp;&amp;
                      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)
...
根據廣告位adslot 實現對創意數據的過濾

由於咱們的廣告位是有不一樣的大小,不一樣的類型,所以,咱們在獲取到全部符合咱們查詢維度以及流量類型的條件後,還須要針對不一樣的廣告位來展現不一樣的廣告創意信息。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 -&gt; {
              //審覈狀態必須是經過
              return creative.getAuditStatus().equals(CommonStatus.VALID.getStatus())
                      &amp;&amp; creative.getWidth().equals(width)
                      &amp;&amp; creative.getHeight().equals(height)
                      &amp;&amp; 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&lt;&gt;();
    //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>&gt; 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 -&gt;{}", 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>
相關文章
相關標籤/搜索